From d0b2a6b8550f0bee50b8ef0fcc2870173989aa06 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 26 Jan 2025 17:00:46 +0000 Subject: [PATCH] Deployed 58cb1db with MkDocs version: 1.6.1 --- .nojekyll | 0 404.html | 464 ++ algorithm-readme-template/index.html | 1141 +++ .../evolutionary-algorithms/index.html | 497 ++ .../expert-systems/index.html | 497 ++ algorithms/artificial-intelligence/index.html | 546 ++ .../knowledge-based-systems/index.html | 497 ++ .../reinforcement-learning/index.html | 497 ++ .../search-and-optimization/index.html | 497 ++ .../image-augmentation/index.html | 497 ++ .../image-processing/index.html | 497 ++ algorithms/computer-vision/index.html | 535 ++ .../object-detection/index.html | 497 ++ .../semantic-segmentation/index.html | 497 ++ .../deep-learning/architectures/index.html | 497 ++ algorithms/deep-learning/index.html | 535 ++ .../convolutional-neural-network/index.html | 714 ++ .../deep-learning/neural-networks/index.html | 502 ++ .../optimization-algorithms/index.html | 497 ++ .../pre-trained-models/index.html | 497 ++ .../ac-gan/index.html | 787 ++ .../basic-gan/index.html | 727 ++ .../c-gan/index.html | 755 ++ .../eb-gan/index.html | 729 ++ .../index.html | 542 ++ .../info-gan/index.html | 759 ++ algorithms/index.html | 631 ++ .../large-language-models/bert/index.html | 497 ++ .../large-language-models/bloom/index.html | 497 ++ .../gpt-series/index.html | 497 ++ algorithms/large-language-models/index.html | 535 ++ .../large-language-models/t5/index.html | 497 ++ .../machine-learning/boosting/index.html | 501 ++ .../boosting/light-gbm/index.html | 703 ++ .../data-preprocessing/encoding/index.html | 502 ++ .../encoding/ordinal-encoder/index.html | 669 ++ .../data-preprocessing/imputation/index.html | 497 ++ .../data-preprocessing/index.html | 526 ++ .../scaling-and-normalization/index.html | 513 ++ .../min-max-scaler/index.html | 689 ++ .../standard-scaler/index.html | 700 ++ algorithms/machine-learning/index.html | 535 ++ .../supervised/classifications/index.html | 497 ++ .../machine-learning/supervised/index.html | 513 ++ .../AdaBoost_Regression/index.html | 711 ++ .../Decision_Tree_Regression/index.html | 714 ++ .../Elastic_Net_Regression/index.html | 601 ++ .../Gradient_Boosting_Regression/index.html | 727 ++ .../regressions/Huber_Regression/index.html | 614 ++ .../K_Nearest_Neighbors_Regression/index.html | 603 ++ .../regressions/Lasso_Regression/index.html | 616 ++ .../Logistic_Regression/index.html | 633 ++ .../Neural_Network_Regression/index.html | 637 ++ .../Polynomial_Regression/index.html | 623 ++ .../Random_Forest_Regression/index.html | 753 ++ .../regressions/Ridge_Regression/index.html | 617 ++ .../Support_Vector_Regression/index.html | 649 ++ .../XG_Boost_Regression/index.html | 636 ++ .../regressions/bayesian/index.html | 610 ++ .../supervised/regressions/index.html | 512 ++ .../supervised/regressions/linear/index.html | 624 ++ .../unsupervised/clustering/index.html | 502 ++ .../clustering/kmeans-clustering/index.html | 720 ++ .../dimensionality-reduction/index.html | 497 ++ .../machine-learning/unsupervised/index.html | 513 ++ .../Bag_Of_Words/index.html | 536 ++ .../Fast_Text/index.html | 915 +++ .../GloVe/index.html | 824 ++ .../NLTK_Setup/index.html | 788 ++ .../N_L_P_Introduction/index.html | 648 ++ .../Text_PreProcessing_Techniques/index.html | 965 +++ .../Tf_Idf/index.html | 843 +++ .../Transformers/index.html | 669 ++ .../Word_2_Vec/index.html | 862 +++ .../Word_Embeddings/index.html | 846 +++ .../natural-language-processing/index.html | 591 ++ algorithms/statistics/descriptive/index.html | 497 ++ algorithms/statistics/index.html | 536 ++ algorithms/statistics/inferential/index.html | 497 ++ .../errors/Mean_Absolute_Error/index.html | 503 ++ .../errors/Mean_Squared_Error/index.html | 503 ++ .../errors/R2_Squared_Error/index.html | 506 ++ .../errors/Root_Mean_Squared_Error/index.html | 504 ++ .../statistics/metrics-and-losses/index.html | 488 ++ .../Cross_Entropy_Loss/index.html | 655 ++ .../loss-functions/Hinge_Loss/index.html | 524 ++ .../index.html | 539 ++ .../loss-functions/Ranking_Losses/index.html | 571 ++ algorithms/statistics/probability/index.html | 497 ++ assets/images/favicon.png | Bin 0 -> 1870 bytes assets/javascripts/bundle.60a45f97.min.js | 16 + assets/javascripts/bundle.60a45f97.min.js.map | 7 + assets/javascripts/lunr/min/lunr.ar.min.js | 1 + assets/javascripts/lunr/min/lunr.da.min.js | 18 + assets/javascripts/lunr/min/lunr.de.min.js | 18 + assets/javascripts/lunr/min/lunr.du.min.js | 18 + assets/javascripts/lunr/min/lunr.el.min.js | 1 + assets/javascripts/lunr/min/lunr.es.min.js | 18 + assets/javascripts/lunr/min/lunr.fi.min.js | 18 + assets/javascripts/lunr/min/lunr.fr.min.js | 18 + assets/javascripts/lunr/min/lunr.he.min.js | 1 + assets/javascripts/lunr/min/lunr.hi.min.js | 1 + assets/javascripts/lunr/min/lunr.hu.min.js | 18 + assets/javascripts/lunr/min/lunr.hy.min.js | 1 + assets/javascripts/lunr/min/lunr.it.min.js | 18 + assets/javascripts/lunr/min/lunr.ja.min.js | 1 + assets/javascripts/lunr/min/lunr.jp.min.js | 1 + assets/javascripts/lunr/min/lunr.kn.min.js | 1 + assets/javascripts/lunr/min/lunr.ko.min.js | 1 + assets/javascripts/lunr/min/lunr.multi.min.js | 1 + assets/javascripts/lunr/min/lunr.nl.min.js | 18 + assets/javascripts/lunr/min/lunr.no.min.js | 18 + assets/javascripts/lunr/min/lunr.pt.min.js | 18 + assets/javascripts/lunr/min/lunr.ro.min.js | 18 + assets/javascripts/lunr/min/lunr.ru.min.js | 18 + assets/javascripts/lunr/min/lunr.sa.min.js | 1 + .../lunr/min/lunr.stemmer.support.min.js | 1 + assets/javascripts/lunr/min/lunr.sv.min.js | 18 + assets/javascripts/lunr/min/lunr.ta.min.js | 1 + assets/javascripts/lunr/min/lunr.te.min.js | 1 + assets/javascripts/lunr/min/lunr.th.min.js | 1 + assets/javascripts/lunr/min/lunr.tr.min.js | 18 + assets/javascripts/lunr/min/lunr.vi.min.js | 1 + assets/javascripts/lunr/min/lunr.zh.min.js | 1 + assets/javascripts/lunr/tinyseg.js | 206 + assets/javascripts/lunr/wordcut.js | 6708 +++++++++++++++++ .../workers/search.f8cc74c7.min.js | 42 + .../workers/search.f8cc74c7.min.js.map | 7 + assets/stylesheets/main.a40c8224.min.css | 1 + assets/stylesheets/main.a40c8224.min.css.map | 1 + assets/stylesheets/palette.06af60db.min.css | 1 + .../stylesheets/palette.06af60db.min.css.map | 1 + contribute/index.html | 1243 +++ customs/extra.css | 49 + index.html | 662 ++ project-readme-template/index.html | 1273 ++++ projects/artificial-intelligence/index.html | 497 ++ .../index.html | 801 ++ .../brightness-control/index.html | 919 +++ .../counting-bicep-reps/index.html | 832 ++ .../computer-vision/face-detection/index.html | 816 ++ projects/computer-vision/index.html | 520 ++ .../index.html | 1014 +++ .../brain-tumor-detection-model/index.html | 784 ++ projects/deep-learning/index.html | 502 ++ .../index.html | 497 ++ projects/index.html | 631 ++ projects/large-language-models/index.html | 497 ++ .../air-quality-prediction/index.html | 962 +++ .../index.html | 1201 +++ .../index.html | 1308 ++++ .../heart-disease-detection-model/index.html | 921 +++ projects/machine-learning/index.html | 503 ++ .../poker-hand-prediction/index.html | 1089 +++ .../sleep-quality-prediction/index.html | 743 ++ .../used-cars-price-prediction/index.html | 1009 +++ .../chatbot-project-implementation/index.html | 845 +++ .../email_spam_detection/index.html | 933 +++ .../natural-language-processing/index.html | 501 ++ .../name_entity_recognition/index.html | 795 ++ .../next-word-pred/index.html | 869 +++ .../twitter_sentiment_analysis/index.html | 931 +++ .../index.html | 1194 +++ projects/statistics/index.html | 503 ++ search/search_index.json | 1 + sitemap.xml | 475 ++ sitemap.xml.gz | Bin 0 -> 1434 bytes 167 files changed, 86715 insertions(+) create mode 100644 .nojekyll create mode 100644 404.html create mode 100644 algorithm-readme-template/index.html create mode 100644 algorithms/artificial-intelligence/evolutionary-algorithms/index.html create mode 100644 algorithms/artificial-intelligence/expert-systems/index.html create mode 100644 algorithms/artificial-intelligence/index.html create mode 100644 algorithms/artificial-intelligence/knowledge-based-systems/index.html create mode 100644 algorithms/artificial-intelligence/reinforcement-learning/index.html create mode 100644 algorithms/artificial-intelligence/search-and-optimization/index.html create mode 100644 algorithms/computer-vision/image-augmentation/index.html create mode 100644 algorithms/computer-vision/image-processing/index.html create mode 100644 algorithms/computer-vision/index.html create mode 100644 algorithms/computer-vision/object-detection/index.html create mode 100644 algorithms/computer-vision/semantic-segmentation/index.html create mode 100644 algorithms/deep-learning/architectures/index.html create mode 100644 algorithms/deep-learning/index.html create mode 100644 algorithms/deep-learning/neural-networks/convolutional-neural-network/index.html create mode 100644 algorithms/deep-learning/neural-networks/index.html create mode 100644 algorithms/deep-learning/optimization-algorithms/index.html create mode 100644 algorithms/deep-learning/pre-trained-models/index.html create mode 100644 algorithms/generative-adversarial-networks/ac-gan/index.html create mode 100644 algorithms/generative-adversarial-networks/basic-gan/index.html create mode 100644 algorithms/generative-adversarial-networks/c-gan/index.html create mode 100644 algorithms/generative-adversarial-networks/eb-gan/index.html create mode 100644 algorithms/generative-adversarial-networks/index.html create mode 100644 algorithms/generative-adversarial-networks/info-gan/index.html create mode 100644 algorithms/index.html create mode 100644 algorithms/large-language-models/bert/index.html create mode 100644 algorithms/large-language-models/bloom/index.html create mode 100644 algorithms/large-language-models/gpt-series/index.html create mode 100644 algorithms/large-language-models/index.html create mode 100644 algorithms/large-language-models/t5/index.html create mode 100644 algorithms/machine-learning/boosting/index.html create mode 100644 algorithms/machine-learning/boosting/light-gbm/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/encoding/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/imputation/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/scaling-and-normalization/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/index.html create mode 100644 algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/index.html create mode 100644 algorithms/machine-learning/index.html create mode 100644 algorithms/machine-learning/supervised/classifications/index.html create mode 100644 algorithms/machine-learning/supervised/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Huber_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Lasso_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Logistic_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Polynomial_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Ridge_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/bayesian/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/index.html create mode 100644 algorithms/machine-learning/supervised/regressions/linear/index.html create mode 100644 algorithms/machine-learning/unsupervised/clustering/index.html create mode 100644 algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/index.html create mode 100644 algorithms/machine-learning/unsupervised/dimensionality-reduction/index.html create mode 100644 algorithms/machine-learning/unsupervised/index.html create mode 100644 algorithms/natural-language-processing/Bag_Of_Words/index.html create mode 100644 algorithms/natural-language-processing/Fast_Text/index.html create mode 100644 algorithms/natural-language-processing/GloVe/index.html create mode 100644 algorithms/natural-language-processing/NLTK_Setup/index.html create mode 100644 algorithms/natural-language-processing/N_L_P_Introduction/index.html create mode 100644 algorithms/natural-language-processing/Text_PreProcessing_Techniques/index.html create mode 100644 algorithms/natural-language-processing/Tf_Idf/index.html create mode 100644 algorithms/natural-language-processing/Transformers/index.html create mode 100644 algorithms/natural-language-processing/Word_2_Vec/index.html create mode 100644 algorithms/natural-language-processing/Word_Embeddings/index.html create mode 100644 algorithms/natural-language-processing/index.html create mode 100644 algorithms/statistics/descriptive/index.html create mode 100644 algorithms/statistics/index.html create mode 100644 algorithms/statistics/inferential/index.html create mode 100644 algorithms/statistics/metrics-and-losses/errors/Mean_Absolute_Error/index.html create mode 100644 algorithms/statistics/metrics-and-losses/errors/Mean_Squared_Error/index.html create mode 100644 algorithms/statistics/metrics-and-losses/errors/R2_Squared_Error/index.html create mode 100644 algorithms/statistics/metrics-and-losses/errors/Root_Mean_Squared_Error/index.html create mode 100644 algorithms/statistics/metrics-and-losses/index.html create mode 100644 algorithms/statistics/metrics-and-losses/loss-functions/Cross_Entropy_Loss/index.html create mode 100644 algorithms/statistics/metrics-and-losses/loss-functions/Hinge_Loss/index.html create mode 100644 algorithms/statistics/metrics-and-losses/loss-functions/Kullback_Leibler_Divergence_Loss/index.html create mode 100644 algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/index.html create mode 100644 algorithms/statistics/probability/index.html create mode 100644 assets/images/favicon.png create mode 100644 assets/javascripts/bundle.60a45f97.min.js create mode 100644 assets/javascripts/bundle.60a45f97.min.js.map create mode 100644 assets/javascripts/lunr/min/lunr.ar.min.js create mode 100644 assets/javascripts/lunr/min/lunr.da.min.js create mode 100644 assets/javascripts/lunr/min/lunr.de.min.js create mode 100644 assets/javascripts/lunr/min/lunr.du.min.js create mode 100644 assets/javascripts/lunr/min/lunr.el.min.js create mode 100644 assets/javascripts/lunr/min/lunr.es.min.js create mode 100644 assets/javascripts/lunr/min/lunr.fi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.fr.min.js create mode 100644 assets/javascripts/lunr/min/lunr.he.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hu.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hy.min.js create mode 100644 assets/javascripts/lunr/min/lunr.it.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ja.min.js create mode 100644 assets/javascripts/lunr/min/lunr.jp.min.js create mode 100644 assets/javascripts/lunr/min/lunr.kn.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ko.min.js create mode 100644 assets/javascripts/lunr/min/lunr.multi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.nl.min.js create mode 100644 assets/javascripts/lunr/min/lunr.no.min.js create mode 100644 assets/javascripts/lunr/min/lunr.pt.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ro.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ru.min.js create mode 100644 assets/javascripts/lunr/min/lunr.sa.min.js create mode 100644 assets/javascripts/lunr/min/lunr.stemmer.support.min.js create mode 100644 assets/javascripts/lunr/min/lunr.sv.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ta.min.js create mode 100644 assets/javascripts/lunr/min/lunr.te.min.js create mode 100644 assets/javascripts/lunr/min/lunr.th.min.js create mode 100644 assets/javascripts/lunr/min/lunr.tr.min.js create mode 100644 assets/javascripts/lunr/min/lunr.vi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.zh.min.js create mode 100644 assets/javascripts/lunr/tinyseg.js create mode 100644 assets/javascripts/lunr/wordcut.js create mode 100644 assets/javascripts/workers/search.f8cc74c7.min.js create mode 100644 assets/javascripts/workers/search.f8cc74c7.min.js.map create mode 100644 assets/stylesheets/main.a40c8224.min.css create mode 100644 assets/stylesheets/main.a40c8224.min.css.map create mode 100644 assets/stylesheets/palette.06af60db.min.css create mode 100644 assets/stylesheets/palette.06af60db.min.css.map create mode 100644 contribute/index.html create mode 100644 customs/extra.css create mode 100644 index.html create mode 100644 project-readme-template/index.html create mode 100644 projects/artificial-intelligence/index.html create mode 100644 projects/computer-vision/black_and_white_image_colorizer/index.html create mode 100644 projects/computer-vision/brightness-control/index.html create mode 100644 projects/computer-vision/counting-bicep-reps/index.html create mode 100644 projects/computer-vision/face-detection/index.html create mode 100644 projects/computer-vision/index.html create mode 100644 projects/computer-vision/music_genre_classification_model/index.html create mode 100644 projects/deep-learning/brain-tumor-detection-model/index.html create mode 100644 projects/deep-learning/index.html create mode 100644 projects/generative-adversarial-networks/index.html create mode 100644 projects/index.html create mode 100644 projects/large-language-models/index.html create mode 100644 projects/machine-learning/air-quality-prediction/index.html create mode 100644 projects/machine-learning/cardiovascular-disease-prediction/index.html create mode 100644 projects/machine-learning/health-insurance-cross-sell-prediction/index.html create mode 100644 projects/machine-learning/heart-disease-detection-model/index.html create mode 100644 projects/machine-learning/index.html create mode 100644 projects/machine-learning/poker-hand-prediction/index.html create mode 100644 projects/machine-learning/sleep-quality-prediction/index.html create mode 100644 projects/machine-learning/used-cars-price-prediction/index.html create mode 100644 projects/natural-language-processing/chatbot-project-implementation/index.html create mode 100644 projects/natural-language-processing/email_spam_detection/index.html create mode 100644 projects/natural-language-processing/index.html create mode 100644 projects/natural-language-processing/name_entity_recognition/index.html create mode 100644 projects/natural-language-processing/next-word-pred/index.html create mode 100644 projects/natural-language-processing/twitter_sentiment_analysis/index.html create mode 100644 projects/statistics/bangladesh-premier-league-analysis/index.html create mode 100644 projects/statistics/index.html create mode 100644 search/search_index.json create mode 100644 sitemap.xml create mode 100644 sitemap.xml.gz diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 00000000..e69de29b diff --git a/404.html b/404.html new file mode 100644 index 00000000..afeab349 --- /dev/null +++ b/404.html @@ -0,0 +1,464 @@ + + + + + + + + + + + + + + + + + + + AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ +

404 - Not found

+ +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithm-readme-template/index.html b/algorithm-readme-template/index.html new file mode 100644 index 00000000..8a815267 --- /dev/null +++ b/algorithm-readme-template/index.html @@ -0,0 +1,1141 @@ + + + + + + + + + + + + + + + + + + + + + + + 🧮 Algorithm Template - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + + + + + + + + + +
+
+ + + + + + + + +

🧮 Algorithm Title

+ +
+ +
+ +

🎯 Objective

+ +
    +
  • Example: "This is a K-Nearest Neighbors (KNN) classifier algorithm used for classifying data points based on their proximity to other points in the dataset."
  • +
+

📚 Prerequisites

+ + +
    +
  • Linear Algebra Basics
  • +
  • Probability and Statistics
  • +
  • Libraries: NumPy, TensorFlow, PyTorch (as applicable)
  • +
+
+

🧩 Inputs

+ +
    +
  • Example: The input dataset should be in CSV format with features and labels for supervised learning algorithms.
  • +
+

📤 Outputs

+ +
    +
  • Example: The algorithm returns a predicted class label or a regression value for each input sample.
  • +
+
+

🏛️ Algorithm Architecture

+ +
    +
  • Example: "The neural network consists of 3 layers: an input layer, one hidden layer with 128 units, and an output layer with 10 units for classification."
  • +
+

🏋️‍♂️ Training Process

+ +
    +
  • Example:
      +
    • The model is trained using the gradient descent optimizer.
    • +
    • Learning rate: 0.01
    • +
    • Batch size: 32
    • +
    • Number of epochs: 50
    • +
    • Validation set: 20% of the training data
    • +
    +
  • +
+

📊 Evaluation Metrics

+ +
    +
  • Example: "Accuracy and F1-Score are used to evaluate the classification performance of the model. Cross-validation is used to reduce overfitting."
  • +
+
+

💻 Code Implementation

+ +
# Example: Bayesian Regression implementation
+
+import numpy as np
+from sklearn.linear_model import BayesianRidge
+import matplotlib.pyplot as plt
+
+# Generate Synthetic Data
+np.random.seed(42)
+X = np.random.rand(20, 1) * 10
+y = 3 * X.squeeze() + np.random.randn(20) * 2
+
+# Initialize and Train Bayesian Ridge Regression
+model = BayesianRidge(alpha_1=1e-6, lambda_1=1e-6, compute_score=True)
+model.fit(X, y)
+
+# Make Predictions
+X_test = np.linspace(0, 10, 100).reshape(-1, 1)
+y_pred, y_std = model.predict(X_test, return_std=True)
+
+# Display Results
+print("Coefficients:", model.coef_)
+print("Intercept:", model.intercept_)
+
+# Visualization
+plt.figure(figsize=(8, 5))
+plt.scatter(X, y, color="blue", label="Training Data")
+plt.plot(X_test, y_pred, color="red", label="Mean Prediction")
+plt.fill_between(
+    X_test.squeeze(),
+    y_pred - y_std,
+    y_pred + y_std,
+    color="orange",
+    alpha=0.3,
+    label="Predictive Uncertainty",
+)
+plt.title("Bayesian Regression with Predictive Uncertainty")
+plt.xlabel("X")
+plt.ylabel("y")
+plt.legend()
+plt.show()
+
+

🔍 Scratch Code Explanation

+ + +

Bayesian Regression is a probabilistic approach to linear regression that incorporates prior beliefs and updates these beliefs based on observed data to form posterior distributions of the model parameters. Below is a breakdown of the implementation, structured for clarity and understanding.

+
+

1. Class Constructor: Initialization

+
class BayesianRegression:
+    def __init__(self, alpha=1, beta=1):
+        """
+        Constructor for the BayesianRegression class.
+
+        Parameters:
+        - alpha: Prior precision (controls the weight of the prior belief).
+        - beta: Noise precision (inverse of noise variance in the data).
+        """
+        self.alpha = alpha
+        self.beta = beta
+        self.w_mean = None
+        self.w_precision = None
+
+
    +
  • Key Idea
      +
    • The alpha (Prior precision, representing our belief in the model parameters' variability) and beta (Precision of the noise in the data) hyperparameters are crucial to controlling the Bayesian framework. A higher alpha means stronger prior belief in smaller weights, while beta controls the confidence in the noise level of the observations.
    • +
    • w_mean - Posterior mean of weights (initialized as None)
    • +
    • w_precision - Posterior precision matrix (initialized as None)
    • +
    +
  • +
+
+

2. Fitting the Model: Bayesian Learning

+
def fit(self, X, y):
+    """
+    Fit the Bayesian Regression model to the input data.
+
+    Parameters:
+    - X: Input features (numpy array of shape [n_samples, n_features]).
+    - y: Target values (numpy array of shape [n_samples]).
+    """
+    # Add a bias term to X for intercept handling.
+    X = np.c_[np.ones(X.shape[0]), X]
+
+    # Compute the posterior precision matrix.
+    self.w_precision = (
+        self.alpha * np.eye(X.shape[1])  # Prior contribution.
+        + self.beta * X.T @ X  # Data contribution.
+    )
+
+    # Compute the posterior mean of the weights.
+    self.w_mean = np.linalg.solve(self.w_precision, self.beta * X.T @ y)
+
+

Key Steps in the Fitting Process

+
    +
  1. Add Bias Term: The bias term (column of ones) is added to X to account for the intercept in the linear model.
  2. +
  3. +

    Posterior Precision Matrix: + $$ + \mathbf{S}_w^{-1} = \alpha \mathbf{I} + \beta \mathbf{X}^\top \mathbf{X} + $$

    +
      +
    • The prior contributes \(\alpha \mathbf{I}\), which regularizes the weights.
    • +
    • The likelihood contributes \(\beta \mathbf{X}^\top \mathbf{X}\), based on the observed data.
    • +
    +
  4. +
  5. +

    Posterior Mean of Weights: + $$ + \mathbf{m}_w = \mathbf{S}_w \beta \mathbf{X}^\top \mathbf{y} + $$

    +
      +
    • This reflects the most probable weights under the posterior distribution, balancing prior beliefs and observed data.
    • +
    +
  6. +
+
+

3. Making Predictions: Posterior Inference

+
def predict(self, X):
+    """
+    Make predictions on new data.
+
+    Parameters:
+    - X: Input features for prediction (numpy array of shape [n_samples, n_features]).
+
+    Returns:
+    - Predicted values (numpy array of shape [n_samples]).
+    """
+    # Add a bias term to X for intercept handling.
+    X = np.c_[np.ones(X.shape[0]), X]
+
+    # Compute the mean of the predictions using the posterior mean of weights.
+    y_pred = X @ self.w_mean
+
+    return y_pred
+
+

Key Prediction Details

+
    +
  1. Adding Bias Term: The bias term ensures that predictions account for the intercept term in the model.
  2. +
  3. Posterior Predictive Mean: + $$ + \hat{\mathbf{y}} = \mathbf{X} \mathbf{m}_w + $$
      +
    • This computes the expected value of the targets using the posterior mean of the weights.
    • +
    +
  4. +
+
+

4. Code Walkthrough

+
    +
  • Posterior Precision Matrix (\(\mathbf{S}_w^{-1}\)): Balances the prior (\(\alpha \mathbf{I}\)) and the data (\(\beta \mathbf{X}^\top \mathbf{X}\)) to regularize and incorporate observed evidence.
  • +
  • Posterior Mean (\(\mathbf{m}_w\)): Encodes the most likely parameter values given the data and prior.
  • +
  • Prediction (\(\hat{\mathbf{y}}\)): Uses the posterior mean to infer new outputs, accounting for both prior knowledge and learned data trends.
  • +
+
+

🛠️ Example Usage

+ + +
# Example Data
+X = np.array([[1.0], [2.0], [3.0]])  # Features
+y = np.array([2.0, 4.0, 6.0])        # Targets
+
+# Initialize and Train Model
+model = BayesianRegression(alpha=1.0, beta=1.0)
+model.fit(X, y)
+
+# Predict on New Data
+X_new = np.array([[4.0], [5.0]])
+y_pred = model.predict(X_new)
+
+print(f"Predictions: {y_pred}")
+
+
    +
  • Explanation
      +
    • A small dataset is provided where the relationship between \(X\) and \(y\) is linear.
    • +
    • The model fits this data by learning posterior distributions of the weights.
    • +
    • Predictions are made for new inputs using the learned posterior mean.
    • +
    +
  • +
+
+

🌟 Advantages

+
    +
  • Encodes uncertainty explicitly, providing confidence intervals for predictions.
  • +
  • Regularization is naturally incorporated through prior distributions.
  • +
  • Handles small datasets effectively by leveraging prior knowledge.
  • +
+

⚠️ Limitations

+
    +
  • Computationally intensive for high-dimensional data due to matrix inversions.
  • +
  • Sensitive to prior hyperparameters (\(\alpha, \beta\)).
  • +
+

🚀 Application

+ + +
+
+
+

Explain your application

+
+
+

Explain your application

+
+
+
+ + + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/artificial-intelligence/evolutionary-algorithms/index.html b/algorithms/artificial-intelligence/evolutionary-algorithms/index.html new file mode 100644 index 00000000..bdbe8ddb --- /dev/null +++ b/algorithms/artificial-intelligence/evolutionary-algorithms/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Evolutionary Algorithms 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Evolutionary Algorithms 💡

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/artificial-intelligence/expert-systems/index.html b/algorithms/artificial-intelligence/expert-systems/index.html new file mode 100644 index 00000000..80c9647e --- /dev/null +++ b/algorithms/artificial-intelligence/expert-systems/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Expert Systems 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Expert Systems 💡

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/artificial-intelligence/index.html b/algorithms/artificial-intelligence/index.html new file mode 100644 index 00000000..4081d8ff --- /dev/null +++ b/algorithms/artificial-intelligence/index.html @@ -0,0 +1,546 @@ + + + + + + + + + + + + + + + + + + + + + Artificial Intelligence 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/artificial-intelligence/knowledge-based-systems/index.html b/algorithms/artificial-intelligence/knowledge-based-systems/index.html new file mode 100644 index 00000000..266cfc98 --- /dev/null +++ b/algorithms/artificial-intelligence/knowledge-based-systems/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Knowledge Based Systems 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Knowledge Based Systems 💡

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/artificial-intelligence/reinforcement-learning/index.html b/algorithms/artificial-intelligence/reinforcement-learning/index.html new file mode 100644 index 00000000..32ca75a5 --- /dev/null +++ b/algorithms/artificial-intelligence/reinforcement-learning/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Reinforcement Learning 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Reinforcement Learning 💡

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/artificial-intelligence/search-and-optimization/index.html b/algorithms/artificial-intelligence/search-and-optimization/index.html new file mode 100644 index 00000000..b5afd01b --- /dev/null +++ b/algorithms/artificial-intelligence/search-and-optimization/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Search and Optimization 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Search and Optimization 💡

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/computer-vision/image-augmentation/index.html b/algorithms/computer-vision/image-augmentation/index.html new file mode 100644 index 00000000..794a6151 --- /dev/null +++ b/algorithms/computer-vision/image-augmentation/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Image Augmentation 🎥 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Image Augmentation 🎥

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/computer-vision/image-processing/index.html b/algorithms/computer-vision/image-processing/index.html new file mode 100644 index 00000000..68c591e4 --- /dev/null +++ b/algorithms/computer-vision/image-processing/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Image Processing 🎥 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Image Processing 🎥

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/computer-vision/index.html b/algorithms/computer-vision/index.html new file mode 100644 index 00000000..d412f31b --- /dev/null +++ b/algorithms/computer-vision/index.html @@ -0,0 +1,535 @@ + + + + + + + + + + + + + + + + + + + + + Computer Vision 🎥 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/computer-vision/object-detection/index.html b/algorithms/computer-vision/object-detection/index.html new file mode 100644 index 00000000..35340ba2 --- /dev/null +++ b/algorithms/computer-vision/object-detection/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Object Detection 🎥 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Object Detection 🎥

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/computer-vision/semantic-segmentation/index.html b/algorithms/computer-vision/semantic-segmentation/index.html new file mode 100644 index 00000000..fc1d4b1d --- /dev/null +++ b/algorithms/computer-vision/semantic-segmentation/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Semantic Segmentation 🎥 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Semantic Segmentation 🎥

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/deep-learning/architectures/index.html b/algorithms/deep-learning/architectures/index.html new file mode 100644 index 00000000..e47a770a --- /dev/null +++ b/algorithms/deep-learning/architectures/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Architectures ✨ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Architectures ✨

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/deep-learning/index.html b/algorithms/deep-learning/index.html new file mode 100644 index 00000000..2dea2691 --- /dev/null +++ b/algorithms/deep-learning/index.html @@ -0,0 +1,535 @@ + + + + + + + + + + + + + + + + + + + + + Deep Learning ✨ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/deep-learning/neural-networks/convolutional-neural-network/index.html b/algorithms/deep-learning/neural-networks/convolutional-neural-network/index.html new file mode 100644 index 00000000..e3c64c0f --- /dev/null +++ b/algorithms/deep-learning/neural-networks/convolutional-neural-network/index.html @@ -0,0 +1,714 @@ + + + + + + + + + + + + + + + + + + + + + Convolutional Neural Networks - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Convolutional Neural Networks

+
+ +
+ +

Overview

+

Convolutional Neural Networks (CNNs) are a type of deep learning algorithm specifically designed for processing structured grid data such as images. They are widely used in computer vision tasks like image classification, object detection, and image segmentation.

+
+

How CNNs Work

+

1. Architecture

+

CNNs are composed of the following layers: +- Convolutional Layers: Extract spatial features from the input data. +- Pooling Layers: Reduce the spatial dimensions of feature maps to lower computational costs. +- Fully Connected Layers: Perform high-level reasoning for final predictions.

+

2. Key Concepts

+
    +
  • Filters (Kernels): Small matrices that slide over the input to extract features.
  • +
  • Strides: Step size of the filter movement.
  • +
  • Padding: Adding borders to the input for better filter coverage.
  • +
  • Activation Functions: Introduce non-linearity (e.g., ReLU).
  • +
+
+

CNN Algorithms

+

1. LeNet

+
    +
  • Proposed By: Yann LeCun (1998)
  • +
  • Use Case: Handwritten digit recognition (e.g., MNIST dataset).
  • +
  • Architecture:
  • +
  • Input → Convolution → Pooling → Convolution → Pooling → Fully Connected → Output
  • +
+

2. AlexNet

+
    +
  • Proposed By: Alex Krizhevsky (2012)
  • +
  • Use Case: ImageNet classification challenge.
  • +
  • Key Features:
  • +
  • Uses ReLU for activation.
  • +
  • Includes dropout to prevent overfitting.
  • +
  • Designed for GPUs for faster computation.
  • +
+

3. VGGNet

+
    +
  • Proposed By: Visual Geometry Group (2014)
  • +
  • Use Case: Image classification and transfer learning.
  • +
  • Key Features:
  • +
  • Uses small 3x3 filters.
  • +
  • Depth of the network increases (e.g., VGG-16, VGG-19).
  • +
+

4. ResNet

+
    +
  • Proposed By: Kaiming He et al. (2015)
  • +
  • Use Case: Solving vanishing gradient problems in deep networks.
  • +
  • Key Features:
  • +
  • Introduces residual blocks with skip connections.
  • +
  • Enables training of very deep networks (e.g., ResNet-50, ResNet-101).
  • +
+

5. MobileNet

+
    +
  • Proposed By: Google (2017)
  • +
  • Use Case: Mobile and embedded vision applications.
  • +
  • Key Features:
  • +
  • Utilizes depthwise separable convolutions.
  • +
  • Lightweight architecture suitable for mobile devices.
  • +
+
+

Code Example: Implementing a Simple CNN

+

Here’s a Python example of a CNN using TensorFlow/Keras:

+
    +
  • Sequential: Used to stack layers to create a neural network model.
  • +
  • Conv2D: Implements the convolutional layers to extract features from input images.
  • +
  • MaxPooling2D: Reduces the size of feature maps while retaining important features.
  • +
  • Flatten: Converts 2D feature maps into a 1D vector to pass into fully connected layers.
  • +
  • Dense: Implements fully connected (dense) layers, responsible for decision-making.
  • +
+
from tensorflow.keras.models import Sequential
+from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
+
+# Build the CNN
+model = Sequential([
+    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
+    MaxPooling2D(pool_size=(2, 2)),
+    Conv2D(64, (3, 3), activation='relu'),
+    MaxPooling2D(pool_size=(2, 2)),
+    Flatten(),
+    Dense(128, activation='relu'),
+    Dense(10, activation='softmax')  # Replace 10 with the number of classes in your dataset
+])
+
+# Compile the model
+model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
+
+# Summary
+model.summary()
+
+
+

Visualizations

+
    +
  • Filters and Feature Maps: Visualizing how the CNN learns features from images.
  • +
  • Training Metrics: Plotting accuracy and loss during training.
  • +
+
import matplotlib.pyplot as plt
+
+# Example: Visualizing accuracy and loss
+plt.plot(history.history['accuracy'], label='Accuracy')
+plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
+plt.xlabel('Epochs')
+plt.ylabel('Accuracy')
+plt.legend()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/deep-learning/neural-networks/index.html b/algorithms/deep-learning/neural-networks/index.html new file mode 100644 index 00000000..350aa471 --- /dev/null +++ b/algorithms/deep-learning/neural-networks/index.html @@ -0,0 +1,502 @@ + + + + + + + + + + + + + + + + + + + + + Neural Networks ✨ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/deep-learning/optimization-algorithms/index.html b/algorithms/deep-learning/optimization-algorithms/index.html new file mode 100644 index 00000000..dbbf7f94 --- /dev/null +++ b/algorithms/deep-learning/optimization-algorithms/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Optimization Algorithms ✨ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Optimization Algorithms ✨

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/deep-learning/pre-trained-models/index.html b/algorithms/deep-learning/pre-trained-models/index.html new file mode 100644 index 00000000..b1c58b39 --- /dev/null +++ b/algorithms/deep-learning/pre-trained-models/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Pre-Trained Models ✨ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Pre-Trained Models ✨

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/generative-adversarial-networks/ac-gan/index.html b/algorithms/generative-adversarial-networks/ac-gan/index.html new file mode 100644 index 00000000..68fc4b40 --- /dev/null +++ b/algorithms/generative-adversarial-networks/ac-gan/index.html @@ -0,0 +1,787 @@ + + + + + + + + + + + + + + + + + + + + + AC GAN - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

AC GAN

+
+ +
+ +

Overview

+

Auxiliary Classifier Generative Adversarial Network (ACGAN) is an extension of the traditional GAN architecture. It incorporates class information into both the generator and discriminator, enabling controlled generation of samples with specific characteristics.

+

ACGANs can: +- Generate high-quality images conditioned on specific classes. +- Predict class labels of generated images via the discriminator.

+

This dual capability allows for more controlled and targeted image synthesis.

+
+

Key Concepts

+
    +
  1. +

    Generator:

    +
      +
    • Takes random noise and class labels as input to generate synthetic images conditioned on the class labels.
    • +
    +
  2. +
  3. +

    Discriminator:

    +
      +
    • Differentiates between real and fake images.
    • +
    • Predicts the class labels of images.
    • +
    +
  4. +
  5. +

    Class Conditioning:

    +
      +
    • By integrating label embeddings, the generator learns to associate specific features with each class, enhancing image quality and controllability.
    • +
    +
  6. +
+
+

Implementation Overview

+

Below is a high-level explanation of the ACGAN implementation:

+
    +
  1. +

    Dataset:

    +
      +
    • The MNIST dataset is used for training, consisting of grayscale images of digits (0-9).
    • +
    +
  2. +
  3. +

    Model Architecture:

    +
      +
    • Generator:
        +
      • Takes random noise (latent vector) and class labels as input.
      • +
      • Outputs images that correspond to the input class labels.
      • +
      +
    • +
    • Discriminator:
        +
      • Classifies images as real or fake.
      • +
      • Simultaneously predicts the class label of the image.
      • +
      +
    • +
    +
  4. +
  5. +

    Training Process:

    +
      +
    • The generator is trained to fool the discriminator into classifying fake images as real.
    • +
    • The discriminator is trained to:
        +
      • Differentiate real from fake images.
      • +
      • Accurately predict the class labels of real images.
      • +
      +
    • +
    +
  6. +
  7. +

    Loss Functions:

    +
      +
    • Binary Cross-Entropy Loss for real/fake classification.
    • +
    • Categorical Cross-Entropy Loss for class label prediction.
    • +
    +
  8. +
+
+

Implementation Code

+

Core Components

+

Discriminator

+
class Discriminator(nn.Module):
+    def __init__(self):
+        super(Discriminator, self).__init__()
+        self.label_emb = nn.Embedding(num_classes, num_classes)
+        self.model = nn.Sequential(
+            nn.Linear(image_size + num_classes, hidden_size),
+            nn.LeakyReLU(0.2),
+            nn.Dropout(0.3),
+            nn.Linear(hidden_size, hidden_size),
+            nn.LeakyReLU(0.2),
+            nn.Dropout(0.3),
+            nn.Linear(hidden_size, 1),
+            nn.Sigmoid()
+        )
+
+    def forward(self, x, labels):
+        x = x.view(x.size(0), image_size)
+        c = self.label_emb(labels)
+        x = torch.cat([x, c], 1)
+        return self.model(x)
+
+

Generator

+
class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.label_emb = nn.Embedding(num_classes, num_classes)
+        self.model = nn.Sequential(
+            nn.Linear(latent_size + num_classes, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z, labels):
+        z = z.view(z.size(0), latent_size)
+        c = self.label_emb(labels)
+        x = torch.cat([z, c], 1)
+        return self.model(x)
+
+

Training Loop

+
for epoch in range(num_epochs):
+    for i, (images, labels) in enumerate(train_loader):
+        batch_size = images.size(0)
+        images = images.to(device)
+        labels = labels.to(device)
+
+        # Real and fake labels
+        real_labels = torch.ones(batch_size, 1).to(device)
+        fake_labels = torch.zeros(batch_size, 1).to(device)
+
+        # Train Discriminator
+        outputs = D(images, labels)
+        d_loss_real = criterion(outputs, real_labels)
+
+        z = create_noise(batch_size, latent_size)
+        fake_images = G(z, labels)
+        outputs = D(fake_images, labels)
+        d_loss_fake = criterion(outputs, fake_labels)
+
+        d_loss = d_loss_real + d_loss_fake
+        D.zero_grad()
+        d_loss.backward()
+        d_optimizer.step()
+
+        # Train Generator
+        z = create_noise(batch_size, latent_size)
+        fake_images = G(z, labels)
+        outputs = D(fake_images, labels)
+        g_loss = criterion(outputs, real_labels)
+
+        G.zero_grad()
+        g_loss.backward()
+        g_optimizer.step()
+
+        if (i+1) % 200 == 0:
+            print(f"Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}")
+
+
+

Applications of ACGAN

+
    +
  1. +

    Image Synthesis:

    +
      +
    • Generate diverse images conditioned on specific labels.
    • +
    +
  2. +
  3. +

    Data Augmentation:

    +
      +
    • Create synthetic data to augment existing datasets.
    • +
    +
  4. +
  5. +

    Creative Domains:

    +
      +
    • Design tools for controlled image generation in fashion, gaming, and media.
    • +
    +
  6. +
+
+

Additional Resources

+ + + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/generative-adversarial-networks/basic-gan/index.html b/algorithms/generative-adversarial-networks/basic-gan/index.html new file mode 100644 index 00000000..311e546d --- /dev/null +++ b/algorithms/generative-adversarial-networks/basic-gan/index.html @@ -0,0 +1,727 @@ + + + + + + + + + + + + + + + + + + + + + Basic GAN - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Basic GAN

+
Basic GAN stands for Basic Generative Adversarial Network
+
+

This folder contains a basic implementation of a Generative Adversarial Network (GAN) using PyTorch. GANs are a type of neural network architecture that consists of two networks: a generator and a discriminator. The generator learns to create realistic data samples (e.g., images) from random noise, while the discriminator learns to distinguish between real and generated samples.

+

Overview

+

This project implements a simple GAN architecture to generate hand-written digits resembling those from the MNIST dataset. The generator network creates fake images, while the discriminator network tries to differentiate between real and generated images. The networks are trained simultaneously in a minimax game until the generator produces realistic images.

+
+

Files

+
import torch
+import torch.nn as nn
+import torch.optim as optim
+import torchvision.datasets as dsets
+import torchvision.transforms as transforms
+from torch.utils.data import DataLoader
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+image_size = 784  # 28x28
+hidden_size = 256
+latent_size = 64
+num_epochs = 200
+batch_size = 100
+learning_rate = 0.0002
+
+# MNIST dataset
+dataset = dsets.MNIST(root='../data/',
+                      train=True,
+                      transform=transforms.ToTensor(),
+                      download=True)
+
+# Data loader
+data_loader = DataLoader(dataset=dataset,
+                         batch_size=batch_size, 
+                         shuffle=True)
+
+# Discriminator
+D = nn.Sequential(
+    nn.Linear(image_size, hidden_size),
+    nn.LeakyReLU(0.2),
+    nn.Linear(hidden_size, hidden_size),
+    nn.LeakyReLU(0.2),
+    nn.Linear(hidden_size, 1),
+    nn.Sigmoid())
+
+# Generator
+G = nn.Sequential(
+    nn.Linear(latent_size, hidden_size),
+    nn.ReLU(),
+    nn.Linear(hidden_size, hidden_size),
+    nn.ReLU(),
+    nn.Linear(hidden_size, image_size),
+    nn.Tanh())
+
+# Device setting
+D = D.to(device)
+G = G.to(device)
+
+# Binary cross entropy loss and optimizer
+criterion = nn.BCELoss()
+d_optimizer = optim.Adam(D.parameters(), lr=learning_rate)
+g_optimizer = optim.Adam(G.parameters(), lr=learning_rate)
+
+# Utility function to create real and fake labels
+def create_real_labels(size):
+    data = torch.ones(size, 1)
+    return data.to(device)
+
+def create_fake_labels(size):
+    data = torch.zeros(size, 1)
+    return data.to(device)
+
+# Utility function to generate random noise
+def create_noise(size, latent_dim):
+    return torch.randn(size, latent_dim).to(device)
+
+# Training the GAN
+total_step = len(data_loader)
+for epoch in range(num_epochs):
+    for i, (images, _) in enumerate(data_loader):
+        batch_size = images.size(0)
+        images = images.reshape(batch_size, -1).to(device)
+
+        # Create the labels which are later used as input for the BCE loss
+        real_labels = create_real_labels(batch_size)
+        fake_labels = create_fake_labels(batch_size)
+
+        # ================================================================== #
+        #                      Train the discriminator                       #
+        # ================================================================== #
+        # Compute BCELoss using real images
+        # Second term of the loss is always zero since real_labels == 1
+        outputs = D(images)
+        d_loss_real = criterion(outputs, real_labels)
+        real_score = outputs
+
+        # Compute BCELoss using fake images
+        noise = create_noise(batch_size, latent_size)
+        fake_images = G(noise)
+        outputs = D(fake_images)
+        d_loss_fake = criterion(outputs, fake_labels)
+        fake_score = outputs
+
+        # Backprop and optimize
+        d_loss = d_loss_real + d_loss_fake
+        d_optimizer.zero_grad()
+        d_loss.backward()
+        d_optimizer.step()
+
+        # ================================================================== #
+        #                        Train the generator                         #
+        # ================================================================== #
+        # Compute loss with fake images
+        noise = create_noise(batch_size, latent_size)
+        fake_images = G(noise)
+        outputs = D(fake_images)
+
+        # We train G to maximize log(D(G(z)) instead of minimizing log(1-D(G(z)))
+        # For the reason, look at the last part of section 3 of the paper:
+        # https://arxiv.org/pdf/1406.2661.pdf
+        g_loss = criterion(outputs, real_labels)
+
+        # Backprop and optimize
+        g_optimizer.zero_grad()
+        g_loss.backward()
+        g_optimizer.step()
+
+        if (i+1) % 200 == 0:
+            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}, D(x): {real_score.mean().item():.2f}, D(G(z)): {fake_score.mean().item():.2f}')
+
+# Save the trained models
+torch.save(G.state_dict(), 'G.pth')
+torch.save(D.state_dict(), 'D.pth')
+
+# Plot some generated images
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+G.eval()
+with torch.no_grad():
+    noise = create_noise(64, latent_size)
+    fake_images = G(noise)
+    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)
+    fake_images = denorm(fake_images)
+    grid = np.transpose(fake_images.cpu(), (0, 2, 3, 1)).numpy()
+
+    plt.figure(figsize=(8, 8))
+    for i in range(grid.shape[0]):
+        plt.subplot(8, 8, i+1)
+        plt.imshow(grid[i, :, :, 0], cmap='gray')
+        plt.axis('off')
+    plt.show()
+
+
    +
  • BasicGAN.py: Contains the implementation of the GAN model, training loop, and saving of trained models.
  • +
+
import torch
+import torch.nn as nn
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+latent_size = 64
+hidden_size = 256
+image_size = 784  # 28x28
+
+# Generator
+G = nn.Sequential(
+    nn.Linear(latent_size, hidden_size),
+    nn.ReLU(),
+    nn.Linear(hidden_size, hidden_size),
+    nn.ReLU(),
+    nn.Linear(hidden_size, image_size),
+    nn.Tanh())
+
+# Load the trained generator model
+G.load_state_dict(torch.load('G.pth'))
+G.to(device)
+G.eval()
+
+# Utility function to generate random noise
+def create_noise(size, latent_dim):
+    return torch.randn(size, latent_dim).to(device)
+
+# Utility function to denormalize the images
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+# Generate images
+with torch.no_grad():
+    noise = create_noise(64, latent_size)
+    fake_images = G(noise)
+    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)
+    fake_images = denorm(fake_images)
+    grid = np.transpose(fake_images.cpu(), (0, 2, 3, 1)).numpy()
+
+    plt.figure(figsize=(8, 8))
+    for i in range(grid.shape[0]):
+        plt.subplot(8, 8, i+1)
+        plt.imshow(grid[i, :, :, 0], cmap='gray')
+        plt.axis('off')
+    plt.show()
+
+
    +
  • test_BasicGAN.py: Uses the trained generator to generate sample images after training.
  • +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/generative-adversarial-networks/c-gan/index.html b/algorithms/generative-adversarial-networks/c-gan/index.html new file mode 100644 index 00000000..ac065f36 --- /dev/null +++ b/algorithms/generative-adversarial-networks/c-gan/index.html @@ -0,0 +1,755 @@ + + + + + + + + + + + + + + + + + + + + + C GAN - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

C GAN

+

This folder contains an implementation of a Conditional Generative Adversarial Network (cGAN) using PyTorch. cGANs generate images conditioned on specific class labels, allowing for controlled image synthesis.

+
+

Overview

+

cGANs extend the traditional GAN architecture by including class information in both the generator and discriminator. The generator learns to generate images conditioned on given class labels, while the discriminator not only distinguishes between real and fake images but also predicts the class labels of the generated images.

+
+

Files

+
import torch
+import torch.nn as nn
+import torch.optim as optim
+import torchvision.datasets as dsets
+import torchvision.transforms as transforms
+from torch.utils.data import DataLoader
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+image_size = 28 * 28
+num_classes = 10
+latent_size = 100
+hidden_size = 256
+num_epochs = 100
+batch_size = 64
+learning_rate = 0.0002
+
+# MNIST dataset
+transform = transforms.Compose([
+    transforms.ToTensor(),
+    transforms.Normalize(mean=(0.5,), std=(0.5,))
+])
+
+train_dataset = dsets.MNIST(root='../data/',
+                            train=True,
+                            transform=transform,
+                            download=True)
+
+train_loader = DataLoader(dataset=train_dataset,
+                          batch_size=batch_size,
+                          shuffle=True)
+
+# Discriminator
+class Discriminator(nn.Module):
+    def __init__(self):
+        super(Discriminator, self).__init__()
+        self.label_emb = nn.Embedding(num_classes, num_classes)
+
+        self.model = nn.Sequential(
+            nn.Linear(image_size + num_classes, hidden_size),
+            nn.LeakyReLU(0.2),
+            nn.Dropout(0.3),
+            nn.Linear(hidden_size, hidden_size),
+            nn.LeakyReLU(0.2),
+            nn.Dropout(0.3),
+            nn.Linear(hidden_size, 1),
+            nn.Sigmoid()
+        )
+
+    def forward(self, x, labels):
+        x = x.view(x.size(0), image_size)
+        c = self.label_emb(labels)
+        x = torch.cat([x, c], 1)
+        out = self.model(x)
+        return out
+
+# Generator
+class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.label_emb = nn.Embedding(num_classes, num_classes)
+
+        self.model = nn.Sequential(
+            nn.Linear(latent_size + num_classes, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z, labels):
+        z = z.view(z.size(0), latent_size)
+        c = self.label_emb(labels)
+        x = torch.cat([z, c], 1)
+        out = self.model(x)
+        return out
+
+# Initialize models
+D = Discriminator().to(device)
+G = Generator().to(device)
+
+# Loss function and optimizer
+criterion = nn.BCELoss()
+d_optimizer = optim.Adam(D.parameters(), lr=learning_rate)
+g_optimizer = optim.Adam(G.parameters(), lr=learning_rate)
+
+# Utility functions
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+def create_noise(batch_size, latent_size):
+    return torch.randn(batch_size, latent_size).to(device)
+
+def create_labels(batch_size):
+    return torch.randint(0, num_classes, (batch_size,)).to(device)
+
+# Training the cGAN
+total_step = len(train_loader)
+for epoch in range(num_epochs):
+    for i, (images, labels) in enumerate(train_loader):
+        batch_size = images.size(0)
+        images = images.to(device)
+        labels = labels.to(device)
+
+        # Create the labels which are later used as input for the discriminator
+        real_labels = torch.ones(batch_size, 1).to(device)
+        fake_labels = torch.zeros(batch_size, 1).to(device)
+
+        # ================================================================== #
+        #                      Train the discriminator                       #
+        # ================================================================== #
+
+        # Compute BCELoss using real images
+        outputs = D(images, labels)
+        d_loss_real = criterion(outputs, real_labels)
+        real_score = outputs
+
+        # Compute BCELoss using fake images
+        z = create_noise(batch_size, latent_size)
+        fake_images = G(z, labels)
+        outputs = D(fake_images, labels)
+        d_loss_fake = criterion(outputs, fake_labels)
+        fake_score = outputs
+
+        # Backprop and optimize
+        d_loss = d_loss_real + d_loss_fake
+        D.zero_grad()
+        d_loss.backward()
+        d_optimizer.step()
+
+        # ================================================================== #
+        #                        Train the generator                         #
+        # ================================================================== #
+
+        # Compute loss with fake images
+        z = create_noise(batch_size, latent_size)
+        fake_images = G(z, labels)
+        outputs = D(fake_images, labels)
+
+        # We train G to maximize log(D(G(z)))
+        g_loss = criterion(outputs, real_labels)
+
+        # Backprop and optimize
+        G.zero_grad()
+        g_loss.backward()
+        g_optimizer.step()
+
+        if (i+1) % 200 == 0:
+            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}, D(x): {real_score.mean().item():.2f}, D(G(z)): {fake_score.mean().item():.2f}')
+
+# Save the trained models
+torch.save(G.state_dict(), 'G_cgan.pth')
+torch.save(D.state_dict(), 'D_cgan.pth')
+
+
    +
  • cGAN.py: Contains the implementation of the ACGAN model, training loop, and saving of trained models.
  • +
+
import torch
+import torch.nn as nn
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+latent_size = 100
+num_classes = 10
+image_size = 28 * 28
+
+# Generator
+class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.label_emb = nn.Embedding(num_classes, num_classes)
+
+        self.model = nn.Sequential(
+            nn.Linear(latent_size + num_classes, 256),
+            nn.ReLU(),
+            nn.Linear(256, 512),
+            nn.ReLU(),
+            nn.Linear(512, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z, labels):
+        z = z.view(z.size(0), latent_size)
+        c = self.label_emb(labels)
+        x = torch.cat([z, c], 1)
+        out = self.model(x)
+        return out
+
+# Load the trained generator model
+G = Generator()
+G.load_state_dict(torch.load('G_cgan.pth', map_location=torch.device('cpu')))
+G.eval()
+
+# Utility function to generate random noise
+def create_noise(size, latent_dim):
+    return torch.randn(size, latent_dim)
+
+# Utility function to generate labels
+def create_labels(size):
+    return torch.randint(0, num_classes, (size,))
+
+# Utility function to denormalize the images
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+# Generate images
+with torch.no_grad():
+    noise = create_noise(64, latent_size)
+    labels = create_labels(64)
+    fake_images = G(noise, labels)
+    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)
+    fake_images = denorm(fake_images)
+    grid = np.transpose(fake_images, (0, 2, 3, 1)).numpy()
+
+    plt.figure(figsize=(8, 8))
+    for i in range(grid.shape[0]):
+        plt.subplot(8, 8, i+1)
+        plt.imshow(grid[i, :, :, 0], cmap='gray')
+        plt.axis('off')
+    plt.show()
+
+
    +
  • test_cGAN.py: Uses the trained generator to generate sample images after training.
  • +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/generative-adversarial-networks/eb-gan/index.html b/algorithms/generative-adversarial-networks/eb-gan/index.html new file mode 100644 index 00000000..c6e0bd70 --- /dev/null +++ b/algorithms/generative-adversarial-networks/eb-gan/index.html @@ -0,0 +1,729 @@ + + + + + + + + + + + + + + + + + + + + + EB GAN - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

EB GAN

+

This folder contains an implementation of an Energy-Based Generative Adversarial Network (EBGAN) using PyTorch. EBGAN focuses on matching the energy distribution of generated samples to that of real data, optimizing both a discriminator and a generator network.

+
+

Overview

+

EBGAN introduces an energy function that is used to measure the quality of generated samples. The discriminator (autoencoder-like) network tries to minimize this energy function while the generator tries to maximize it. This results in a more stable training process compared to traditional GANs.

+
+

Files

+
import torch
+import torch.nn as nn
+import torch.optim as optim
+import torchvision.datasets as dsets
+import torchvision.transforms as transforms
+from torch.utils.data import DataLoader
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+image_size = 28 * 28
+latent_size = 64
+hidden_size = 256
+num_epochs = 100
+batch_size = 64
+learning_rate = 0.0002
+k = 3  # Number of iterations for optimizing D
+
+# MNIST dataset
+transform = transforms.Compose([
+    transforms.ToTensor(),
+    transforms.Normalize(mean=(0.5,), std=(0.5,))
+])
+
+train_dataset = dsets.MNIST(root='../data/',
+                            train=True,
+                            transform=transform,
+                            download=True)
+
+train_loader = DataLoader(dataset=train_dataset,
+                          batch_size=batch_size,
+                          shuffle=True)
+
+# Discriminator
+class Discriminator(nn.Module):
+    def __init__(self):
+        super(Discriminator, self).__init__()
+        self.encoder = nn.Sequential(
+            nn.Linear(image_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, latent_size)
+        )
+        self.decoder = nn.Sequential(
+            nn.Linear(latent_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, x):
+        encoded = self.encoder(x)
+        decoded = self.decoder(encoded)
+        return decoded, encoded
+
+# Generator
+class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.model = nn.Sequential(
+            nn.Linear(latent_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, hidden_size),
+            nn.ReLU(),
+            nn.Linear(hidden_size, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z):
+        out = self.model(z)
+        return out
+
+# Initialize models
+D = Discriminator().to(device)
+G = Generator().to(device)
+
+# Loss function and optimizer
+criterion_rec = nn.MSELoss()
+d_optimizer = optim.Adam(D.parameters(), lr=learning_rate)
+g_optimizer = optim.Adam(G.parameters(), lr=learning_rate)
+
+# Utility functions
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+# Training the EBGAN
+total_step = len(train_loader)
+for epoch in range(num_epochs):
+    for i, (images, _) in enumerate(train_loader):
+        batch_size = images.size(0)
+        images = images.view(-1, image_size).to(device)
+
+        # ================================================================== #
+        #                      Train the discriminator                       #
+        # ================================================================== #
+
+        encoded_real, _ = D(images)
+        decoded_real = D.decoder(encoded_real)
+
+        rec_loss_real = criterion_rec(decoded_real, images)
+
+        z = torch.randn(batch_size, latent_size).to(device)
+        fake_images = G(z)
+        encoded_fake, _ = D(fake_images.detach())
+        decoded_fake = D.decoder(encoded_fake)
+
+        rec_loss_fake = criterion_rec(decoded_fake, fake_images.detach())
+
+        d_loss = rec_loss_real + torch.max(torch.zeros(1).to(device), k * rec_loss_real - rec_loss_fake)
+
+        D.zero_grad()
+        d_loss.backward()
+        d_optimizer.step()
+
+        # ================================================================== #
+        #                        Train the generator                         #
+        # ================================================================== #
+
+        z = torch.randn(batch_size, latent_size).to(device)
+        fake_images = G(z)
+        encoded_fake, _ = D(fake_images)
+        decoded_fake = D.decoder(encoded_fake)
+
+        rec_loss_fake = criterion_rec(decoded_fake, fake_images)
+
+        g_loss = rec_loss_fake
+
+        G.zero_grad()
+        g_loss.backward()
+        g_optimizer.step()
+
+        if (i+1) % 200 == 0:
+            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}, Rec_loss_real: {rec_loss_real.item():.4f}, Rec_loss_fake: {rec_loss_fake.item():.4f}')
+
+# Save the trained models
+torch.save(G.state_dict(), 'G_ebgan.pth')
+torch.save(D.state_dict(), 'D_ebgan.pth')
+
+
    +
  • EBGAN.py: Contains the implementation of the ACGAN model, training loop, and saving of trained models.
  • +
+
import torch
+import torch.nn as nn
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+latent_size = 64
+image_size = 28 * 28
+
+# Generator
+class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.model = nn.Sequential(
+            nn.Linear(latent_size, 256),
+            nn.ReLU(),
+            nn.Linear(256, 512),
+            nn.ReLU(),
+            nn.Linear(512, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z):
+        out = self.model(z)
+        return out
+
+# Load the trained generator model
+G = Generator()
+G.load_state_dict(torch.load('G_ebgan.pth', map_location=torch.device('cpu')))
+G.eval()
+
+# Utility function to generate random noise
+def create_noise(size, latent_dim):
+    return torch.randn(size, latent_dim)
+
+# Utility function to denormalize the images
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+# Generate images
+with torch.no_grad():
+    noise = create_noise(64, latent_size)
+    fake_images = G(noise)
+    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)
+    fake_images = denorm(fake_images)
+    grid = np.transpose(fake_images, (0, 2, 3, 1)).numpy()
+
+    plt.figure(figsize=(8, 8))
+    for i in range(grid.shape[0]):
+        plt.subplot(8, 8, i+1)
+        plt.imshow(grid[i, :, :, 0], cmap='gray')
+        plt.axis('off')
+    plt.show()
+
+
    +
  • test_EBGAN.py: Uses the trained generator to generate sample images after training.
  • +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/generative-adversarial-networks/index.html b/algorithms/generative-adversarial-networks/index.html new file mode 100644 index 00000000..293fcec8 --- /dev/null +++ b/algorithms/generative-adversarial-networks/index.html @@ -0,0 +1,542 @@ + + + + + + + + + + + + + + + + + + + + + Generative Adversarial Networks 💱 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+ + +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/generative-adversarial-networks/info-gan/index.html b/algorithms/generative-adversarial-networks/info-gan/index.html new file mode 100644 index 00000000..f0b69e85 --- /dev/null +++ b/algorithms/generative-adversarial-networks/info-gan/index.html @@ -0,0 +1,759 @@ + + + + + + + + + + + + + + + + + + + + + Info GAN - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Info GAN

+
Information Maximizing Generative Adversarial Network
+
+

This folder contains an implementation of InfoGAN using PyTorch. InfoGAN extends the traditional GAN framework by incorporating unsupervised learning of interpretable and disentangled representations.

+
+

Overview

+

InfoGAN introduces latent codes that can be split into categorical and continuous variables, allowing for more control over the generated outputs. The generator is conditioned on these latent codes, which are learned in an unsupervised manner during training.

+
+

Files

+
import torch
+import torch.nn as nn
+import torch.optim as optim
+import torchvision.datasets as dsets
+import torchvision.transforms as transforms
+from torch.utils.data import DataLoader
+import numpy as np
+import matplotlib.pyplot as plt
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+image_size = 28 * 28
+num_epochs = 50
+batch_size = 100
+latent_size = 62
+num_continuous = 2
+num_categories = 10
+learning_rate = 0.0002
+
+# MNIST dataset
+transform = transforms.Compose([
+    transforms.ToTensor(),
+    transforms.Normalize(mean=(0.5,), std=(0.5,))
+])
+
+train_dataset = dsets.MNIST(root='../data/',
+                            train=True,
+                            transform=transform,
+                            download=True)
+
+train_loader = DataLoader(dataset=train_dataset,
+                          batch_size=batch_size,
+                          shuffle=True)
+
+# Generator
+class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.fc = nn.Sequential(
+            nn.Linear(latent_size + num_categories + num_continuous, 256),
+            nn.ReLU(),
+            nn.Linear(256, 512),
+            nn.ReLU(),
+            nn.Linear(512, 1024),
+            nn.ReLU(),
+            nn.Linear(1024, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z, c_cat, c_cont):
+        inputs = torch.cat([z, c_cat, c_cont], dim=1)
+        return self.fc(inputs)
+
+
+# Discriminator
+class Discriminator(nn.Module):
+    def __init__(self):
+        super(Discriminator, self).__init__()
+        self.fc = nn.Sequential(
+            nn.Linear(image_size, 1024),
+            nn.ReLU(),
+            nn.Dropout(0.3),
+            nn.Linear(1024, 512),
+            nn.ReLU(),
+            nn.Dropout(0.3),
+        )
+
+        self.fc_disc = nn.Linear(512, num_categories)
+        self.fc_mu = nn.Linear(512, num_continuous)
+        self.fc_var = nn.Linear(512, num_continuous)
+
+    def forward(self, x):
+        x = self.fc(x)
+        disc_logits = self.fc_disc(x)
+        mu = self.fc_mu(x)
+        var = torch.exp(self.fc_var(x))
+        return disc_logits, mu, var
+
+
+# Initialize networks
+G = Generator().to(device)
+D = Discriminator().to(device)
+
+# Loss functions
+criterion_cat = nn.CrossEntropyLoss()
+criterion_cont = nn.MSELoss()
+
+# Optimizers
+g_optimizer = optim.Adam(G.parameters(), lr=learning_rate)
+d_optimizer = optim.Adam(D.parameters(), lr=learning_rate)
+
+# Utility functions
+def sample_noise(batch_size, latent_size):
+    return torch.randn(batch_size, latent_size).to(device)
+
+def sample_categorical(batch_size, num_categories):
+    return torch.randint(0, num_categories, (batch_size,)).to(device)
+
+def sample_continuous(batch_size, num_continuous):
+    return torch.rand(batch_size, num_continuous).to(device)
+
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+# Training InfoGAN
+total_step = len(train_loader)
+for epoch in range(num_epochs):
+    for i, (images, labels) in enumerate(train_loader):
+        batch_size = images.size(0)
+        images = images.view(-1, image_size).to(device)
+
+        # Create labels for discriminator
+        real_labels = torch.ones(batch_size, dtype=torch.long, device=device)
+        fake_labels = torch.zeros(batch_size, dtype=torch.long, device=device)
+
+        # Sample noise, categorical, and continuous latent codes
+        z = sample_noise(batch_size, latent_size)
+        c_cat = sample_categorical(batch_size, num_categories)
+        c_cont = sample_continuous(batch_size, num_continuous)
+
+        # Generate fake images
+        fake_images = G(z, c_cat, c_cont)
+
+        # Train discriminator
+        d_optimizer.zero_grad()
+        d_real_cat, d_real_mu, d_real_var = D(images)
+        d_real_loss_cat = criterion_cat(d_real_cat, labels)
+        d_fake_cat, d_fake_mu, d_fake_var = D(fake_images.detach())
+        d_fake_loss_cat = criterion_cat(d_fake_cat, c_cat)
+
+        d_loss_cat = d_real_loss_cat + d_fake_loss_cat
+
+        d_real_loss_cont = torch.mean(0.5 * torch.sum(torch.div((d_real_mu - c_cont)**2, d_real_var), dim=1))
+        d_fake_loss_cont = torch.mean(0.5 * torch.sum(torch.div((d_fake_mu - c_cont)**2, d_fake_var), dim=1))
+
+        d_loss_cont = d_real_loss_cont + d_fake_loss_cont
+
+        d_loss = d_loss_cat + d_loss_cont
+        d_loss.backward()
+        d_optimizer.step()
+
+        # Train generator
+        g_optimizer.zero_grad()
+        _, d_fake_mu, d_fake_var = D(fake_images)
+
+        g_loss_cat = criterion_cat(_, c_cat)
+        g_loss_cont = torch.mean(0.5 * torch.sum(torch.div((d_fake_mu - c_cont)**2, d_fake_var), dim=1))
+
+        g_loss = g_loss_cat + g_loss_cont
+        g_loss.backward()
+        g_optimizer.step()
+
+        if (i+1) % 200 == 0:
+            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}')
+
+# Save the trained models
+torch.save(G.state_dict(), 'G_infogan.pth')
+torch.save(D.state_dict(), 'D_infogan.pth')
+
+
    +
  • InfoGAN.py: Contains the implementation of the ACGAN model, training loop, and saving of trained models.
  • +
+
import torch
+import torch.nn as nn
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Device configuration
+device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+
+# Hyper-parameters
+latent_size = 62
+num_categories = 10
+num_continuous = 2
+image_size = 28 * 28
+
+# Generator
+class Generator(nn.Module):
+    def __init__(self):
+        super(Generator, self).__init__()
+        self.fc = nn.Sequential(
+            nn.Linear(latent_size + num_categories + num_continuous, 256),
+            nn.ReLU(),
+            nn.Linear(256, 512),
+            nn.ReLU(),
+            nn.Linear(512, 1024),
+            nn.ReLU(),
+            nn.Linear(1024, image_size),
+            nn.Tanh()
+        )
+
+    def forward(self, z, c_cat, c_cont):
+        inputs = torch.cat([z, c_cat, c_cont], dim=1)
+        return self.fc(inputs)
+
+# Load the trained generator model
+G = Generator().to(device)
+G.load_state_dict(torch.load('G_infogan.pth', map_location=torch.device('cpu')))
+G.eval()
+
+# Utility functions to generate samples
+def sample_noise(batch_size, latent_size):
+    return torch.randn(batch_size, latent_size).to(device)
+
+def sample_categorical(batch_size, num_categories):
+    return torch.randint(0, num_categories, (batch_size,)).to(device)
+
+def sample_continuous(batch_size, num_continuous):
+    return torch.rand(batch_size, num_continuous).to(device)
+
+def denorm(x):
+    out = (x + 1) / 2
+    return out.clamp(0, 1)
+
+# Generate images
+with torch.no_grad():
+    noise = sample_noise(64, latent_size)
+    c_cat = sample_categorical(64, num_categories)
+    c_cont = sample_continuous(64, num_continuous)
+    fake_images = G(noise, c_cat, c_cont)
+    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)
+    fake_images = denorm(fake_images)
+    grid = np.transpose(fake_images, (0, 2, 3, 1)).numpy()
+
+    plt.figure(figsize=(8, 8))
+    for i in range(grid.shape[0]):
+        plt.subplot(8, 8, i+1)
+        plt.imshow(grid[i, :, :, 0], cmap='gray')
+        plt.axis('off')
+    plt.show()
+
+
    +
  • test_InfoGAN.py: Uses the trained generator to generate sample images after training.
  • +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/index.html b/algorithms/index.html new file mode 100644 index 00000000..16c37a32 --- /dev/null +++ b/algorithms/index.html @@ -0,0 +1,631 @@ + + + + + + + + + + + + + + + + + + + + + + + + + 🔷 Algorithms - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/large-language-models/bert/index.html b/algorithms/large-language-models/bert/index.html new file mode 100644 index 00000000..2991a826 --- /dev/null +++ b/algorithms/large-language-models/bert/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + BERT 🧠 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

BERT 🧠

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/large-language-models/bloom/index.html b/algorithms/large-language-models/bloom/index.html new file mode 100644 index 00000000..45924cec --- /dev/null +++ b/algorithms/large-language-models/bloom/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Bloom 🧠 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Bloom 🧠

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/large-language-models/gpt-series/index.html b/algorithms/large-language-models/gpt-series/index.html new file mode 100644 index 00000000..1258f3ce --- /dev/null +++ b/algorithms/large-language-models/gpt-series/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + GPT Series 🧠 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

GPT Series 🧠

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/large-language-models/index.html b/algorithms/large-language-models/index.html new file mode 100644 index 00000000..7e2f4629 --- /dev/null +++ b/algorithms/large-language-models/index.html @@ -0,0 +1,535 @@ + + + + + + + + + + + + + + + + + + + + + Large Language Models 🧠 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/large-language-models/t5/index.html b/algorithms/large-language-models/t5/index.html new file mode 100644 index 00000000..341d4b27 --- /dev/null +++ b/algorithms/large-language-models/t5/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + T5 🧠 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

T5 🧠

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/boosting/index.html b/algorithms/machine-learning/boosting/index.html new file mode 100644 index 00000000..bc63a451 --- /dev/null +++ b/algorithms/machine-learning/boosting/index.html @@ -0,0 +1,501 @@ + + + + + + + + + + + + + + + + + + + + + Boosting 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/boosting/light-gbm/index.html b/algorithms/machine-learning/boosting/light-gbm/index.html new file mode 100644 index 00000000..5f699d70 --- /dev/null +++ b/algorithms/machine-learning/boosting/light-gbm/index.html @@ -0,0 +1,703 @@ + + + + + + + + + + + + + + + + + + + + + Light gbm - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Light gbm

+ +

LightGBM: A Comprehensive Guide to Scratch Implementation

+

Overview:
+LightGBM (Light Gradient Boosting Machine) is an advanced gradient boosting framework that efficiently handles large datasets. Unlike traditional boosting methods, LightGBM uses leaf-wise tree growth, which improves accuracy and reduces computation time.

+
+

Key Highlights:

+
    +
  • Speed and Efficiency: Faster training on large datasets compared to XGBoost.
  • +
  • Memory Optimization: Lower memory usage, making it scalable.
  • +
  • Built-in Handling of Categorical Data: No need for manual one-hot encoding.
  • +
  • Parallel and GPU Training: Supports multi-threading and GPU acceleration for faster computation.
  • +
+
+

How LightGBM Works (Scratch Implementation Guide):

+

1. Core Concept (Leaf-Wise Tree Growth):

+
    +
  • Level-wise (XGBoost): Grows all leaves at the same depth before moving to the next.
  • +
  • Leaf-wise (LightGBM): Grows the leaf that reduces the most loss, potentially leading to deeper, more accurate trees.
  • +
+

Example Visualization:
+

Level-wise (XGBoost)                Leaf-wise (LightGBM)
+        O                                 O
+       / \                               / \
+      O   O                             O   O
+     / \                                 \
+    O   O                                 O
+

+
+

Algorithm Breakdown:

+
    +
  1. Initialize Model: Start with a simple model (like mean predictions).
  2. +
  3. Compute Residuals: Calculate errors between actual and predicted values.
  4. +
  5. Train Trees to Predict Residuals: Fit new trees to minimize residuals.
  6. +
  7. Update Model: Adjust predictions by adding the new tree’s results.
  8. +
  9. Repeat Until Convergence or Early Stopping.
  10. +
+
+

Parameters Explained:

+
    +
  • num_leaves: Limits the number of leaves in a tree (complexity control).
  • +
  • max_depth: Constrains tree depth to prevent overfitting.
  • +
  • learning_rate: Scales the contribution of each tree to control convergence.
  • +
  • n_estimators: Number of boosting rounds (trees).
  • +
  • min_data_in_leaf: Minimum number of data points in a leaf to avoid overfitting small branches.
  • +
+
+

Scratch Code Example (From the Ground Up):

+

File: lightgbm_model.py
+

import lightgbm as lgb
+from sklearn.model_selection import train_test_split
+from sklearn.metrics import mean_squared_error
+
+class LightGBMModel:
+    def __init__(self, params=None):
+        self.params = params if params else {
+            'objective': 'regression',
+            'metric': 'rmse',
+            'boosting_type': 'gbdt',
+            'num_leaves': 31,
+            'learning_rate': 0.05,
+            'n_estimators': 100
+        }
+        self.model = None
+
+    def fit(self, X_train, y_train):
+        d_train = lgb.Dataset(X_train, label=y_train)
+        self.model = lgb.train(self.params, d_train)
+
+    def predict(self, X_test):
+        return self.model.predict(X_test)
+

+
+

Testing the Model:

+

File: lightgbm_model_test.py
+

import unittest
+import numpy as np
+from sklearn.datasets import load_diabetes
+from sklearn.model_selection import train_test_split
+from lightgbm_model import LightGBMModel
+
+class TestLightGBMModel(unittest.TestCase):
+
+    def test_lightgbm(self):
+        # Load Dataset
+        data = load_diabetes()
+        X_train, X_test, y_train, y_test = train_test_split(
+            data.data, data.target, test_size=0.2, random_state=42)
+
+        # Train Model
+        model = LightGBMModel()
+        model.fit(X_train, y_train)
+
+        # Predict and Evaluate
+        predictions = model.predict(X_test)
+        mse = mean_squared_error(y_test, predictions)
+        self.assertTrue(mse < 3500, "MSE is too high, LightGBM not performing well")
+
+if __name__ == '__main__':
+    unittest.main()
+

+
+

Additional Insights to Aid Understanding:

+
    +
  • Feature Importance:
    +
    lgb.plot_importance(model.model)
    +
  • +
  • Early Stopping Implementation:
    +
    self.model = lgb.train(self.params, d_train, valid_sets=[d_train], early_stopping_rounds=10)
    +
  • +
+
+

Testing and Validation:

+

Use sklearn datasets to validate the implementation. Compare performance with other boosting models to highlight LightGBM’s efficiency.

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/encoding/index.html b/algorithms/machine-learning/data-preprocessing/encoding/index.html new file mode 100644 index 00000000..4dba6ee5 --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/encoding/index.html @@ -0,0 +1,502 @@ + + + + + + + + + + + + + + + + + + + + + Encoding Algorithms 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/index.html b/algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/index.html new file mode 100644 index 00000000..be296e01 --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/index.html @@ -0,0 +1,669 @@ + + + + + + + + + + + + + + + + + + + + + ORDINAL ENCODER - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

ORDINAL ENCODER

+

A custom implementation of an OrdinalEncoder class for encoding categorical data into ordinal integers using a pandas DataFrame. The class maps each unique category to an integer based on the order of appearance.

+

Features

+
    +
  • fit: Learn the mapping of categories to ordinal integers for each column.
  • +
  • transform: Transform the categorical data to ordinal integers based on the learned mapping.
  • +
  • fit_transform: Fit the encoder and transform the data in one step.
  • +
+

Methods

+
    +
  1. __init__(self)
      +
    • Initializes the OrdinalEncoding class.
    • +
    • No parameters are required.
    • +
    +
  2. +
  3. fit(self, data)
      +
    • Learns the mapping of categories to ordinal integers for each column.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame): The data to fit.
      • +
      +
    • +
    • Raises:
        +
      • TypeError: If the input data is not a pandas DataFrame.
      • +
      +
    • +
    +
  4. +
  5. transform(self, data)
      +
    • Transforms the categorical data to ordinal integers based on the learned mapping.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame): The data to transform.
      • +
      +
    • +
    • Returns:
        +
      • pandas.DataFrame: The transformed data.
      • +
      +
    • +
    • Raises:
        +
      • Error: If transform is called before fit or fit_transform.
      • +
      +
    • +
    +
  6. +
  7. fit_transform(self, data)
      +
    • Fits the encoder to the data and transforms the data in one step.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame): The data to fit and transform.
      • +
      +
    • +
    • Returns:
        +
      • pandas.DataFrame: The transformed data.
      • +
      +
    • +
    +
  8. +
+

Error Handling

+
    +
  • Raises a TypeError if the input data is not a pandas DataFrame in the fit method.
  • +
  • Raises an error if transform is called before fit or fit_transform.
  • +
+

Use Case

+

use_case

+

Output

+

output

+
    +
  • ordinal_encoder.py file
  • +
+
import pandas as pd
+
+class OrdinalEncoding:
+    def __init__(self):
+        self.category_mapping = {}
+
+    def fit(self, data):
+        # Fit the encoder to the data (pandas DataFrame).
+        # type check
+        if not type(data)==pd.DataFrame:
+            raise f"Type of data should be Pandas.DataFrame; {type(data)} found"
+        for column in data.columns:
+            unique_categories = sorted(set(data[column]))
+            self.category_mapping[column] = {category: idx for idx, category in enumerate(unique_categories)}
+
+    def transform(self, data):
+        # Transform the data (pandas DataFrame) to ordinal integers.
+        # checking for empty mapping
+        if not self.category_mapping:
+            raise "Catrgorical Mapping not found. Call OrdinalExcoding.fit() method or call OrdinalEncoding.fit_transform() method"
+
+        data_transformed = data.copy()
+        for column in data.columns:
+            data_transformed[column] = data[column].map(self.category_mapping[column])
+        return data_transformed
+
+    def fit_transform(self, data):
+        # Fit the encoder and transform the data in one step.
+        self.fit(data)
+        return self.transform(data)
+
+
    +
  • test_ordinal_encoder.py file
  • +
+
import os
+import sys
+# for resolving any path conflict
+current = os.path.dirname(os.path.realpath("ordinal_encoder.py"))
+parent = os.path.dirname(current)
+sys.path.append(current)
+
+import pandas as pd
+
+from Ordinal_Encoder.ordinal_encoder import OrdinalEncoding
+
+# Example usage
+data = {
+    'Category1': ['low', 'medium', 'high', 'medium', 'low', 'high', 'medium'],
+    'Category2': ['A', 'B', 'A', 'B', 'A', 'B', 'A'],
+    'Category3': ['X', 'Y', 'X', 'Y', 'X', 'Y', 'X']
+}
+df = pd.DataFrame(data)
+
+encoder = OrdinalEncoding()
+encoded_df = encoder.fit_transform(df)
+
+print("Original DataFrame:")
+print(df)
+print("\nEncoded DataFrame:")
+print(encoded_df)
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/imputation/index.html b/algorithms/machine-learning/data-preprocessing/imputation/index.html new file mode 100644 index 00000000..c5cb25cc --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/imputation/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Imputation Algorithm 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Imputation Algorithm 🤖

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/index.html b/algorithms/machine-learning/data-preprocessing/index.html new file mode 100644 index 00000000..c3a745e3 --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/index.html @@ -0,0 +1,526 @@ + + + + + + + + + + + + + + + + + + + + + Data Pre-processing 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/index.html b/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/index.html new file mode 100644 index 00000000..40de407a --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/index.html @@ -0,0 +1,513 @@ + + + + + + + + + + + + + + + + + + + + + Scaling and Normalization 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/index.html b/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/index.html new file mode 100644 index 00000000..dc891204 --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/index.html @@ -0,0 +1,689 @@ + + + + + + + + + + + + + + + + + + + + + MIN MAX SCALER - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

MIN MAX SCALER

+

A custom implementation of a MinMaxScaler class for scaling numerical data in a pandas DataFrame. The class scales the features to a specified range, typically between 0 and 1.

+

Features

+
    +
  • fit: Calculate the minimum and maximum values of the data.
  • +
  • transform: Scale the data to the specified feature range.
  • +
  • fit_transform: Fit the scaler and transform the data in one step.
  • +
  • get_params: Retrieve the minimum and maximum values calculated during fitting.
  • +
+

Methods

+
    +
  1. __init__(self, feature_range=(0, 1))
      +
    • Initializes the MinMaxScaling class.
    • +
    • Parameters:
        +
      • feature_range (tuple): Desired range of transformed data. Default is (0, 1).
      • +
      +
    • +
    +
  2. +
  3. fit(self, data)
      +
    • Calculates the minimum and maximum values of the data.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame): The data to fit.
      • +
      +
    • +
    +
  4. +
  5. transform(self, data)
      +
    • Transforms the data to the specified feature range.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame): The data to transform.
      • +
      +
    • +
    • Returns:
        +
      • pandas.DataFrame: The scaled data.
      • +
      +
    • +
    +
  6. +
  7. fit_transform(self, data)
      +
    • Fits the scaler to the data and transforms the data in one step.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame): The data to fit and transform.
      • +
      +
    • +
    • Returns:
        +
      • pandas.DataFrame: The scaled data.
      • +
      +
    • +
    +
  8. +
  9. get_params(self)
      +
    • Retrieves the minimum and maximum values calculated during fitting.
    • +
    • Returns:
        +
      • dict: Dictionary containing the minimum and maximum values.
      • +
      +
    • +
    +
  10. +
+

Error Handling

+
    +
  • Raises a TypeError if the input data is not a pandas DataFrame in the fit method.
  • +
  • Raises an error if transform is called before fit or fit_transform.
  • +
  • Raises an error in get_params if called before fit.
  • +
+

Use Case

+

use_case

+

Output

+

output

+
    +
  • min_max_scaler.py file
  • +
+
import pandas as pd
+
+# Custom MinMaxScaler class
+class MinMaxScaling:
+    # init function
+    def __init__(self, feature_range=(0, 1)):  # feature range can be specified by the user else it takes (0,1)
+        self.min = feature_range[0]
+        self.max = feature_range[1]
+        self.data_min_ = None
+        self.data_max_ = None
+
+    # fit function to calculate min and max value of the data
+    def fit(self, data):
+        # type check
+        if not type(data)==pd.DataFrame:
+            raise f"TypeError : parameter should be a Pandas.DataFrame; {type(data)} found"
+        else:
+            self.data_min_ = data.min()
+            self.data_max_ = data.max()
+
+    # transform function
+    def transform(self, data):
+        if self.data_max_ is None or self.data_min_ is None:
+            raise "Call MinMaxScaling.fit() first or call MinMaxScaling.fit_transform() as the required params not found"
+        else:
+            data_scaled = (data - self.data_min_) / (self.data_max_ - self.data_min_)
+            data_scaled = data_scaled * (self.max - self.min) + self.min
+            return data_scaled
+
+    # fit_tranform function
+    def fit_transform(self, data):
+        self.fit(data)
+        return self.transform(data)
+
+    # get_params function
+    def get_params(self):
+        if self.data_max_ is None or self.data_min_ is None:
+            raise "Params not found! Call MinMaxScaling.fit() first"
+        else:
+            return {"Min" : self.data_min_,
+                    "Max" : self.data_max_}
+
+
    +
  • test_min_max_scaler.py file
  • +
+
import os
+import sys
+# for resolving any path conflict
+current = os.path.dirname(os.path.realpath("min_max_scaler.py"))
+parent = os.path.dirname(current)
+sys.path.append(current)
+
+import pandas as pd
+
+from Min_Max_Scaler.min_max_scaler import MinMaxScaling
+
+# Example DataFrame
+data = {
+    'A': [1, 2, 3, 4, 5],
+    'B': [10, 20, 30, 40, 50],
+    'C': [100, 200, 300, 400, 500]
+}
+
+df = pd.DataFrame(data)
+
+# Initialize the CustomMinMaxScaler
+scaler = MinMaxScaling()
+
+# Fit the scaler to the data and transform the data
+scaled_df = scaler.fit_transform(df)
+
+print("Original DataFrame:")
+print(df)
+print("\nScaled DataFrame:")
+print(scaled_df)
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/index.html b/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/index.html new file mode 100644 index 00000000..912369a7 --- /dev/null +++ b/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/index.html @@ -0,0 +1,700 @@ + + + + + + + + + + + + + + + + + + + + + STANDARD SCALER - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

STANDARD SCALER

+

A custom implementation of a StandardScaler class for scaling numerical data in a pandas DataFrame or NumPy array. The class scales the features to have zero mean and unit variance.

+

Features

+
    +
  • fit: Calculate the mean and standard deviation of the data.
  • +
  • transform: Scale the data to have zero mean and unit variance.
  • +
  • fit_transform: Fit the scaler and transform the data in one step.
  • +
  • get_params: Retrieve the mean and standard deviation calculated during fitting.
  • +
+

Methods

+
    +
  1. __init__(self)
      +
    • Initializes the StandardScaling class.
    • +
    • No parameters are required.
    • +
    +
  2. +
  3. fit(self, data)
      +
    • Calculates the mean and standard deviation of the data.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame or numpy.ndarray): The data to fit.
      • +
      +
    • +
    • Raises:
        +
      • TypeError: If the input data is not a pandas DataFrame or NumPy array.
      • +
      +
    • +
    +
  4. +
  5. transform(self, data)
      +
    • Transforms the data to have zero mean and unit variance.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame or numpy.ndarray): The data to transform.
      • +
      +
    • +
    • Returns:
        +
      • numpy.ndarray: The scaled data.
      • +
      +
    • +
    • Raises:
        +
      • Error: If transform is called before fit or fit_transform.
      • +
      +
    • +
    +
  6. +
  7. fit_transform(self, data)
      +
    • Fits the scaler to the data and transforms the data in one step.
    • +
    • Parameters:
        +
      • data (pandas.DataFrame or numpy.ndarray): The data to fit and transform.
      • +
      +
    • +
    • Returns:
        +
      • numpy.ndarray: The scaled data.
      • +
      +
    • +
    +
  8. +
  9. get_params(self)
      +
    • Retrieves the mean and standard deviation calculated during fitting.
    • +
    • Returns:
        +
      • dict: Dictionary containing the mean and standard deviation.
      • +
      +
    • +
    • Raises:
        +
      • Error: If get_params is called before fit.
      • +
      +
    • +
    +
  10. +
+

Error Handling

+
    +
  • Raises a TypeError if the input data is not a pandas DataFrame or NumPy array in the fit method.
  • +
  • Raises an error if transform is called before fit or fit_transform.
  • +
  • Raises an error in get_params if called before fit.
  • +
+

Use Case

+

use_case

+

Output

+

output

+
    +
  • standard_scaler.py file
  • +
+
import pandas as pd
+import numpy as np
+
+# Custom MinMaxScaler class
+class StandardScaling:
+    # init function
+    def __init__(self):     
+        self.data_mean_ = None
+        self.data_std_ = None
+
+    # fit function to calculate min and max value of the data
+    def fit(self, data):
+        # type check
+        if not (type(data)==pd.DataFrame or type(data)==np.ndarray):
+            raise f"TypeError : parameter should be a Pandas.DataFrame or Numpy.ndarray; {type(data)} found"
+        elif type(data)==pd.DataFrame:
+            data = data.to_numpy()
+
+        self.data_mean_ = np.mean(data, axis=0)
+        self.data_std_ = np.sqrt(np.var(data, axis=0))
+
+    # transform function
+    def transform(self, data):
+        if self.data_mean_ is None or self.data_std_ is None:
+            raise "Call StandardScaling.fit() first or call StandardScaling.fit_transform() as the required params not found"
+        else:
+            data_scaled = (data - self.data_mean_) / (self.data_std_)
+            return data_scaled
+
+    # fit_tranform function
+    def fit_transform(self, data):
+        self.fit(data)
+        return self.transform(data)
+
+    # get_params function
+    def get_params(self):
+        if self.data_mean_ is None or self.data_std_ is None:
+            raise "Params not found! Call StandardScaling.fit() first"
+        else:
+            return {"Mean" : self.data_mean_,
+                    "Standard Deviation" : self.data_std_}
+
+
    +
  • test_standard_scaler.py file
  • +
+
import os
+import sys
+# for resolving any path conflict
+current = os.path.dirname(os.path.realpath("standard_scaler.py"))
+parent = os.path.dirname(current)
+sys.path.append(current)
+
+import pandas as pd
+
+from Standard_Scaler.standard_scaler import StandardScaling
+
+# Example DataFrame
+data = {
+    'A': [1, 2, 3, 4, 5],
+    'B': [10, 20, 30, 40, 50],
+    'C': [100, 200, 300, 400, 500]
+}
+
+df = pd.DataFrame(data)
+
+# Initialize the CustomMinMaxScaler
+scaler = StandardScaling()
+
+# Fit the scaler to the data and transform the data
+scaled_df = scaler.fit_transform(df)
+
+print("Original DataFrame:")
+print(df)
+print("\nScaled DataFrame:")
+print(scaled_df)
+print("\nAssociated Parameters:")
+print(scaler.get_params())
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/index.html b/algorithms/machine-learning/index.html new file mode 100644 index 00000000..3279fced --- /dev/null +++ b/algorithms/machine-learning/index.html @@ -0,0 +1,535 @@ + + + + + + + + + + + + + + + + + + + + + Machine Learning 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/classifications/index.html b/algorithms/machine-learning/supervised/classifications/index.html new file mode 100644 index 00000000..d07e11b8 --- /dev/null +++ b/algorithms/machine-learning/supervised/classifications/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Classification Algorithms 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Classification Algorithms 🤖

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/index.html b/algorithms/machine-learning/supervised/index.html new file mode 100644 index 00000000..ab023457 --- /dev/null +++ b/algorithms/machine-learning/supervised/index.html @@ -0,0 +1,513 @@ + + + + + + + + + + + + + + + + + + + + + Supervised Machine Learning 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/index.html b/algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/index.html new file mode 100644 index 00000000..52f60b02 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/index.html @@ -0,0 +1,711 @@ + + + + + + + + + + + + + + + + + + + + + AdaBoost - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

AdaBoost

+

Overview:
+AdaBoost (Adaptive Boosting) is one of the most popular ensemble methods for boosting weak learners to create a strong learner. It works by combining multiple "weak" models, typically decision stumps, and focusing more on the errors from previous models. This iterative process improves accuracy and reduces bias.

+
+

Key Highlights:

+
    +
  • Boosting Concept: Builds an ensemble by sequentially focusing on harder-to-classify instances.
  • +
  • Adaptive Weighting: Misclassified instances get higher weights, and correctly classified instances get lower weights in subsequent rounds.
  • +
  • Simple and Effective: Often uses decision stumps (single-level decision trees) as base models.
  • +
  • Versatility: Applicable to both regression and classification problems.
  • +
+
+

How AdaBoost Works (Scratch Implementation Guide):

+

1. Core Concept (Error Weight Adjustment):

+
    +
  • Assigns equal weights to all data points initially.
  • +
  • In each iteration:
  • +
  • A weak model (e.g., a decision stump) is trained on the weighted dataset.
  • +
  • Misclassified points are assigned higher weights for the next iteration.
  • +
  • A final strong model is constructed by combining all weak models, weighted by their accuracy.
  • +
+

Visualization:
+

Iteration 1: Train weak model -> Update weights  
+Iteration 2: Train weak model -> Update weights  
+...  
+Final Model: Combine weak models with weighted contributions  
+

+
+

Algorithm Breakdown:

+
    +
  1. Initialize Weights: Assign equal weights to all instances.
  2. +
  3. Train a Weak Model: Use weighted data to train a weak learner.
  4. +
  5. Calculate Model Error: Compute the weighted error rate of the model.
  6. +
  7. Update Instance Weights: Increase weights for misclassified points and decrease weights for correctly classified points.
  8. +
  9. Update Model Weight: Calculate the model’s contribution based on its accuracy.
  10. +
  11. Repeat for a Set Number of Iterations or Until Convergence.
  12. +
+
+

Parameters Explained:

+
    +
  • n_estimators: Number of weak learners (iterations).
  • +
  • learning_rate: Shrinks the contribution of each weak learner to avoid overfitting.
  • +
  • base_estimator: The weak learner used (e.g., DecisionTreeRegressor or DecisionTreeClassifier).
  • +
+
+

Scratch Code Example (From the Ground Up):

+

File: adaboost_model.py
+

import numpy as np
+from sklearn.tree import DecisionTreeRegressor
+
+class AdaBoostRegressor:
+    def __init__(self, n_estimators=50, learning_rate=1.0):
+        self.n_estimators = n_estimators
+        self.learning_rate = learning_rate
+        self.models = []
+        self.model_weights = []
+
+    def fit(self, X, y):
+        n_samples = X.shape[0]
+        # Initialize weights
+        sample_weights = np.ones(n_samples) / n_samples
+
+        for _ in range(self.n_estimators):
+            # Train weak model
+            model = DecisionTreeRegressor(max_depth=1)
+            model.fit(X, y, sample_weight=sample_weights)
+            predictions = model.predict(X)
+
+            # Calculate weighted error
+            error = np.sum(sample_weights * (y != predictions)) / np.sum(sample_weights)
+            if error > 0.5:
+                break
+
+            # Calculate model weight
+            model_weight = self.learning_rate * np.log((1 - error) / error)
+
+            # Update sample weights
+            sample_weights *= np.exp(model_weight * (y != predictions))
+            sample_weights /= np.sum(sample_weights)
+
+            self.models.append(model)
+            self.model_weights.append(model_weight)
+
+    def predict(self, X):
+        # Combine predictions from all models
+        final_prediction = sum(weight * model.predict(X) for model, weight in zip(self.models, self.model_weights))
+        return np.sign(final_prediction)
+

+
+

Testing the Model:

+

File: adaboost_model_test.py
+

import unittest
+import numpy as np
+from sklearn.datasets import make_regression
+from sklearn.metrics import mean_squared_error
+from adaboost_model import AdaBoostRegressor
+
+class TestAdaBoostRegressor(unittest.TestCase):
+
+    def test_adaboost(self):
+        # Generate synthetic dataset
+        X, y = make_regression(n_samples=100, n_features=1, noise=15, random_state=42)
+        y = np.sign(y)  # Convert to classification-like regression
+
+        # Train AdaBoost Regressor
+        model = AdaBoostRegressor(n_estimators=10)
+        model.fit(X, y)
+
+        # Predict and Evaluate
+        predictions = model.predict(X)
+        mse = mean_squared_error(y, predictions)
+        self.assertTrue(mse < 0.5, "MSE is too high, AdaBoost not performing well")
+
+if __name__ == '__main__':
+    unittest.main()
+

+
+

Additional Insights to Aid Understanding:

+
    +
  • Feature Importance:
    +
    for i, model in enumerate(model.models):
    +    print(f"Model {i} weight: {model_weights[i]}")
    +
  • +
  • Early Stopping Implementation:
    +Use validation metrics to stop training if performance does not improve over several iterations.
  • +
+
+

Testing and Validation:

+

Use datasets from sklearn (e.g., make_regression) to validate the implementation. Compare AdaBoost with other boosting models like Gradient Boosting and LightGBM to analyze performance differences.

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/index.html new file mode 100644 index 00000000..04f276a4 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/index.html @@ -0,0 +1,714 @@ + + + + + + + + + + + + + + + + + + + + + Decision Tree Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Decision Tree Regression

+

This module contains an implementation of Decision Tree Regression, a versatile algorithm for predicting a continuous outcome based on input features.

+

Parameters

+
    +
  • max_depth: Maximum depth of the decision tree. Controls the complexity of the model.
  • +
+

Scratch Code

+
    +
  • decision_tree_regression.py file
  • +
+
import numpy as np
+
+class DecisionTreeRegression:
+
+    def __init__(self, max_depth=None):
+        """
+        Constructor for the DecisionTreeRegression class.
+
+        Parameters:
+        - max_depth: Maximum depth of the decision tree.
+        """
+        self.max_depth = max_depth
+        self.tree = None
+
+    def _calculate_variance(self, y):
+        """
+        Calculate the variance of a set of target values.
+
+        Parameters:
+        - y: Target values (numpy array).
+
+        Returns:
+        - Variance of the target values.
+        """
+        return np.var(y)
+
+    def _split_dataset(self, X, y, feature_index, threshold):
+        """
+        Split the dataset based on a feature and threshold.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        - feature_index: Index of the feature to split on.
+        - threshold: Threshold value for the split.
+
+        Returns:
+        - Left and right subsets of the dataset.
+        """
+        left_mask = X[:, feature_index] <= threshold
+        right_mask = ~left_mask
+        return X[left_mask], X[right_mask], y[left_mask], y[right_mask]
+
+    def _find_best_split(self, X, y):
+        """
+        Find the best split for the dataset.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+
+        Returns:
+        - Index of the best feature and the corresponding threshold.
+        """
+        m, n = X.shape
+        best_feature_index = None
+        best_threshold = None
+        best_variance_reduction = 0
+
+        initial_variance = self._calculate_variance(y)
+
+        for feature_index in range(n):
+            thresholds = np.unique(X[:, feature_index])
+
+            for threshold in thresholds:
+                # Split the dataset
+                _, _, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)
+
+                # Calculate variance reduction
+                left_weight = len(y_left) / m
+                right_weight = len(y_right) / m
+                variance_reduction = initial_variance - (left_weight * self._calculate_variance(y_left) + right_weight * self._calculate_variance(y_right))
+
+                # Update the best split if variance reduction is greater
+                if variance_reduction > best_variance_reduction:
+                    best_feature_index = feature_index
+                    best_threshold = threshold
+                    best_variance_reduction = variance_reduction
+
+        return best_feature_index, best_threshold
+
+    def _build_tree(self, X, y, depth):
+        """
+        Recursively build the decision tree.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        - depth: Current depth of the tree.
+
+        Returns:
+        - Node of the decision tree.
+        """
+        # Check if max depth is reached or if all target values are the same
+        if depth == self.max_depth or np.all(y == y[0]):
+            return {'value': np.mean(y)}
+
+        # Find the best split
+        feature_index, threshold = self._find_best_split(X, y)
+
+        if feature_index is not None:
+            # Split the dataset
+            X_left, X_right, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)
+
+            # Recursively build left and right subtrees
+            left_subtree = self._build_tree(X_left, y_left, depth + 1)
+            right_subtree = self._build_tree(X_right, y_right, depth + 1)
+
+            return {'feature_index': feature_index,
+                    'threshold': threshold,
+                    'left': left_subtree,
+                    'right': right_subtree}
+        else:
+            # If no split is found, return a leaf node
+            return {'value': np.mean(y)}
+
+    def fit(self, X, y):
+        """
+        Fit the Decision Tree Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        self.tree = self._build_tree(X, y, depth=0)
+
+    def _predict_single(self, node, x):
+        """
+        Recursively predict a single data point.
+
+        Parameters:
+        - node: Current node in the decision tree.
+        - x: Input features for prediction.
+
+        Returns:
+        - Predicted value.
+        """
+        if 'value' in node:
+            return node['value']
+        else:
+            if x[node['feature_index']] <= node['threshold']:
+                return self._predict_single(node['left'], x)
+            else:
+                return self._predict_single(node['right'], x)
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        return np.array([self._predict_single(self.tree, x) for x in X])
+
+
    +
  • decision_tree_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from DecisionTreeRegressor import DecisionTreeRegression
+
+class TestDecisionTreeRegressor(unittest.TestCase):
+
+    def setUp(self):
+        # Create sample data for testing
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 2)
+        self.y_train = 2 * self.X_train[:, 0] + 3 * self.X_train[:, 1] + np.random.normal(0, 0.1, 100)
+
+        self.X_test = np.random.rand(10, 2)
+
+    def test_fit_predict(self):
+        # Test if the model can be fitted and predictions are made
+        dt_model = DecisionTreeRegression(max_depth=3)
+        dt_model.fit(self.X_train, self.y_train)
+
+        # Ensure predictions are made without errors
+        predictions = dt_model.predict(self.X_test)
+
+        # Add your specific assertions based on the expected behavior of your model
+        self.assertIsInstance(predictions, np.ndarray)
+        self.assertEqual(predictions.shape, (10,))
+
+    # Add more test cases as needed
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/index.html new file mode 100644 index 00000000..da2cefb6 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/index.html @@ -0,0 +1,601 @@ + + + + + + + + + + + + + + + + + + + + + Elastic Net Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Elastic Net Regression

+

This module contains an implementation of Elastic Net Regression, a powerful linear regression technique that combines both L1 (Lasso) and L2 (Ridge) regularization. Elastic Net is particularly useful when dealing with high-dimensional datasets and can effectively handle correlated features.

+

Parameters

+
    +
  • alpha: The regularization strength. A positive float value.
  • +
  • l1_ratio: The ratio of L1 regularization to L2 regularization. Should be between 0 and 1.
  • +
  • max_iter: The maximum number of iterations to run the optimization algorithm.
  • +
  • tol: The tolerance for the optimization. If the updates are smaller than this value, the optimization will stop.
  • +
+

Scratch Code

+
    +
  • elastic_net_regression.py file
  • +
+
import numpy as np
+
+class ElasticNetRegression:
+    def __init__(self, alpha=1.0, l1_ratio=0.5, max_iter=1000, tol=1e-4):
+        self.alpha = alpha
+        self.l1_ratio = l1_ratio
+        self.max_iter = max_iter
+        self.tol = tol
+        self.coef_ = None
+        self.intercept_ = None
+
+    def fit(self, X, y):
+        n_samples, n_features = X.shape
+        self.coef_ = np.zeros(n_features)
+        self.intercept_ = 0
+        learning_rate = 0.01  
+
+        for iteration in range(self.max_iter):
+            y_pred = np.dot(X, self.coef_) + self.intercept_
+            error = y - y_pred
+
+            gradient_w = (-2 / n_samples) * (X.T.dot(error)) + self.alpha * (self.l1_ratio * np.sign(self.coef_) + (1 - self.l1_ratio) * 2 * self.coef_)
+            gradient_b = (-2 / n_samples) * np.sum(error)
+
+            new_coef = self.coef_ - learning_rate * gradient_w
+            new_intercept = self.intercept_ - learning_rate * gradient_b
+
+            if np.all(np.abs(new_coef - self.coef_) < self.tol) and np.abs(new_intercept - self.intercept_) < self.tol:
+                break
+
+            self.coef_ = new_coef
+            self.intercept_ = new_intercept
+
+    def predict(self, X):
+        return np.dot(X, self.coef_) + self.intercept_
+
+
    +
  • elastic_net_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from sklearn.linear_model import ElasticNet
+from ElasticNetRegression import ElasticNetRegression
+
+class TestElasticNetRegression(unittest.TestCase):
+
+    def test_elastic_net_regression(self):
+        np.random.seed(42)
+        X_train = np.random.rand(100, 1) * 10
+        y_train = 2 * X_train.squeeze() + np.random.randn(100) * 2 
+
+        X_test = np.array([[2.5], [5.0], [7.5]])
+
+        custom_model = ElasticNetRegression(alpha=1.0, l1_ratio=0.5)
+        custom_model.fit(X_train, y_train)
+        custom_predictions = custom_model.predict(X_test)
+
+        sklearn_model = ElasticNet(alpha=1.0, l1_ratio=0.5, max_iter=1000, tol=1e-4)
+        sklearn_model.fit(X_train, y_train)
+        sklearn_predictions = sklearn_model.predict(X_test)
+
+        np.testing.assert_allclose(custom_predictions, sklearn_predictions, rtol=1e-1)
+
+        train_predictions_custom = custom_model.predict(X_train)
+        train_predictions_sklearn = sklearn_model.predict(X_train)
+
+        custom_mse = np.mean((y_train - train_predictions_custom) ** 2)
+        sklearn_mse = np.mean((y_train - train_predictions_sklearn) ** 2)
+
+        print(f"Custom Model MSE: {custom_mse}")
+        print(f"Scikit-learn Model MSE: {sklearn_mse}")
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/index.html new file mode 100644 index 00000000..28139fc6 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/index.html @@ -0,0 +1,727 @@ + + + + + + + + + + + + + + + + + + + + + Gradient Boosting Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Gradient Boosting Regression

+

This module contains an implementation of Gradient Boosting Regression, an ensemble learning method that combines multiple weak learners (typically decision trees) to create a more robust and accurate model for predicting continuous outcomes based on input features.

+

Parameters

+
    +
  • n_estimators: Number of boosting stages (trees) to be run.
  • +
  • learning_rate: Step size shrinkage to prevent overfitting.
  • +
  • max_depth: Maximum depth of each decision tree.
  • +
+

Scratch Code

+
    +
  • gradient_boosting_regression.py file
  • +
+
import numpy as np
+
+class GradientBoostingRegression:
+    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
+        """
+        Constructor for the GradientBoostingRegression class.
+
+        Parameters:
+        - n_estimators: Number of trees in the ensemble.
+        - learning_rate: Step size for each tree's contribution.
+        - max_depth: Maximum depth of each decision tree.
+        """
+        self.n_estimators = n_estimators
+        self.learning_rate = learning_rate
+        self.max_depth = max_depth
+        self.trees = []
+
+    def fit(self, X, y):
+        """
+        Fit the gradient boosting regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        # Initialize predictions with the mean of the target values
+        predictions = np.mean(y) * np.ones_like(y)
+
+        for _ in range(self.n_estimators):
+            # Compute residuals
+            residuals = y - predictions
+
+            # Fit a decision tree to the residuals
+            tree = self._fit_tree(X, residuals, depth=0)
+
+            # Update predictions using the tree's contribution scaled by the learning rate
+            predictions += self.learning_rate * self._predict_tree(X, tree)
+
+            # Save the tree in the ensemble
+            self.trees.append(tree)
+
+    def _fit_tree(self, X, y, depth):
+        """
+        Fit a decision tree to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        - depth: Current depth of the tree.
+
+        Returns:
+        - Tree structure (dictionary).
+        """
+        if depth == self.max_depth:
+            # If the maximum depth is reached, return the mean of the target values
+            return np.mean(y)
+
+        # Find the best split point
+        feature_index, threshold = self._find_best_split(X, y)
+
+        if feature_index is None:
+            # If no split improves the purity, return the mean of the target values
+            return np.mean(y)
+
+        # Split the data
+        mask = X[:, feature_index] < threshold
+        left_tree = self._fit_tree(X[mask], y[mask], depth + 1)
+        right_tree = self._fit_tree(X[~mask], y[~mask], depth + 1)
+
+        # Return the tree structure
+        return {'feature_index': feature_index, 'threshold': threshold,
+                'left_tree': left_tree, 'right_tree': right_tree}
+
+    def _find_best_split(self, X, y):
+        """
+        Find the best split point for a decision tree.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+
+        Returns:
+        - Best feature index and threshold for the split.
+        """
+        m, n = X.shape
+        if m <= 1:
+            return None, None  # No split is possible
+
+        # Calculate the initial impurity
+        initial_impurity = self._calculate_impurity(y)
+
+        # Initialize variables to store the best split parameters
+        best_feature_index, best_threshold, best_impurity_reduction = None, None, 0
+
+        for feature_index in range(n):
+            # Sort the feature values and corresponding target values
+            sorted_indices = np.argsort(X[:, feature_index])
+            sorted_X = X[sorted_indices, feature_index]
+            sorted_y = y[sorted_indices]
+
+            # Initialize variables to keep track of impurity and counts for the left and right nodes
+            left_impurity, left_count = 0, 0
+            right_impurity, right_count = initial_impurity, m
+
+            for i in range(1, m):
+                # Update impurity and counts for the left and right nodes
+                value = sorted_X[i]
+                left_impurity += (i / m) * self._calculate_impurity(sorted_y[i-1:i+1])
+                left_count += 1
+                right_impurity -= ((i-1) / m) * self._calculate_impurity(sorted_y[i-1:i+1])
+                right_count -= 1
+
+                # Calculate impurity reduction
+                impurity_reduction = initial_impurity - (left_count / m * left_impurity + right_count / m * right_impurity)
+
+                # Check if this is the best split so far
+                if impurity_reduction > best_impurity_reduction:
+                    best_feature_index = feature_index
+                    best_threshold = value
+                    best_impurity_reduction = impurity_reduction
+
+        return best_feature_index, best_threshold
+
+    def _calculate_impurity(self, y):
+        """
+        Calculate the impurity of a node.
+
+        Parameters:
+        - y: Target values (numpy array).
+
+        Returns:
+        - Impurity.
+        """
+        # For regression, impurity is the variance of the target values
+        return np.var(y)
+
+    def _predict_tree(self, X, tree):
+        """
+        Make predictions using a decision tree.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - tree: Tree structure (dictionary).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        if 'feature_index' not in tree:
+            # If the node is a leaf, return the constant value
+            return tree
+        else:
+            # Recursively traverse the tree
+            mask = X[:, tree['feature_index']] < tree['threshold']
+            return np.where(mask, self._predict_tree(X, tree['left_tree']), self._predict_tree(X, tree['right_tree']))
+
+    def predict(self, X):
+        """
+        Make predictions on new data using the Gradient Boosting Regression.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        predictions = np.sum(self.learning_rate * self._predict_tree(X, tree) for tree in self.trees)
+        return predictions
+
+
    +
  • gradient_boosting_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from GradientBoostingRegressor import GradientBoostingRegression
+
+class TestGradientBoostingRegressor(unittest.TestCase):
+
+    def setUp(self):
+        # Create sample data for testing
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 2)
+        self.y_train = 2 * self.X_train[:, 0] + 3 * self.X_train[:, 1] + np.random.normal(0, 0.1, 100)
+
+        self.X_test = np.random.rand(10, 2)
+
+    def test_fit_predict(self):
+        # Test if the model can be fitted and predictions are made
+        gbr_model = GradientBoostingRegression(n_estimators=5, learning_rate=0.1, max_depth=3)
+        gbr_model.fit(self.X_train, self.y_train)
+
+        # Ensure predictions are made without errors
+        predictions = gbr_model.predict(self.X_test)
+
+        # Add your specific assertions based on the expected behavior of your model
+        self.assertIsInstance(predictions, np.ndarray)
+        self.assertEqual(predictions.shape, (10,))
+
+    # Add more test cases as needed
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Huber_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Huber_Regression/index.html new file mode 100644 index 00000000..29c8688f --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Huber_Regression/index.html @@ -0,0 +1,614 @@ + + + + + + + + + + + + + + + + + + + + + Huber Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Huber Regression

+

This module contains an implementation of Huber Regression, a robust linear regression technique that combines the properties of both least squares and absolute error loss functions. Huber Regression is particularly useful when dealing with datasets that have outliers, as it is less sensitive to outliers compared to standard linear regression.

+

Overview

+

Huber Regression is a regression algorithm that adds a penalty based on the Huber loss function. This loss function is quadratic for small errors and linear for large errors, providing robustness against outliers.

+

Parameters

+
    +
  • alpha: The regularization strength. A positive float value.
  • +
  • epsilon: The threshold for the Huber loss function. A positive float value.
  • +
  • max_iter: The maximum number of iterations to run the optimization algorithm.
  • +
  • tol: The tolerance for the optimization. If the updates are smaller than this value, the optimization will stop.
  • +
+

Scratch Code

+
    +
  • huber_regression.py file
  • +
+
import numpy as np
+
+class HuberRegression:
+    def __init__(self, alpha=1.0, epsilon=1.35, max_iter=1000, tol=1e-4):
+        self.alpha = alpha
+        self.epsilon = epsilon
+        self.max_iter = max_iter
+        self.tol = tol
+        self.coef_ = None
+        self.intercept_ = None
+
+    def fit(self, X, y):
+        n_samples, n_features = X.shape
+        self.coef_ = np.zeros(n_features)
+        self.intercept_ = 0
+        learning_rate = 0.01
+
+        for iteration in range(self.max_iter):
+            y_pred = np.dot(X, self.coef_) + self.intercept_
+            error = y - y_pred
+
+            # Compute Huber gradient
+            mask = np.abs(error) <= self.epsilon
+            gradient_w = (-2 / n_samples) * (X.T.dot(error * mask) + self.epsilon * np.sign(error) * (~mask)) + self.alpha * self.coef_
+            gradient_b = (-2 / n_samples) * (np.sum(error * mask) + self.epsilon * np.sign(error) * (~mask))
+
+            new_coef = self.coef_ - learning_rate * gradient_w
+            new_intercept = self.intercept_ - learning_rate * gradient_b
+
+            if np.all(np.abs(new_coef - self.coef_) < self.tol) and np.abs(new_intercept - self.intercept_) < self.tol:
+                break
+
+            self.coef_ = new_coef
+            self.intercept_ = new_intercept
+
+    def predict(self, X):
+        return np.dot(X, self.coef_) + self.intercept_
+
+
    +
  • huber_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from sklearn.linear_model import HuberRegressor
+from HuberRegression import HuberRegression
+
+class TestHuberRegression(unittest.TestCase):
+
+    def test_huber_regression(self):
+        np.random.seed(42)
+        X_train = np.random.rand(100, 1) * 10
+        y_train = 2 * X_train.squeeze() + np.random.randn(100) * 2
+
+        X_test = np.array([[2.5], [5.0], [7.5]])
+
+        huber_model = HuberRegression(alpha=1.0, epsilon=1.35)
+        huber_model.fit(X_train, y_train)
+        huber_predictions = huber_model.predict(X_test)
+
+        sklearn_model = HuberRegressor(alpha=1.0, epsilon=1.35, max_iter=1000, tol=1e-4)
+        sklearn_model.fit(X_train, y_train)
+        sklearn_predictions = sklearn_model.predict(X_test)
+
+        np.testing.assert_allclose(huber_predictions, sklearn_predictions, rtol=1e-1)
+
+        train_predictions_huber = huber_model.predict(X_train)
+        train_predictions_sklearn = sklearn_model.predict(X_train)
+
+        huber_mse = np.mean((y_train - train_predictions_huber) ** 2)
+        sklearn_mse = np.mean((y_train - train_predictions_sklearn) ** 2)
+
+        print(f"Huber Model MSE: {huber_mse}")
+        print(f"Scikit-learn Model MSE: {sklearn_mse}")
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/index.html b/algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/index.html new file mode 100644 index 00000000..0705aebd --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/index.html @@ -0,0 +1,603 @@ + + + + + + + + + + + + + + + + + + + + + K Nearest Neighbors Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

K Nearest Neighbors Regression

+

This module contains an implementation of K-Nearest Neighbors Regression, a simple yet effective algorithm for predicting continuous outcomes based on input features.

+

Parameters

+
    +
  • k: Number of neighbors to consider for prediction.
  • +
+

Scratch Code

+
    +
  • k_nearest_neighbors_regression.py file
  • +
+
import numpy as np
+
+class KNNRegression:
+    def __init__(self, k=5):
+        """
+        Constructor for the KNNRegression class.
+
+        Parameters:
+        - k: Number of neighbors to consider.
+        """
+        self.k = k
+        self.X_train = None
+        self.y_train = None
+
+    def fit(self, X, y):
+        """
+        Fit the KNN model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        self.X_train = X
+        self.y_train = y
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        predictions = []
+        for x in X:
+            # Calculate Euclidean distances between the input point and all training points
+            distances = np.linalg.norm(self.X_train - x, axis=1)
+
+            # Get indices of k-nearest neighbors
+            indices = np.argsort(distances)[:self.k]
+
+            # Average the target values of k-nearest neighbors
+            predicted_value = np.mean(self.y_train[indices])
+            predictions.append(predicted_value)
+
+        return np.array(predictions)
+
+
    +
  • k_nearest_neighbors_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from KNearestNeighborsRegression import KNNRegression
+
+class TestKNNRegression(unittest.TestCase):
+
+    def test_knn_regression(self):
+        # Create synthetic data
+        np.random.seed(42)
+        X_train = np.random.rand(100, 1) * 10
+        y_train = 2 * X_train.squeeze() + np.random.randn(100) * 2  # Linear relationship with noise
+
+        X_test = np.array([[2.5], [5.0], [7.5]])
+
+        # Initialize and fit the KNN Regression model
+        knn_model = KNNRegression(k=3)
+        knn_model.fit(X_train, y_train)
+
+        # Test predictions
+        predictions = knn_model.predict(X_test)
+        expected_predictions = [2 * 2.5, 2 * 5.0, 2 * 7.5]  # Assuming a linear relationship
+
+        # Check if predictions are close to the expected values
+        np.testing.assert_allclose(predictions, expected_predictions, rtol=1e-5)
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Lasso_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Lasso_Regression/index.html new file mode 100644 index 00000000..e92e5d56 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Lasso_Regression/index.html @@ -0,0 +1,616 @@ + + + + + + + + + + + + + + + + + + + + + Lasso Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Lasso Regression

+

This module contains an implementation of Lasso Regression, a linear regression technique with L1 regularization.

+

Overview

+

Lasso Regression is a regression algorithm that adds a penalty term based on the absolute values of the coefficients. This penalty term helps in feature selection by driving some of the coefficients to exactly zero, effectively ignoring certain features.

+

Parameters

+
    +
  • learning_rate: The step size for gradient descent.
  • +
  • lambda_param: Regularization strength (L1 penalty).
  • +
  • n_iterations: The number of iterations for gradient descent.
  • +
+

Scratch Code

+
    +
  • lasso_regression.py file
  • +
+
import numpy as np
+
+class LassoRegression:
+    def __init__(self, learning_rate=0.01, lambda_param=0.01, n_iterations=1000):
+        """
+        Constructor for the LassoRegression class.
+
+        Parameters:
+        - learning_rate: The step size for gradient descent.
+        - lambda_param: Regularization strength.
+        - n_iterations: The number of iterations for gradient descent.
+        """
+        self.learning_rate = learning_rate
+        self.lambda_param = lambda_param
+        self.n_iterations = n_iterations
+        self.weights = None
+        self.bias = None
+
+    def fit(self, X, y):
+        """
+        Fit the Lasso Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        # Initialize weights and bias
+        num_samples, num_features = X.shape
+        self.weights = np.zeros(num_features)
+        self.bias = 0
+
+        # Perform gradient descent
+        for _ in range(self.n_iterations):
+            predictions = np.dot(X, self.weights) + self.bias
+            errors = y - predictions
+
+            # Update weights and bias
+            self.weights += self.learning_rate * (1/num_samples) * (np.dot(X.T, errors) - self.lambda_param * np.sign(self.weights))
+            self.bias += self.learning_rate * (1/num_samples) * np.sum(errors)
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        return np.dot(X, self.weights) + self.bias
+
+
    +
  • lasso_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from LassoRegression import LassoRegression
+
+class TestLassoRegression(unittest.TestCase):
+    def setUp(self):
+        # Create a sample dataset
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 2)
+        self.y_train = 3 * self.X_train[:, 0] + 2 * self.X_train[:, 1] + np.random.randn(100)
+
+        self.X_test = np.random.rand(10, 2)
+
+    def test_fit_predict(self):
+        # Test the fit and predict methods
+        model = LassoRegression(learning_rate=0.01, lambda_param=0.1, n_iterations=1000)
+        model.fit(self.X_train, self.y_train)
+        predictions = model.predict(self.X_test)
+
+        # Ensure predictions are of the correct shape
+        self.assertEqual(predictions.shape, (10,))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Logistic_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Logistic_Regression/index.html new file mode 100644 index 00000000..a94eb7e5 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Logistic_Regression/index.html @@ -0,0 +1,633 @@ + + + + + + + + + + + + + + + + + + + + + Logistic Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Logistic Regression

+

This module contains an implementation of Logistic Regression, a popular algorithm for binary classification.

+

Parameters

+
    +
  • learning_rate: Step size for gradient descent.
  • +
  • n_iterations: Number of iterations for gradient descent.
  • +
+

Scratch Code

+
    +
  • logistic_regression.py file
  • +
+
import numpy as np
+
+class LogisticRegression:
+    def __init__(self, learning_rate=0.01, n_iterations=1000):
+        """
+        Constructor for the LogisticRegression class.
+
+        Parameters:
+        - learning_rate: The step size for gradient descent.
+        - n_iterations: The number of iterations for gradient descent.
+        """
+        self.learning_rate = learning_rate
+        self.n_iterations = n_iterations
+        self.weights = None
+        self.bias = None
+
+    def _sigmoid(self, z):
+        """
+        Sigmoid activation function.
+
+        Parameters:
+        - z: Linear combination of input features and weights.
+
+        Returns:
+        - Sigmoid of z.
+        """
+        return 1 / (1 + np.exp(-z))
+
+    def _initialize_parameters(self, n_features):
+        """
+        Initialize weights and bias.
+
+        Parameters:
+        - n_features: Number of input features.
+
+        Returns:
+        - Initialized weights and bias.
+        """
+        self.weights = np.zeros(n_features)
+        self.bias = 0
+
+    def fit(self, X, y):
+        """
+        Fit the Logistic Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target labels (numpy array).
+        """
+        n_samples, n_features = X.shape
+        self._initialize_parameters(n_features)
+
+        for _ in range(self.n_iterations):
+            # Linear combination of features and weights
+            linear_combination = np.dot(X, self.weights) + self.bias
+
+            # Predictions using the sigmoid function
+            predictions = self._sigmoid(linear_combination)
+
+            # Update weights and bias using gradient descent
+            dw = (1 / n_samples) * np.dot(X.T, (predictions - y))
+            db = (1 / n_samples) * np.sum(predictions - y)
+
+            self.weights -= self.learning_rate * dw
+            self.bias -= self.learning_rate * db
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted labels (numpy array).
+        """
+        linear_combination = np.dot(X, self.weights) + self.bias
+        predictions = self._sigmoid(linear_combination)
+
+        # Convert probabilities to binary predictions (0 or 1)
+        return np.round(predictions)
+
+
    +
  • logistic_regression_test.py file
  • +
+
import numpy as np
+import unittest
+from LogisticRegression import LogisticRegression
+
+class TestLogisticRegression(unittest.TestCase):
+    def setUp(self):
+        # Generate synthetic data for testing
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 2)
+        self.y_train = (np.random.rand(100) > 0.5).astype(int)
+
+        self.X_test = np.random.rand(20, 2)
+
+    def test_fit_predict(self):
+        model = LogisticRegression(learning_rate=0.01, n_iterations=1000)
+        model.fit(self.X_train, self.y_train)
+        predictions = model.predict(self.X_test)
+
+        self.assertEqual(predictions.shape, (20,))
+        self.assertTrue(np.all(predictions == 0) or np.all(predictions == 1))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/index.html new file mode 100644 index 00000000..ab00528c --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/index.html @@ -0,0 +1,637 @@ + + + + + + + + + + + + + + + + + + + + + Neural Network Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Neural Network Regression

+

This module contains an implementation of Neural Network Regression, a powerful algorithm for predicting continuous outcomes based on input features.

+

Parameters

+
    +
  • input_size: Number of features in the input data.
  • +
  • hidden_size: Number of neurons in the hidden layer.
  • +
  • output_size: Number of output neurons.
  • +
  • learning_rate: Step size for updating weights during training.
  • +
  • n_iterations: Number of iterations for training the neural network.
  • +
+

Scratch Code

+
    +
  • neural_network_regression.py file
  • +
+
import numpy as np
+
+class NeuralNetworkRegression:
+    def __init__(self, input_size, hidden_size, output_size, learning_rate=0.01, n_iterations=1000):
+        """
+        Constructor for the NeuralNetworkRegression class.
+
+        Parameters:
+        - input_size: Number of input features.
+        - hidden_size: Number of neurons in the hidden layer.
+        - output_size: Number of output neurons.
+        - learning_rate: Step size for gradient descent.
+        - n_iterations: Number of iterations for gradient descent.
+        """
+        self.input_size = input_size
+        self.hidden_size = hidden_size
+        self.output_size = output_size
+        self.learning_rate = learning_rate
+        self.n_iterations = n_iterations
+
+        # Initialize weights and biases
+        self.weights_input_hidden = np.random.rand(self.input_size, self.hidden_size)
+        self.bias_hidden = np.zeros((1, self.hidden_size))
+        self.weights_hidden_output = np.random.rand(self.hidden_size, self.output_size)
+        self.bias_output = np.zeros((1, self.output_size))
+
+    def sigmoid(self, x):
+        """Sigmoid activation function."""
+        return 1 / (1 + np.exp(-x))
+
+    def sigmoid_derivative(self, x):
+        """Derivative of the sigmoid function."""
+        return x * (1 - x)
+
+    def fit(self, X, y):
+        """
+        Fit the Neural Network model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        for _ in range(self.n_iterations):
+            # Forward pass
+            hidden_layer_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
+            hidden_layer_output = self.sigmoid(hidden_layer_input)
+
+            output_layer_input = np.dot(hidden_layer_output, self.weights_hidden_output) + self.bias_output
+            predicted_output = self.sigmoid(output_layer_input)
+
+            # Backpropagation
+            error = y - predicted_output
+            output_delta = error * self.sigmoid_derivative(predicted_output)
+
+            hidden_layer_error = output_delta.dot(self.weights_hidden_output.T)
+            hidden_layer_delta = hidden_layer_error * self.sigmoid_derivative(hidden_layer_output)
+
+            # Update weights and biases
+            self.weights_hidden_output += hidden_layer_output.T.dot(output_delta) * self.learning_rate
+            self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * self.learning_rate
+
+            self.weights_input_hidden += X.T.dot(hidden_layer_delta) * self.learning_rate
+            self.bias_hidden += np.sum(hidden_layer_delta, axis=0, keepdims=True) * self.learning_rate
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        hidden_layer_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
+        hidden_layer_output = self.sigmoid(hidden_layer_input)
+
+        output_layer_input = np.dot(hidden_layer_output, self.weights_hidden_output) + self.bias_output
+        predicted_output = self.sigmoid(output_layer_input)
+
+        return predicted_output
+
+
    +
  • neural_network_regression_test.py file
  • +
+
import numpy as np
+import unittest
+from NeuralNetworkRegression import NeuralNetworkRegression
+
+class TestNeuralNetworkRegression(unittest.TestCase):
+    def setUp(self):
+        # Generate synthetic data for testing
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 3)
+        self.y_train = np.random.rand(100, 1)
+
+        self.X_test = np.random.rand(10, 3)
+
+    def test_fit_predict(self):
+        # Initialize and fit the model
+        model = NeuralNetworkRegression(input_size=3, hidden_size=4, output_size=1, learning_rate=0.01, n_iterations=1000)
+        model.fit(self.X_train, self.y_train)
+
+        # Ensure predictions have the correct shape
+        predictions = model.predict(self.X_test)
+        self.assertEqual(predictions.shape, (10, 1))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Polynomial_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Polynomial_Regression/index.html new file mode 100644 index 00000000..604097fd --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Polynomial_Regression/index.html @@ -0,0 +1,623 @@ + + + + + + + + + + + + + + + + + + + + + Polynomial Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Polynomial Regression

+

This module contains an implementation of Polynomial Regression, an extension of Linear Regression that models the relationship between the independent variable and the dependent variable as a polynomial.

+

Parameters

+
    +
  • degree: Degree of the polynomial.
  • +
  • learning_rate: The step size for gradient descent.
  • +
  • n_iterations: The number of iterations for gradient descent.
  • +
+

Scratch Code

+
    +
  • polynomial_regression.py file
  • +
+
import numpy as np
+
+# Polynomial regression implementation
+class PolynomialRegression:
+    def __init__(self, degree=2, learning_rate=0.01, n_iterations=1000):
+        """
+        Constructor for the PolynomialRegression class.
+
+        Parameters:
+        - degree: Degree of the polynomial.
+        - learning_rate: The step size for gradient descent.
+        - n_iterations: The number of iterations for gradient descent.
+        """
+        self.degree = degree
+        self.learning_rate = learning_rate
+        self.n_iterations = n_iterations
+        self.weights = None
+        self.bias = None
+
+    def _polynomial_features(self, X):
+        """
+        Create polynomial features up to the specified degree.
+
+        Parameters:
+        - X: Input features (numpy array).
+
+        Returns:
+        - Polynomial features (numpy array).
+        """
+        return np.column_stack([X ** i for i in range(1, self.degree + 1)])
+
+    def fit(self, X, y):
+        """
+        Fit the polynomial regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        X_poly = self._polynomial_features(X)
+        self.weights = np.zeros((X_poly.shape[1], 1))
+        self.bias = 0
+
+        for _ in range(self.n_iterations):
+            predictions = np.dot(X_poly, self.weights) + self.bias
+            errors = predictions - y
+
+            self.weights -= self.learning_rate * (1 / len(X_poly)) * np.dot(X_poly.T, errors)
+            self.bias -= self.learning_rate * (1 / len(X_poly)) * np.sum(errors)
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        X_poly = self._polynomial_features(X)
+        return np.dot(X_poly, self.weights) + self.bias
+
+
    +
  • polynomial_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from PolynomialRegression import PolynomialFeatures
+
+class TestPolynomialRegression(unittest.TestCase):
+
+    def setUp(self):
+        # Create synthetic data for testing
+        np.random.seed(42)
+        self.X_train = 2 * np.random.rand(100, 1)
+        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)
+
+    def test_fit_predict(self):
+        # Test the fit and predict methods
+        poly_model = PolynomialFeatures(degree=2)
+        poly_model.fit(self.X_train, self.y_train)
+
+        # Create test data
+        X_test = np.array([[1.5], [2.0]])
+
+        # Make predictions
+        predictions = poly_model.predict(X_test)
+
+        # Assert that the predictions are NumPy arrays
+        self.assertTrue(isinstance(predictions, np.ndarray))
+
+        # Assert that the shape of predictions is as expected
+        self.assertEqual(predictions.shape, (X_test.shape[0], 1))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/index.html new file mode 100644 index 00000000..ecc82c13 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/index.html @@ -0,0 +1,753 @@ + + + + + + + + + + + + + + + + + + + + + Random Forest Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Random Forest Regression

+

This module contains an implementation of Random Forest Regression, an ensemble learning method that combines multiple decision trees to create a more robust and accurate model for predicting continuous outcomes based on input features.

+

Parameters

+
    +
  • n_trees: Number of trees in the random forest.
  • +
  • max_depth: Maximum depth of each decision tree.
  • +
  • max_features: Maximum number of features to consider for each split.
  • +
+

Scratch Code

+
    +
  • random_forest_regression.py file
  • +
+
import numpy as np
+
+class RandomForestRegression:
+
+    def __init__(self, n_trees=100, max_depth=None, max_features=None):
+        """
+        Constructor for the RandomForestRegression class.
+
+        Parameters:
+        - n_trees: Number of trees in the random forest.
+        - max_depth: Maximum depth of each decision tree.
+        - max_features: Maximum number of features to consider for each split.
+        """
+        self.n_trees = n_trees
+        self.max_depth = max_depth
+        self.max_features = max_features
+        self.trees = []
+
+    def _bootstrap_sample(self, X, y):
+        """
+        Create a bootstrap sample of the dataset.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+
+        Returns:
+        - Bootstrap sample of X and y.
+        """
+        indices = np.random.choice(len(X), len(X), replace=True)
+        return X[indices], y[indices]
+
+    def _build_tree(self, X, y, depth):
+        """
+        Recursively build a decision tree.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        - depth: Current depth of the tree.
+
+        Returns:
+        - Node of the decision tree.
+        """
+        if depth == self.max_depth or np.all(y == y[0]):
+            return {'value': np.mean(y)}
+
+        n_features = X.shape[1]
+        if self.max_features is None:
+            subset_features = np.arange(n_features)
+        else:
+            subset_features = np.random.choice(n_features, self.max_features, replace=False)
+
+        # Create a random subset of features for this tree
+        X_subset = X[:, subset_features]
+
+        # Create a bootstrap sample
+        X_bootstrap, y_bootstrap = self._bootstrap_sample(X_subset, y)
+
+        # Find the best split using the selected subset of features
+        feature_index, threshold = self._find_best_split(X_bootstrap, y_bootstrap, subset_features)
+
+        if feature_index is not None:
+            # Split the dataset
+            X_left, X_right, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)
+
+            # Recursively build left and right subtrees
+            left_subtree = self._build_tree(X_left, y_left, depth + 1)
+            right_subtree = self._build_tree(X_right, y_right, depth + 1)
+
+            return {'feature_index': feature_index,
+                    'threshold': threshold,
+                    'left': left_subtree,
+                    'right': right_subtree}
+        else:
+            # If no split is found, return a leaf node
+            return {'value': np.mean(y)}
+
+    def _find_best_split(self, X, y, subset_features):
+        """
+        Find the best split for a subset of features.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        - subset_features: Subset of features to consider.
+
+        Returns:
+        - Index of the best feature and the corresponding threshold.
+        """
+        m, n = X.shape
+        best_feature_index = None
+        best_threshold = None
+        best_variance_reduction = 0
+
+        initial_variance = self._calculate_variance(y)
+
+        for feature_index in subset_features:
+            thresholds = np.unique(X[:, feature_index])
+
+            for threshold in thresholds:
+                # Split the dataset
+                _, _, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)
+
+                # Calculate variance reduction
+                left_weight = len(y_left) / m
+                right_weight = len(y_right) / m
+                variance_reduction = initial_variance - (left_weight * self._calculate_variance(y_left) + right_weight * self._calculate_variance(y_right))
+
+                # Update the best split if variance reduction is greater
+                if variance_reduction > best_variance_reduction:
+                    best_feature_index = feature_index
+                    best_threshold = threshold
+                    best_variance_reduction = variance_reduction
+
+        return best_feature_index, best_threshold
+
+    def _calculate_variance(self, y):
+        """
+        Calculate the variance of a set of target values.
+
+        Parameters:
+        - y: Target values (numpy array).
+
+        Returns:
+        - Variance of the target values.
+        """
+        return np.var(y)
+
+    def _split_dataset(self, X, y, feature_index, threshold):
+        """
+        Split the dataset based on a feature and threshold.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        - feature_index: Index of the feature to split on.
+        - threshold: Threshold value for the split.
+
+        Returns:
+        - Left and right subsets of the dataset.
+        """
+        left_mask = X[:, feature_index] <= threshold
+        right_mask = ~left_mask
+        return X[left_mask], X[right_mask], y[left_mask], y[right_mask]
+
+    def fit(self, X, y):
+        """
+        Fit the Random Forest Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        self.trees = []
+        for _ in range(self.n_trees):
+            # Create a bootstrap sample for each tree
+            X_bootstrap, y_bootstrap = self._bootstrap_sample(X, y)
+
+            # Build a decision tree and add it to the forest
+            tree = self._build_tree(X_bootstrap, y_bootstrap, depth=0)
+            self.trees.append(tree)
+
+    def _predict_single(self, tree, x):
+        """
+        Recursively predict a single data point using a decision tree.
+
+        Parameters:
+        - tree: Decision tree node.
+        - x: Input features for prediction.
+
+        Returns:
+        - Predicted value.
+        """
+        if 'value' in tree:
+            return tree['value']
+        else:
+            if x[tree['feature_index']] <= tree['threshold']:
+                return self._predict_single(tree['left'], x)
+            else:
+                return self._predict_single(tree['right'], x)
+
+    def predict(self, X):
+        """
+        Make predictions on new data using the Random Forest.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        predictions = np.array([self._predict_single(tree, x) for x in X for tree in self.trees])
+        return np.mean(predictions.reshape(-1, len(self.trees)), axis=1)
+
+
    +
  • random_forest_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from RandomForestRegressor import RandomForestRegression
+
+class TestRandomForestRegressor(unittest.TestCase):
+    def setUp(self):
+        # Create sample data for testing
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 2)
+        self.y_train = 2 * self.X_train[:, 0] + 3 * self.X_train[:, 1] + np.random.normal(0, 0.1, 100)
+
+        self.X_test = np.random.rand(10, 2)
+
+    def test_fit_predict(self):
+        # Test if the model can be fitted and predictions are made
+        rfr_model = RandomForestRegression(n_trees=5, max_depth=3, max_features=2)
+        rfr_model.fit(self.X_train, self.y_train)
+
+        # Ensure predictions are made without errors
+        predictions = rfr_model.predict(self.X_test)
+
+        # Add your specific assertions based on the expected behavior of your model
+        self.assertIsInstance(predictions, np.ndarray)
+        self.assertEqual(predictions.shape, (10,))
+
+    # Add more test cases as needed
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Ridge_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Ridge_Regression/index.html new file mode 100644 index 00000000..ea31698a --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Ridge_Regression/index.html @@ -0,0 +1,617 @@ + + + + + + + + + + + + + + + + + + + + + Ridge Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Ridge Regression

+

This module contains an implementation of Ridge Regression, a linear regression variant that includes regularization to prevent overfitting.

+

Overview

+

Ridge Regression is a linear regression technique with an added regularization term to handle multicollinearity and prevent the model from becoming too complex.

+

Parameters

+
    +
  • alpha: Regularization strength. A higher alpha increases the penalty for large coefficients.
  • +
+

Scratch Code

+
    +
  • ridge_regression.py file
  • +
+
import numpy as np
+
+class RidgeRegression:
+    def __init__(self, alpha=1.0):
+        """
+        Constructor for the Ridge Regression class.
+
+        Parameters:
+        - alpha: Regularization strength. Higher values specify stronger regularization.
+        """
+        self.alpha = alpha
+        self.weights = None
+
+    def fit(self, X, y):
+        """
+        Fit the Ridge Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        # Add a column of ones to the input features for the bias term
+        X_bias = np.c_[np.ones(X.shape[0]), X]
+
+        # Compute the closed-form solution for Ridge Regression
+        identity_matrix = np.identity(X_bias.shape[1])
+        self.weights = np.linalg.inv(X_bias.T @ X_bias + self.alpha * identity_matrix) @ X_bias.T @ y
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        # Add a column of ones to the input features for the bias term
+        X_bias = np.c_[np.ones(X.shape[0]), X]
+
+        # Make predictions using the learned weights
+        predictions = X_bias @ self.weights
+
+        return predictions
+
+
    +
  • ridge_regression_test.py file
  • +
+
import numpy as np
+import unittest
+from RidgeRegression import RidgeRegression  # Assuming your RidgeRegression class is in a separate file
+
+class TestRidgeRegression(unittest.TestCase):
+    def test_fit_predict(self):
+        # Generate synthetic data for testing
+        np.random.seed(42)
+        X_train = np.random.rand(100, 2)
+        y_train = 3 * X_train[:, 0] + 5 * X_train[:, 1] + 2 + 0.1 * np.random.randn(100)
+        X_test = np.random.rand(20, 2)
+
+        # Create a Ridge Regression model
+        ridge_model = RidgeRegression(alpha=0.1)
+
+        # Fit the model to training data
+        ridge_model.fit(X_train, y_train)
+
+        # Make predictions on test data
+        predictions = ridge_model.predict(X_test)
+
+        # Ensure the predictions have the correct shape
+        self.assertEqual(predictions.shape, (20,))
+
+    def test_invalid_alpha(self):
+        # Check if an exception is raised for an invalid alpha value
+        with self.assertRaises(ValueError):
+            RidgeRegression(alpha=-1)
+
+    # Add more test cases as needed
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/index.html b/algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/index.html new file mode 100644 index 00000000..8cd2b29c --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/index.html @@ -0,0 +1,649 @@ + + + + + + + + + + + + + + + + + + + + + Support Vector Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Support Vector Regression

+

This module contains an implementation of Support Vector Regression (SVR), a regression technique using Support Vector Machines (SVM) principles.

+

Parameters

+
    +
  • epsilon: Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function.
  • +
  • C: Regularization parameter. The strength of the regularization is inversely proportional to C.
  • +
+

Scratch Code

+
    +
  • support_vector_regression.py file
  • +
+
import numpy as np
+
+class SupportVectorRegression:
+
+    def __init__(self, epsilon=0.1, C=1.0):
+        """
+        Constructor for the SupportVectorRegression class.
+
+        Parameters:
+        - epsilon: Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function.
+        - C: Regularization parameter. The strength of the regularization is inversely proportional to C.
+        """
+        self.epsilon = epsilon
+        self.C = C
+        self.weights = None
+        self.bias = None
+
+    def _linear_kernel(self, X1, X2):
+        """
+        Linear kernel function.
+
+        Parameters:
+        - X1, X2: Input data (numpy arrays).
+
+        Returns:
+        - Linear kernel result (numpy array).
+        """
+        return np.dot(X1, X2.T)
+
+    def _compute_kernel_matrix(self, X):
+        """
+        Compute the kernel matrix for the linear kernel.
+
+        Parameters:
+        - X: Input data (numpy array).
+
+        Returns:
+        - Kernel matrix (numpy array).
+        """
+        m = X.shape[0]
+        kernel_matrix = np.zeros((m, m))
+
+        for i in range(m):
+            for j in range(m):
+                kernel_matrix[i, j] = self._linear_kernel(X[i, :], X[j, :])
+
+        return kernel_matrix
+
+    def fit(self, X, y):
+        """
+        Fit the Support Vector Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        m, n = X.shape
+
+        # Create the kernel matrix
+        kernel_matrix = self._compute_kernel_matrix(X)
+
+        # Quadratic programming problem coefficients
+        P = np.vstack([np.hstack([kernel_matrix, -kernel_matrix]),
+                       np.hstack([-kernel_matrix, kernel_matrix])])
+        q = np.vstack([self.epsilon * np.ones((m, 1)) - y, self.epsilon * np.ones((m, 1)) + y])
+
+        # Constraints matrix
+        G = np.vstack([np.eye(2 * m), -np.eye(2 * m)])
+        h = np.vstack([self.C * np.ones((2 * m, 1)), np.zeros((2 * m, 1))])
+
+        # Solve the quadratic programming problem
+        solution = np.linalg.solve(P, q)
+
+        # Extract weights and bias
+        self.weights = solution[:n]
+        self.bias = solution[n]
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        predictions = np.dot(X, self.weights) + self.bias
+        return predictions
+
+
    +
  • support_vector_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from SVR import SupportVectorRegression
+
+class TestSupportVectorRegression(unittest.TestCase):
+
+    def setUp(self):
+        # Create synthetic data for testing
+        np.random.seed(42)
+        self.X_train = 2 * np.random.rand(100, 1)
+        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)
+
+    def test_fit_predict(self):
+        # Test the fit and predict methods
+        svr_model = SupportVectorRegression(epsilon=0.1, C=1.0)
+        svr_model.fit(self.X_train, self.y_train)
+
+        # Create test data
+        X_test = np.array([[1.5], [2.0]])
+
+        # Make predictions
+        predictions = svr_model.predict(X_test)
+
+        # Assert that the predictions are NumPy arrays
+        self.assertTrue(isinstance(predictions, np.ndarray))
+
+        # Assert that the shape of predictions is as expected
+        self.assertEqual(predictions.shape, (X_test.shape[0], 1))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/index.html b/algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/index.html new file mode 100644 index 00000000..f54faaa6 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/index.html @@ -0,0 +1,636 @@ + + + + + + + + + + + + + + + + + + + + + XG Boost Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

XG Boost Regression

+

This module contains an implementation of the XGBoost Regressor, a popular ensemble learning algorithm that combines the predictions from multiple decision trees to create a more robust and accurate model for regression tasks.

+

Parameters

+
    +
  • n_estimators: Number of boosting rounds (trees).
  • +
  • learning_rate: Step size shrinkage to prevent overfitting.
  • +
  • max_depth: Maximum depth of each tree.
  • +
  • gamma: Minimum loss reduction required to make a further partition.
  • +
+

Scratch Code

+
    +
  • x_g_boost_regression.py file
  • +
+
import numpy as np
+from sklearn.tree import DecisionTreeRegressor
+
+class XGBoostRegressor:
+
+    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3, gamma=0):
+        """
+        Constructor for the XGBoostRegressor class.
+
+        Parameters:
+        - n_estimators: Number of boosting rounds (trees).
+        - learning_rate: Step size shrinkage to prevent overfitting.
+        - max_depth: Maximum depth of each tree.
+        - gamma: Minimum loss reduction required to make a further partition.
+        """
+        self.n_estimators = n_estimators
+        self.learning_rate = learning_rate
+        self.max_depth = max_depth
+        self.gamma = gamma
+        self.trees = []
+
+    def fit(self, X, y):
+        """
+        Fit the XGBoost model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        # Initialize residuals
+        residuals = np.copy(y)
+
+        for _ in range(self.n_estimators):
+            # Fit a weak learner (decision tree) to the residuals
+            tree = DecisionTreeRegressor(max_depth=self.max_depth, min_samples_split=self.gamma)
+            tree.fit(X, residuals)
+
+            # Compute predictions from the weak learner
+            predictions = tree.predict(X)
+
+            # Update residuals with the weighted sum of previous residuals and predictions
+            residuals -= self.learning_rate * predictions
+
+            # Store the tree in the list
+            self.trees.append(tree)
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        # Initialize predictions with zeros
+        predictions = np.zeros(X.shape[0])
+
+        # Make predictions using each tree and update the overall prediction
+        for tree in self.trees:
+            predictions += self.learning_rate * tree.predict(X)
+
+        return predictions
+
+
    +
  • x_g_boost_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from XGBoostRegressor import XGBoostRegressor
+
+class TestXGBoostRegressor(unittest.TestCase):
+
+    def setUp(self):
+        # Generate synthetic data for testing
+        np.random.seed(42)
+        self.X_train = np.random.rand(100, 5)
+        self.y_train = np.random.rand(100)
+        self.X_test = np.random.rand(20, 5)
+
+    def test_fit_predict(self):
+        # Test the fit and predict methods
+        xgb_model = XGBoostRegressor(n_estimators=50, learning_rate=0.1, max_depth=3, gamma=0.1)
+        xgb_model.fit(self.X_train, self.y_train)
+        predictions = xgb_model.predict(self.X_test)
+
+        # Ensure predictions have the correct shape
+        self.assertEqual(predictions.shape, (20,))
+
+    def test_invalid_parameters(self):
+        # Test invalid parameter values
+        with self.assertRaises(ValueError):
+            XGBoostRegressor(n_estimators=-1, learning_rate=0.1, max_depth=3, gamma=0.1)
+
+        with self.assertRaises(ValueError):
+            XGBoostRegressor(n_estimators=50, learning_rate=-0.1, max_depth=3, gamma=0.1)
+
+        with self.assertRaises(ValueError):
+            XGBoostRegressor(n_estimators=50, learning_rate=0.1, max_depth=-3, gamma=0.1)
+
+    def test_invalid_fit(self):
+        # Test fitting with mismatched X_train and y_train shapes
+        xgb_model = XGBoostRegressor(n_estimators=50, learning_rate=0.1, max_depth=3, gamma=0.1)
+        with self.assertRaises(ValueError):
+            xgb_model.fit(self.X_train, np.random.rand(50))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/bayesian/index.html b/algorithms/machine-learning/supervised/regressions/bayesian/index.html new file mode 100644 index 00000000..c992b940 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/bayesian/index.html @@ -0,0 +1,610 @@ + + + + + + + + + + + + + + + + + + + + + Bayesian Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Bayesian Regression

+

This module contains an implementation of Bayesian Regression, a probabilistic approach to linear regression that provides uncertainty estimates for predictions.

+

Overview

+

Bayesian Regression is an extension of traditional linear regression that models the distribution of coefficients, allowing for uncertainty in the model parameters. It's particularly useful when dealing with limited data and provides a full probability distribution over the possible values of the regression coefficients.

+

Parameters

+
    +
  • alpha: Prior precision for the coefficients.
  • +
  • beta: Precision of the noise in the observations.
  • +
+

Scratch Code

+
    +
  • bayesian_regression.py file
  • +
+
import numpy as np
+
+class BayesianRegression:
+    def __init__(self, alpha=1, beta=1):
+        """
+        Constructor for the BayesianRegression class.
+
+        Parameters:
+        - alpha: Prior precision.
+        - beta: Noise precision.
+        """
+        self.alpha = alpha
+        self.beta = beta
+        self.w_mean = None
+        self.w_precision = None
+
+    def fit(self, X, y):
+        """
+        Fit the Bayesian Regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        # Add a bias term to X
+        X = np.c_[np.ones(X.shape[0]), X]
+
+        # Compute posterior precision and mean
+        self.w_precision = self.alpha * np.eye(X.shape[1]) + self.beta * X.T @ X
+        self.w_mean = self.beta * np.linalg.solve(self.w_precision, X.T @ y)
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        # Add a bias term to X
+        X = np.c_[np.ones(X.shape[0]), X]
+
+        # Compute predicted mean
+        y_pred = X @ self.w_mean
+
+        return y_pred
+
+
    +
  • bayesian_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from BayesianRegression import BayesianRegression
+
+class TestBayesianRegression(unittest.TestCase):
+    def setUp(self):
+        # Generate synthetic data for testing
+        np.random.seed(42)
+        self.X_train = 2 * np.random.rand(100, 1)
+        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)
+
+        self.X_test = 2 * np.random.rand(20, 1)
+
+    def test_fit_predict(self):
+        blr = BayesianRegression()
+        blr.fit(self.X_train, self.y_train)
+        y_pred = blr.predict(self.X_test)
+
+        self.assertTrue(y_pred.shape == (20, 1))
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/index.html b/algorithms/machine-learning/supervised/regressions/index.html new file mode 100644 index 00000000..2a37b69c --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/index.html @@ -0,0 +1,512 @@ + + + + + + + + + + + + + + + + + + + + + Regression Algorithms 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/supervised/regressions/linear/index.html b/algorithms/machine-learning/supervised/regressions/linear/index.html new file mode 100644 index 00000000..92ca6055 --- /dev/null +++ b/algorithms/machine-learning/supervised/regressions/linear/index.html @@ -0,0 +1,624 @@ + + + + + + + + + + + + + + + + + + + + + Linear Regression - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Linear Regression

+

This module contains an implementation of the Linear Regression algorithm, a fundamental technique in machine learning for predicting a continuous outcome based on input features.

+

Parameters

+
    +
  • learning_rate: The step size for gradient descent.
  • +
  • n_iterations: The number of iterations for gradient descent.
  • +
+

Scratch Code

+
    +
  • linear_regression.py file
  • +
+
import numpy as np
+
+# Linear regression implementation
+class LinearRegression:
+    def __init__(self, learning_rate=0.01, n_iterations=1000):
+        """
+        Constructor for the LinearRegression class.
+
+        Parameters:
+        - learning_rate: The step size for gradient descent.
+        - n_iterations: The number of iterations for gradient descent.
+        - n_iterations: n_epochs.
+        """
+        self.learning_rate = learning_rate
+        self.n_iterations = n_iterations
+        self.weights = None
+        self.bias = None
+
+    def fit(self, X, y):
+        """
+        Fit the linear regression model to the input data.
+
+        Parameters:
+        - X: Input features (numpy array).
+        - y: Target values (numpy array).
+        """
+        # Initialize weights and bias
+        self.weights = np.zeros((X.shape[1], 1))
+        self.bias = 0
+
+        # Gradient Descent
+        for _ in range(self.n_iterations):
+            # Compute predictions
+            predictions = np.dot(X, self.weights) + self.bias
+
+            # Calculate errors
+            errors = predictions - y
+
+            # Update weights and bias
+            self.weights -= self.learning_rate * (1 / len(X)) * np.dot(X.T, errors)
+            self.bias -= self.learning_rate * (1 / len(X)) * np.sum(errors)
+
+    def predict(self, X):
+        """
+        Make predictions on new data.
+
+        Parameters:
+        - X: Input features for prediction (numpy array).
+
+        Returns:
+        - Predicted values (numpy array).
+        """
+        return np.dot(X, self.weights) + self.bias
+
+
    +
  • linear_regression_test.py file
  • +
+
import unittest
+import numpy as np
+from LinearRegression import LinearRegression
+
+class TestLinearRegression(unittest.TestCase):
+
+    def setUp(self):
+        # Set up some common data for testing
+        np.random.seed(42)
+        self.X_train = 2 * np.random.rand(100, 1)
+        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)
+
+        self.X_test = 2 * np.random.rand(20, 1)
+        self.y_test = 4 + 3 * self.X_test + np.random.randn(20, 1)
+
+    def test_fit_predict(self):
+        # Test the fit and predict methods
+
+        # Create a LinearRegression model
+        lr_model = LinearRegression()
+
+        # Fit the model to the training data
+        lr_model.fit(self.X_train, self.y_train)
+
+        # Make predictions on the test data
+        predictions = lr_model.predict(self.X_test)
+
+        # Check that the predictions are of the correct shape
+        self.assertEqual(predictions.shape, self.y_test.shape)
+
+    def test_predict_with_unfitted_model(self):
+        # Test predicting with an unfitted model
+
+        # Create a LinearRegression model (not fitted)
+        lr_model = LinearRegression()
+
+        # Attempt to make predictions without fitting the model
+        with self.assertRaises(ValueError):
+            _ = lr_model.predict(self.X_test)
+
+if __name__ == '__main__':
+    unittest.main()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/unsupervised/clustering/index.html b/algorithms/machine-learning/unsupervised/clustering/index.html new file mode 100644 index 00000000..b0811f39 --- /dev/null +++ b/algorithms/machine-learning/unsupervised/clustering/index.html @@ -0,0 +1,502 @@ + + + + + + + + + + + + + + + + + + + + + Clustering Algorithms 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/index.html b/algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/index.html new file mode 100644 index 00000000..4f0c62bc --- /dev/null +++ b/algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/index.html @@ -0,0 +1,720 @@ + + + + + + + + + + + + + + + + + + + + + K Means Clustering - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

K Means Clustering

+

Overview:
+K-means clustering is an unsupervised machine learning algorithm for grouping similar data points together into clusters based on their features.

+
+

Advantages of K-means:

+
    +
  • Simple and Easy to implement
  • +
  • Efficiency: K-means is computationally efficient and can handle large datasets with high dimensionality.
  • +
  • Flexibility: K-means offers flexibility as it can be easily customized for different applications, allowing the use of various distance metrics and + initialization techniques.
  • +
  • Scalability: K-means can handle large datasets with many data points
  • +
+
+

How K-means Works (Scratch Implementation Guide):

+

Algorithm Overview:

+
    +
  1. Initialization:
  2. +
  3. +

    Choose k initial centroids randomly from the dataset.

    +
  4. +
  5. +

    Iterative Process:

    +
  6. +
  7. Assign Data Points: For each data point, calculate the Euclidean distance to all centroids and assign the data point to the nearest centroid.
  8. +
  9. Update Centroids: Recalculate the centroids by averaging the data points assigned to each cluster.
  10. +
  11. +

    Check for Convergence: If the centroids do not change significantly between iterations (i.e., they converge), stop. Otherwise, repeat the process.

    +
  12. +
  13. +

    Termination:

    +
  14. +
  15. +

    The algorithm terminates either when the centroids have converged or when the maximum number of iterations is reached.

    +
  16. +
  17. +

    Output:

    +
  18. +
  19. The final cluster assignments for each data point.
  20. +
+

Parameters

+
    +
  • num_clusters: Number of clusters to form.
  • +
  • max_iterations: Maximum number of iterations before stopping.
  • +
  • show_steps: Whether to visualize the clustering process step by step (Boolean).
  • +
+

Scratch Code

+
    +
  • kmeans_scratch.py file
  • +
+
import numpy as np
+import matplotlib.pyplot as plt
+
+def euclidean_distance(point1, point2):
+    """
+    Calculate the Euclidean distance between two points in space.
+    """
+    return np.sqrt(np.sum((point1 - point2) ** 2))
+
+class KMeansClustering:
+    def __init__(self, num_clusters=5, max_iterations=100, show_steps=False):
+        """
+        Initialize the KMeans clustering model with the following parameters:
+        - num_clusters: Number of clusters we want to form
+        - max_iterations: Maximum number of iterations for the algorithm
+        - show_steps: Boolean flag to visualize the clustering process step by step
+        """
+        self.num_clusters = num_clusters
+        self.max_iterations = max_iterations
+        self.show_steps = show_steps
+        self.clusters = [[] for _ in range(self.num_clusters)]  # Initialize empty clusters
+        self.centroids = []  # List to store the centroids of clusters
+
+    def fit_predict(self, data):
+        """
+        Fit the KMeans model on the data and predict the cluster labels for each data point.
+        """
+        self.data = data
+        self.num_samples, self.num_features = data.shape  # Get number of samples and features
+        initial_sample_indices = np.random.choice(self.num_samples, self.num_clusters, replace=False)
+        self.centroids = [self.data[idx] for idx in initial_sample_indices]
+
+        for _ in range(self.max_iterations):
+            # Step 1: Assign each data point to the closest centroid to form clusters
+            self.clusters = self._assign_to_clusters(self.centroids)
+            if self.show_steps:
+                self._plot_clusters()
+
+            # Step 2: Calculate new centroids by averaging the data points in each cluster
+            old_centroids = self.centroids
+            self.centroids = self._calculate_new_centroids(self.clusters)
+
+            # Step 3: Check for convergence 
+            if self._has_converged(old_centroids, self.centroids):
+                break
+            if self.show_steps:
+                self._plot_clusters()
+
+        return self._get_cluster_labels(self.clusters)
+
+    def _assign_to_clusters(self, centroids):
+        """
+        Assign each data point to the closest centroid based on Euclidean distance.
+        """
+        clusters = [[] for _ in range(self.num_clusters)]
+        for sample_idx, sample in enumerate(self.data):
+            closest_centroid_idx = self._find_closest_centroid(sample, centroids)
+            clusters[closest_centroid_idx].append(sample_idx)
+        return clusters
+
+    def _find_closest_centroid(self, sample, centroids):
+        """
+        Find the index of the closest centroid to the given data point (sample).
+        """
+        distances = [euclidean_distance(sample, centroid) for centroid in centroids]
+        closest_idx = np.argmin(distances)  # Index of the closest centroid
+        return closest_idx
+
+    def _calculate_new_centroids(self, clusters):
+        """
+        Calculate new centroids by averaging the data points in each cluster.
+        """
+        centroids = np.zeros((self.num_clusters, self.num_features))
+        for cluster_idx, cluster in enumerate(clusters):
+            cluster_mean = np.mean(self.data[cluster], axis=0)
+            centroids[cluster_idx] = cluster_mean
+        return centroids
+
+    def _has_converged(self, old_centroids, new_centroids):
+        """
+        Check if the centroids have converged 
+        """
+        distances = [euclidean_distance(old_centroids[i], new_centroids[i]) for i in range(self.num_clusters)]
+        return sum(distances) == 0  # If centroids haven't moved, they are converged
+
+    def _get_cluster_labels(self, clusters):
+        """
+        Get the cluster labels for each data point based on the final clusters.
+        """
+        labels = np.empty(self.num_samples)
+        for cluster_idx, cluster in enumerate(clusters):
+            for sample_idx in cluster:
+                labels[sample_idx] = cluster_idx
+        return labels
+
+    def _plot_clusters(self):
+        """
+        Visualize the clusters and centroids in a 2D plot using matplotlib.
+        """
+        fig, ax = plt.subplots(figsize=(12, 8))
+        for i, cluster in enumerate(self.clusters):
+            cluster_points = self.data[cluster]
+            ax.scatter(cluster_points[:, 0], cluster_points[:, 1])
+
+        for centroid in self.centroids:
+            ax.scatter(centroid[0], centroid[1], marker="x", color="black", linewidth=2)
+
+        plt.show()
+
+
    +
  • test_kmeans.py file
  • +
+

```py +import unittest +import numpy as np +from kmeans_scratch import KMeansClustering

+

class TestKMeansClustering(unittest.TestCase):

+
def setUp(self):
+    np.random.seed(42)
+    self.X_train = np.vstack([
+        np.random.randn(100, 2) + np.array([5, 5]),
+        np.random.randn(100, 2) + np.array([-5, -5]),
+        np.random.randn(100, 2) + np.array([5, -5]),
+        np.random.randn(100, 2) + np.array([-5, 5])
+    ])
+
+def test_kmeans(self):
+    """Test the basic KMeans clustering functionality"""
+    kmeans = KMeansClustering(num_clusters=4, max_iterations=100, show_steps=False)
+
+    cluster_labels = kmeans.fit_predict(self.X_train)
+
+    unique_labels = np.unique(cluster_labels)
+    self.assertEqual(len(unique_labels), 4)  
+    self.assertEqual(cluster_labels.shape, (self.X_train.shape[0],))  
+    print("Cluster labels for the data points:")
+    print(cluster_labels)
+
+

if name == 'main': + unittest.main()

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/unsupervised/dimensionality-reduction/index.html b/algorithms/machine-learning/unsupervised/dimensionality-reduction/index.html new file mode 100644 index 00000000..4583ce35 --- /dev/null +++ b/algorithms/machine-learning/unsupervised/dimensionality-reduction/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Dimensionality Reduction 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Dimensionality Reduction 🤖

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/machine-learning/unsupervised/index.html b/algorithms/machine-learning/unsupervised/index.html new file mode 100644 index 00000000..a4e46737 --- /dev/null +++ b/algorithms/machine-learning/unsupervised/index.html @@ -0,0 +1,513 @@ + + + + + + + + + + + + + + + + + + + + + Unsupervised Machine Learning 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Bag_Of_Words/index.html b/algorithms/natural-language-processing/Bag_Of_Words/index.html new file mode 100644 index 00000000..3ec36b06 --- /dev/null +++ b/algorithms/natural-language-processing/Bag_Of_Words/index.html @@ -0,0 +1,536 @@ + + + + + + + + + + + + + + + + + + + + + Bag Of Words - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Bag Of Words

+
import re
+import pandas as pd
+from sklearn.feature_extraction.text import CountVectorizer,TfidfTransformer
+
+#data collection
+data = [
+    'Fashion is an art form and expression.',
+    'Style is a way to say who you are without having to speak.',
+    'Fashion is what you buy, style is what you do with it.',
+    'With fashion, you convey a message about yourself without uttering a single word'
+]
+
+#text processing
+
+def preprocess_text(text):
+    text = text.lower()
+    text = re.sub(r'[^a-zs]',' ',text)
+    return text
+
+preprocessed_data = [preprocess_text(doc) for doc in data]
+
+for i, doc in enumerate(preprocessed_data, 1):
+    print(f'Data-{i} {doc}')
+
+
+
+# removing words like the, is, are, and as they usually do not carry much useful information for the analysis.
+vectorizer = CountVectorizer(stop_words='english')
+X=vectorizer.fit_transform(preprocessed_data)
+Word=vectorizer.get_feature_names_out()
+
+bow_df = pd.DataFrame(X.toarray(),columns=Word)
+bow_df.index =[f'Data {i}' for i in range(1, len(data) + 1)]
+
+tfidf_transformer = TfidfTransformer()
+X_tfidf=tfidf_transformer.fit_transform(X)
+tfidf_df=pd.DataFrame(X_tfidf.toarray(), columns=Word)
+tfidf_df.index=[f'Data {i}' for i in range(1, len(data) + 1)]
+
+
+print()
+print("--------------------------------BoW Represention----------------------------")
+print(bow_df)
+
+print()
+print("--------------------------------TF-IDF Value----------------------------")
+print(tfidf_df)
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Fast_Text/index.html b/algorithms/natural-language-processing/Fast_Text/index.html new file mode 100644 index 00000000..b5b8b118 --- /dev/null +++ b/algorithms/natural-language-processing/Fast_Text/index.html @@ -0,0 +1,915 @@ + + + + + + + + + + + + + + + + + + + + + Fast Text - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Fast Text

+

Introduction

+

The FastText class implements a word representation and classification tool developed by Facebook's AI Research (FAIR) lab. FastText extends the Word2Vec model by representing each word as a bag of character n-grams. This approach helps capture subword information and improves the handling of rare words.

+

Explanation

+

Initialization

+
    +
  • vocab_size: Size of the vocabulary.
  • +
  • embedding_dim: Dimension of the word embeddings.
  • +
  • n_gram_size: Size of character n-grams.
  • +
  • learning_rate: Learning rate for updating embeddings.
  • +
  • epochs: Number of training epochs.
  • +
+

Building Vocabulary

+
    +
  • build_vocab(): Constructs the vocabulary from the input sentences and creates a reverse mapping of words to indices.
  • +
+

Generating N-grams

+
    +
  • get_ngrams(): Generates character n-grams for a given word. It pads the word with < and > symbols to handle edge cases effectively.
  • +
+

Training

+
    +
  • train(): Updates word and context embeddings using a simple Stochastic Gradient Descent (SGD) approach. The loss is computed as the squared error between the predicted and actual values.
  • +
+

Prediction

+
    +
  • predict(): Calculates the dot product between the target word and context embeddings to predict word vectors.
  • +
+

Getting Word Vectors

+
    +
  • get_word_vector(): Retrieves the embedding for a specific word from the trained model.
  • +
+

Normalization

+
    +
  • get_embedding_matrix(): Returns the normalized embedding matrix for better performance and stability.
  • +
+

Advantages

+
    +
  • Subword Information: FastText captures morphological details by using character n-grams, improving handling of rare and out-of-vocabulary words.
  • +
  • Improved Representations: The use of subwords allows for better word representations, especially for languages with rich morphology.
  • +
  • Efficiency: FastText is designed to handle large-scale datasets efficiently, with optimizations for both training and inference.
  • +
+

Applications

+
    +
  • Natural Language Processing (NLP): FastText embeddings are used in tasks like text classification, sentiment analysis, and named entity recognition.
  • +
  • Information Retrieval: Enhances search engines by providing more nuanced semantic matching between queries and documents.
  • +
  • Machine Translation: Improves translation models by leveraging subword information for better handling of rare words and phrases.
  • +
+

Implementation

+

Preprocessing

+
    +
  1. Initialization: Set up parameters such as vocabulary size, embedding dimension, n-gram size, learning rate, and number of epochs.
  2. +
+

Building Vocabulary

+
    +
  1. Build Vocabulary: Construct the vocabulary from the input sentences and create a mapping for words.
  2. +
+

Generating N-grams

+
    +
  1. Generate N-grams: Create character n-grams for each word in the vocabulary, handling edge cases with padding.
  2. +
+

Training

+
    +
  1. Train the Model: Use SGD to update word and context embeddings based on the training data.
  2. +
+

Prediction

+
    +
  1. Predict Word Vectors: Calculate the dot product between target and context embeddings to predict word vectors.
  2. +
+

Getting Word Vectors

+
    +
  1. Retrieve Word Vectors: Extract the embedding for a specific word from the trained model.
  2. +
+

Normalization

+
    +
  1. Normalize Embeddings: Return the normalized embedding matrix for stability and improved performance.
  2. +
+

For more advanced implementations, consider using optimized libraries like the FastText library by Facebook or other frameworks that offer additional features and efficiency improvements.

+

Code

+
    +
  • main.py
  • +
+
from fasttext import FastText
+
+# Example sentences
+sentences = [
+    "fast text is a library for efficient text classification",
+    "word embeddings are useful for NLP tasks",
+    "fasttext models can handle out-of-vocabulary words"
+]
+
+# Initialize and train FastText model
+fasttext_model = FastText(vocab_size=100, embedding_dim=50)
+fasttext_model.build_vocab(sentences)
+fasttext_model.train(sentences)
+
+# Get the vector for a word
+vector = fasttext_model.get_word_vector("fast")
+print(f"Vector for 'fast': {vector}")
+
+
    +
  • fast_test.py
  • +
+
import numpy as np
+from collections import defaultdict
+from sklearn.preprocessing import normalize
+
+class FastText:
+    def __init__(self, vocab_size, embedding_dim, n_gram_size=3, learning_rate=0.01, epochs=10):
+        self.vocab_size = vocab_size
+        self.embedding_dim = embedding_dim
+        self.n_gram_size = n_gram_size
+        self.learning_rate = learning_rate
+        self.epochs = epochs
+        self.word_embeddings = np.random.uniform(-0.1, 0.1, (vocab_size, embedding_dim))
+        self.context_embeddings = np.random.uniform(-0.1, 0.1, (vocab_size, embedding_dim))
+        self.vocab = {}
+        self.rev_vocab = {}
+
+    def build_vocab(self, sentences):
+        """
+        Build vocabulary from sentences.
+
+        Args:
+        sentences (list): List of sentences (strings).
+        """
+        word_count = defaultdict(int)
+        for sentence in sentences:
+            words = sentence.split()
+            for word in words:
+                word_count[word] += 1
+        self.vocab = {word: idx for idx, (word, _) in enumerate(word_count.items())}
+        self.rev_vocab = {idx: word for word, idx in self.vocab.items()}
+
+    def get_ngrams(self, word):
+        """
+        Get n-grams for a given word.
+
+        Args:
+        word (str): Input word.
+
+        Returns:
+        set: Set of n-grams.
+        """
+        ngrams = set()
+        word = '<' * (self.n_gram_size - 1) + word + '>' * (self.n_gram_size - 1)
+        for i in range(len(word) - self.n_gram_size + 1):
+            ngrams.add(word[i:i + self.n_gram_size])
+        return ngrams
+
+    def train(self, sentences):
+        """
+        Train the FastText model using the given sentences.
+
+        Args:
+        sentences (list): List of sentences (strings).
+        """
+        for epoch in range(self.epochs):
+            loss = 0
+            for sentence in sentences:
+                words = sentence.split()
+                for i, word in enumerate(words):
+                    if word not in self.vocab:
+                        continue
+                    word_idx = self.vocab[word]
+                    target_ngrams = self.get_ngrams(word)
+                    for j in range(max(0, i - 1), min(len(words), i + 2)):
+                        if i != j and words[j] in self.vocab:
+                            context_idx = self.vocab[words[j]]
+                            prediction = self.predict(word_idx, context_idx)
+                            error = prediction - 1 if j == i + 1 else prediction
+                            loss += error**2
+                            self.word_embeddings[word_idx] -= self.learning_rate * error * self.context_embeddings[context_idx]
+                            self.context_embeddings[context_idx] -= self.learning_rate * error * self.word_embeddings[word_idx]
+            print(f'Epoch {epoch + 1}/{self.epochs}, Loss: {loss}')
+
+    def predict(self, word_idx, context_idx):
+        """
+        Predict the dot product of the word and context embeddings.
+
+        Args:
+        word_idx (int): Index of the word.
+        context_idx (int): Index of the context word.
+
+        Returns:
+        float: Dot product.
+        """
+        return np.dot(self.word_embeddings[word_idx], self.context_embeddings[context_idx])
+
+    def get_word_vector(self, word):
+        """
+        Get the word vector for the specified word.
+
+        Args:
+        word (str): Input word.
+
+        Returns:
+        np.ndarray: Word vector.
+        """
+        if word in self.vocab:
+            return self.word_embeddings[self.vocab[word]]
+        else:
+            raise ValueError(f"Word '{word}' not found in vocabulary")
+
+    def get_embedding_matrix(self):
+        """
+        Get the normalized embedding matrix.
+
+        Returns:
+        np.ndarray: Normalized word embeddings.
+        """
+        return normalize(self.word_embeddings, axis=1)
+
+

References

+
    +
  1. FastText - Facebook AI Research
  2. +
  3. Understanding FastText
  4. +
  5. FastText on GitHub
  6. +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/GloVe/index.html b/algorithms/natural-language-processing/GloVe/index.html new file mode 100644 index 00000000..baac2c07 --- /dev/null +++ b/algorithms/natural-language-processing/GloVe/index.html @@ -0,0 +1,824 @@ + + + + + + + + + + + + + + + + + + + + + GloVe - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

GloVe

+

Introduction

+

The GloVe class implements the Global Vectors for Word Representation algorithm, developed by Stanford researchers. GloVe generates dense vector representations of words, capturing semantic relationships between them. Unlike traditional one-hot encoding, GloVe produces low-dimensional, continuous vectors that convey meaningful information about words and their contexts.

+

Key Concepts

+
    +
  • +

    Co-occurrence Matrix: GloVe starts by creating a co-occurrence matrix from a large corpus of text. This matrix counts how often words appear together within a given context window, capturing the frequency of word pairs.

    +
  • +
  • +

    Weighted Least Squares: The main idea behind GloVe is to factorize this co-occurrence matrix to find word vectors that capture the relationships between words. It aims to represent words that frequently appear together in similar contexts with similar vectors.

    +
  • +
  • +

    Weighting Function: To ensure that the optimization process doesn't get overwhelmed by very frequent co-occurrences, GloVe uses a weighting function. This function reduces the influence of extremely common word pairs.

    +
  • +
  • +

    Training Objective: The goal is to adjust the word vectors so that their dot products align with the observed co-occurrence counts. This helps in capturing the similarity between words based on their contexts.

    +
  • +
+

GloVe Training Objective

+

GloVe’s training involves adjusting word vectors so that their interactions match the observed co-occurrence data. It focuses on ensuring that words appearing together often have similar vector representations.

+

Advantages

+
    +
  • Efficient Training: By using a global co-occurrence matrix, GloVe captures semantic relationships effectively, including long-range dependencies.
  • +
  • Meaningful Vectors: The resulting vectors can represent complex relationships between words, such as analogies (e.g., "king" - "man" + "woman" ≈ "queen").
  • +
  • Flexibility: GloVe vectors are versatile and can be used in various NLP tasks, including sentiment analysis and machine translation.
  • +
+

Applications

+
    +
  • Natural Language Processing (NLP): GloVe vectors are used as features in NLP tasks like sentiment analysis, named entity recognition, and question answering.
  • +
  • Information Retrieval: Enhance search engines by providing better semantic matching between queries and documents.
  • +
  • Machine Translation: Improve translation models by capturing semantic similarities between words in different languages.
  • +
+

Implementation

+

Preprocessing

+
    +
  1. Clean and Tokenize: Prepare the text data by cleaning and tokenizing it into words.
  2. +
+

Building Vocabulary

+
    +
  1. Create Vocabulary: Construct a vocabulary and map words to unique indices.
  2. +
+

Co-occurrence Matrix

+
    +
  1. Build Co-occurrence Matrix: Create a matrix that captures how often each word pair appears together within a specified context.
  2. +
+

GloVe Model

+
    +
  1. Initialization: Set up the model parameters and hyperparameters.
  2. +
  3. Weighting Function: Define how to balance the importance of different co-occurrence counts.
  4. +
  5. Training: Use optimization techniques to adjust the word vectors based on the co-occurrence data.
  6. +
  7. Get Word Vector: Extract the vector representation for each word from the trained model.
  8. +
+

For more advanced implementations, consider using libraries like TensorFlow or PyTorch, which offer enhanced functionalities and optimizations.

+

Code

+
    +
  • main.py file
  • +
+
import numpy as np
+from preprocess import preprocess
+from vocab_and_matrix import build_vocab, build_cooccurrence_matrix
+from glove_model import GloVe
+
+# Example text corpus
+corpus = ["I love NLP", "NLP is a fascinating field", "Natural language processing with GloVe"]
+
+# Preprocess the corpus
+tokens = [token for sentence in corpus for token in preprocess(sentence)]
+
+# Build vocabulary and co-occurrence matrix
+word_to_index = build_vocab(tokens)
+cooccurrence_matrix = build_cooccurrence_matrix(tokens, word_to_index)
+
+# Initialize and train the GloVe model
+glove = GloVe(vocab_size=len(word_to_index), embedding_dim=50)
+glove.train(cooccurrence_matrix, epochs=100)
+
+# Get the word vector for 'nlp'
+word_vector = glove.get_word_vector('nlp', word_to_index)
+print(word_vector)
+
+
    +
  • glove_model.py file
  • +
+
import numpy as np
+
+class GloVe:
+    def __init__(self, vocab_size, embedding_dim=50, x_max=100, alpha=0.75):
+        self.vocab_size = vocab_size
+        self.embedding_dim = embedding_dim
+        self.x_max = x_max
+        self.alpha = alpha
+        self.W = np.random.rand(vocab_size, embedding_dim)
+        self.W_tilde = np.random.rand(vocab_size, embedding_dim)
+        self.b = np.random.rand(vocab_size)
+        self.b_tilde = np.random.rand(vocab_size)
+        self.gradsq_W = np.ones((vocab_size, embedding_dim))
+        self.gradsq_W_tilde = np.ones((vocab_size, embedding_dim))
+        self.gradsq_b = np.ones(vocab_size)
+        self.gradsq_b_tilde = np.ones(vocab_size)
+
+    def weighting_function(self, x):
+        if x < self.x_max:
+            return (x / self.x_max) ** self.alpha
+        return 1.0
+
+    def train(self, cooccurrence_matrix, epochs=100, learning_rate=0.05):
+        for epoch in range(epochs):
+            total_cost = 0
+            for i in range(self.vocab_size):
+                for j in range(self.vocab_size):
+                    if cooccurrence_matrix[i, j] == 0:
+                        continue
+                    X_ij = cooccurrence_matrix[i, j]
+                    weight = self.weighting_function(X_ij)
+                    cost = weight * (np.dot(self.W[i], self.W_tilde[j]) + self.b[i] + self.b_tilde[j] - np.log(X_ij)) ** 2
+                    total_cost += cost
+
+                    grad_common = weight * (np.dot(self.W[i], self.W_tilde[j]) + self.b[i] + self.b_tilde[j] - np.log(X_ij))
+                    grad_W = grad_common * self.W_tilde[j]
+                    grad_W_tilde = grad_common * self.W[i]
+                    grad_b = grad_common
+                    grad_b_tilde = grad_common
+
+                    self.W[i] -= learning_rate * grad_W / np.sqrt(self.gradsq_W[i])
+                    self.W_tilde[j] -= learning_rate * grad_W_tilde / np.sqrt(self.gradsq_W_tilde[j])
+                    self.b[i] -= learning_rate * grad_b / np.sqrt(self.gradsq_b[i])
+                    self.b_tilde[j] -= learning_rate * grad_b_tilde / np.sqrt(self.gradsq_b_tilde[j])
+
+                    self.gradsq_W[i] += grad_W ** 2
+                    self.gradsq_W_tilde[j] += grad_W_tilde ** 2
+                    self.gradsq_b[i] += grad_b ** 2
+                    self.gradsq_b_tilde[j] += grad_b_tilde ** 2
+
+            if epoch % 10 == 0:
+                print(f'Epoch: {epoch}, Cost: {total_cost}')
+
+    def get_word_vector(self, word, word_to_index):
+        if word in word_to_index:
+            word_index = word_to_index[word]
+            return self.W[word_index]
+        return None
+
+
    +
  • preprocess.py file
  • +
+
import string
+
+def preprocess(text):
+    """
+    Preprocess the text by removing punctuation, converting to lowercase, and splitting into words.
+
+    Args:
+    text (str): Input text string.
+
+    Returns:
+    list: List of words (tokens).
+    """
+    text = text.translate(str.maketrans('', '', string.punctuation)).lower()
+    tokens = text.split()
+    return tokens
+
+
    +
  • vocab_and_matrix.py file
  • +
+
import numpy as np
+from collections import Counter
+
+def build_vocab(tokens):
+    """
+    Build vocabulary from tokens and create a word-to-index mapping.
+
+    Args:
+    tokens (list): List of words (tokens).
+
+    Returns:
+    dict: Word-to-index mapping.
+    """
+    vocab = Counter(tokens)
+    word_to_index = {word: i for i, word in enumerate(vocab)}
+    return word_to_index
+
+def build_cooccurrence_matrix(tokens, word_to_index, window_size=2):
+    """
+    Build the co-occurrence matrix from tokens using a specified window size.
+
+    Args:
+    tokens (list): List of words (tokens).
+    word_to_index (dict): Word-to-index mapping.
+    window_size (int): Context window size.
+
+    Returns:
+    np.ndarray: Co-occurrence matrix.
+    """
+    vocab_size = len(word_to_index)
+    cooccurrence_matrix = np.zeros((vocab_size, vocab_size))
+
+    for i, word in enumerate(tokens):
+        word_index = word_to_index[word]
+        context_start = max(0, i - window_size)
+        context_end = min(len(tokens), i + window_size + 1)
+
+        for j in range(context_start, context_end):
+            if i != j:
+                context_word = tokens[j]
+                context_word_index = word_to_index[context_word]
+                cooccurrence_matrix[word_index, context_word_index] += 1
+
+    return cooccurrence_matrix
+
+

References

+
    +
  1. GloVe - Stanford NLP
  2. +
  3. Understanding GloVe
  4. +
  5. GloVe: Global Vectors for Word Representation - Wikipedia
  6. +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/NLTK_Setup/index.html b/algorithms/natural-language-processing/NLTK_Setup/index.html new file mode 100644 index 00000000..8befb97a --- /dev/null +++ b/algorithms/natural-language-processing/NLTK_Setup/index.html @@ -0,0 +1,788 @@ + + + + + + + + + + + + + + + + + + + + + NLTK Setup - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

NLTK Setup

+

Hello there! 🌟 Welcome to your first step into the fascinating world of Natural Language Processing (NLP) with the Natural Language Toolkit (NLTK). This guide is designed to be super beginner-friendly. We’ll cover everything from installation to basic operations with lots of explanations along the way. Let's get started!

+

What is NLTK?

+

The Natural Language Toolkit (NLTK) is a comprehensive Python library for working with human language data (text). It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning. It also includes wrappers for industrial-strength NLP libraries.

+

Key Features of NLTK:

+

1.Corpora and Lexical Resources: NLTK includes access to a variety of text corpora and lexical resources, such as WordNet, the Brown Corpus, the Gutenberg Corpus, and many more.

+

2.Text Processing Libraries: It provides tools for a wide range of text processing tasks:

+

Tokenization (splitting text into words, sentences, etc.)

+

Part-of-Speech (POS) tagging

+

Named Entity Recognition (NER)

+

Stemming and Lemmatization

+

Parsing (syntax analysis)

+

Semantic reasoning

+

3.Classification and Machine Learning: NLTK includes various classifiers and machine learning algorithms that can be used for text classification tasks.

+

4.Visualization and Demonstrations: It offers visualization tools for trees, graphs, and other linguistic structures. It also includes a number of interactive demonstrations and sample data.

+

Installation

+

First, we need to install NLTK. Make sure you have Python installed on your system. If not, you can download it from python.org. Once you have Python, open your command prompt (or terminal) and type the following command: +

pip install nltk
+
+To verify that NLTK is installed correctly, open a Python shell and import the library:

+

import nltk +If no errors occur, NLTK is successfully installed. +NLTK requires additional data packages for various functionalities. To download all the data packages, open a python shell and run : +

import nltk
+nltk.download ('all')
+
+Alternatively you can download specific data packages using :

+

nltk.download ('punkt') # Tokenizer for splitting sentences into words +nltk.download ('averaged_perceptron_tagger') # Part-of-speech tagger for tagging words with their parts of speech +nltk.download ('maxent_ne_chunker') # Named entity chunker for recognizing named entities in text +nltk.download ('words') # Corpus of English words required for many NLTK functions +Now that we have everything set up, let’s dive into some basic NLP operations with NLTK.

+

Tokenization

+

Tokenization is the process of breaking down text into smaller pieces, like words or sentences. It's like cutting a big cake into smaller slices. +

from nltk.tokenize import word_tokenize, sent_tokenize
+
+#Sample text to work with
+text = "Natural Language Processing with NLTK is fun and educational."
+
+#Tokenize into words
+words = word_tokenize(text)
+print("Word Tokenization:", words)
+
+#Tokenize into sentences
+sentences = sent_tokenize(text)
+print("Sentence Tokenization:", sentences)
+
+Word Tokenization: ['Natural', 'Language', 'Processing', 'with', 'NLTK', 'is', 'fun', 'and', 'educational', '.']

+

Sentence Tokenization: ['Natural Language Processing with NLTK is fun and educational.']

+

Stopwords Removal

+

Stopwords are common words that don’t carry much meaning on their own. In many NLP tasks, we remove these words to focus on the important ones. +

from nltk.corpus import stopwords
+
+# Get the list of stopwords in English
+stop_words = set(stopwords.words('english'))
+
+# Remove stopwords from our list of words
+filtered_words = [word for word in words if word.lower() not in stop_words]
+
+print("Filtered Words:", filtered_words)
+
+Filtered Words: ['Natural', 'Language', 'Processing', 'NLTK', 'fun', 'educational', '.']

+

Explanation:

+

stopwords.words('english'): This gives us a list of common English stopwords.

+

[word for word in words if word.lower() not in stop_words]: This is a list comprehension that filters out the stopwords from our list of words.

+

Stemming

+

Stemming is the process of reducing words to their root form. It’s like finding the 'stem' of a word. +

from nltk.stem import PorterStemmer
+
+# Create a PorterStemmer object
+ps = PorterStemmer()
+
+# Stem each word in our list of words
+stemmed_words = [ps.stem(word) for word in words]
+
+print("Stemmed Words:", stemmed_words)
+
+Stemmed Words: ['natur', 'languag', 'process', 'with', 'nltk', 'is', 'fun', 'and', 'educ', '.']

+

Explanation:

+

PorterStemmer(): This creates a PorterStemmer object, which is a popular stemming algorithm.

+

[ps.stem(word) for word in words]: This applies the stemming algorithm to each word in our list.

+

Lemmatization

+

Lemmatization is similar to stemming but it uses a dictionary to find the base form of a word. It’s more accurate than stemming. +

from nltk.stem import WordNetLemmatizer
+
+# Create a WordNetLemmatizer object
+lemmatizer = WordNetLemmatizer()
+
+# Lemmatize each word in our list of words
+lemmatized_words = [lemmatizer.lemmatize(word) for word in words]
+
+print("Lemmatized Words:", lemmatized_words)
+
+Lemmatized Words: ['Natural', 'Language', 'Processing', 'with', 'NLTK', 'is', 'fun', 'and', 'educational', '.']

+

Explanation:

+

WordNetLemmatizer(): This creates a lemmatizer object.

+

[lemmatizer.lemmatize(word) for word in words]: This applies the lemmatization process to each word in our list.

+

Part-of-speech tagging

+

Part-of-speech tagging is the process of labeling each word in a sentence with its corresponding part of speech, such as noun, verb, adjective, etc. NLTK provides functionality to perform POS tagging easily. +

# Import the word_tokenize function from nltk.tokenize module
+# Import the pos_tag function from nltk module
+from nltk.tokenize import word_tokenize
+from nltk import pos_tag
+
+# Sample text to work with
+text = "NLTK is a powerful tool for natural language processing."
+
+# Tokenize the text into individual words
+# The word_tokenize function splits the text into a list of words
+words = word_tokenize(text)
+
+# Perform Part-of-Speech (POS) tagging
+# The pos_tag function takes a list of words and assigns a part-of-speech tag to each word
+pos_tags = pos_tag(words)
+
+# Print the part-of-speech tags
+print("Part-of-speech tags:")
+print(pos_tags)
+
+Part-of-speech tags: +[('NLTK', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('powerful', 'JJ'), ('tool', 'NN'), ('for', 'IN'), ('natural', 'JJ'), ('language', 'NN'), ('processing', 'NN'), ('.', '.')]

+

Explanation:

+

pos_tags = pos_tag(words): The pos_tag function takes the list of words and assigns a part-of-speech tag to each word. For example, it might tag 'NLTK' as a proper noun (NNP), 'is' as a verb (VBZ), and so on.

+

Here is a list of common POS tags used in the Penn Treebank tag set, along with explanations and examples:

+

Common POS Tags:

+

CC: Coordinating conjunction (e.g., and, but, or)

+

CD: Cardinal number (e.g., one, two)

+

DT: Determiner (e.g., the, a, an)

+

EX: Existential there (e.g., there is)

+

FW: Foreign word (e.g., en route)

+

IN: Preposition or subordinating conjunction (e.g., in, of, like)

+

JJ: Adjective (e.g., big, blue, fast)

+

JJR: Adjective, comparative (e.g., bigger, faster)

+

JJS: Adjective, superlative (e.g., biggest, fastest)

+

LS: List item marker (e.g., 1, 2, One)

+

MD: Modal (e.g., can, will, must)

+

NN: Noun, singular or mass (e.g., dog, city, music)

+

NNS: Noun, plural (e.g., dogs, cities)

+

NNP: Proper noun, singular (e.g., John, London)

+

NNPS: Proper noun, plural (e.g., Americans, Sundays)

+

PDT: Predeterminer (e.g., all, both, half)

+

POS: Possessive ending (e.g., 's, s')

+

PRP: Personal pronoun (e.g., I, you, he)

+

PRP$: Possessive pronoun (e.g., my, your, his)

+

RB: Adverb (e.g., quickly, softly)

+

RBR: Adverb, comparative (e.g., faster, harder)

+

RBS: Adverb, superlative (e.g., fastest, hardest)

+

RP: Particle (e.g., up, off)

+

SYM: Symbol (e.g., $, %, &)

+

TO: to (e.g., to go, to read)

+

UH: Interjection (e.g., uh, well, wow)

+

VB: Verb, base form (e.g., run, eat)

+

VBD: Verb, past tense (e.g., ran, ate)

+

VBG: Verb, gerund or present participle (e.g., running, eating)

+

VBN: Verb, past participle (e.g., run, eaten)

+

VBP: Verb, non-3rd person singular present (e.g., run, eat)

+

VBZ: Verb, 3rd person singular present (e.g., runs, eats)

+

WDT: Wh-determiner (e.g., which, that)

+

WP: Wh-pronoun (e.g., who, what)

+

WP$: Possessive wh-pronoun (e.g., whose)

+

WRB: Wh-adverb (e.g., where, when)

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/N_L_P_Introduction/index.html b/algorithms/natural-language-processing/N_L_P_Introduction/index.html new file mode 100644 index 00000000..3ce99ab6 --- /dev/null +++ b/algorithms/natural-language-processing/N_L_P_Introduction/index.html @@ -0,0 +1,648 @@ + + + + + + + + + + + + + + + + + + + + + NLP Introduction - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

NLP Introduction

+

NLP Banner

+

What is NLP? 🤖

+

Natural Language Processing (NLP) is a field of artificial intelligence that enables computers to understand, interpret, and respond to human language. It bridges the gap between human communication and machine understanding, making it possible for computers to interact with us in a natural and intuitive way.

+

NLP Footer

+

Importance of NLP 🌐

+

NLP is essential for various applications that we use daily, such as:

+
    +
  • Voice Assistants (e.g., Siri, Alexa) 🗣️
  • +
  • Chatbots for customer service 💬
  • +
  • Language Translation services (e.g., Google Translate) 🌍
  • +
  • Sentiment Analysis in social media monitoring 📊
  • +
  • Text Summarization for news articles 📰
  • +
+

History and Evolution of NLP 📜

+
    +
  1. 1950s: Alan Turing's seminal paper "Computing Machinery and Intelligence" proposes the Turing Test.
  2. +
  3. 1960s-1970s: Development of early NLP systems like ELIZA and SHRDLU.
  4. +
  5. 1980s: Introduction of machine learning algorithms and statistical models.
  6. +
  7. 1990s: Emergence of more sophisticated algorithms and large annotated datasets.
  8. +
  9. 2000s: Advent of deep learning, leading to significant breakthroughs in NLP.
  10. +
  11. 2010s-Present: Development of powerful models like BERT and GPT, revolutionizing the field.
  12. +
+

Key Concepts and Terminology 📚

+
    +
  • Tokens: The smallest units of text, such as words or punctuation marks. Example: "Hello, world!" becomes ["Hello", ",", "world", "!"].
  • +
  • Corpus: A large collection of text used for training NLP models. Example: The Wikipedia Corpus.
  • +
  • Stopwords: Commonly used words (e.g., "the", "is", "in") that are often removed from text during preprocessing.
  • +
  • Stemming: Reducing words to their base or root form. Example: "running" becomes "run".
  • +
  • Lemmatization: Similar to stemming, but it reduces words to their dictionary form. Example: "better" becomes "good".
  • +
+

Real-World Use Cases 🌟

+

Voice Assistants 🗣️

+

Voice assistants like Siri and Alexa use NLP to understand and respond to user commands. For example, when you ask, "What's the weather today?", NLP helps the assistant interpret your query and provide the relevant weather information.

+

Customer Service Chatbots 💬

+

Many companies use chatbots to handle customer inquiries. NLP enables these bots to understand customer questions and provide accurate responses, improving customer satisfaction and reducing response time.

+

Language Translation 🌍

+

NLP powers translation services like Google Translate, which can translate text from one language to another. This helps break down language barriers and facilitates global communication.

+

Sentiment Analysis 📊

+

Businesses use sentiment analysis to monitor social media and understand public opinion about their products or services. NLP analyzes text to determine whether the sentiment expressed is positive, negative, or neutral.

+

Text Summarization 📰

+

NLP algorithms can summarize long articles into concise summaries, making it easier for readers to grasp the main points quickly. This is particularly useful for news articles and research papers.

+

Conclusion 🌟

+

NLP is a dynamic and rapidly evolving field that plays a crucial role in how we interact with technology. By understanding its basics and key concepts, you can start exploring the fascinating world of language and machines.

+
+
+

This README provides a brief introduction to NLP. For a deeper dive, explore more resources and start building your own NLP projects!

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Text_PreProcessing_Techniques/index.html b/algorithms/natural-language-processing/Text_PreProcessing_Techniques/index.html new file mode 100644 index 00000000..e3651c1a --- /dev/null +++ b/algorithms/natural-language-processing/Text_PreProcessing_Techniques/index.html @@ -0,0 +1,965 @@ + + + + + + + + + + + + + + + + + + + + + Text Preprocessing Techniques - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Text Preprocessing Techniques

+

Welcome to this comprehensive guide on text preprocessing with NLTK (Natural Language Toolkit)! This notebook will walk you through various essential text preprocessing techniques, all explained in simple terms with easy-to-follow code examples. Whether you're just starting out in NLP (Natural Language Processing) or looking to brush up on your skills, you're in the right place! 🚀

+

NLTK provides a comprehensive suite of tools for processing and analyzing unstructured text data.

+

1. Tokenization

+

Tokenization is the process of splitting text into individual words or sentences.

+

Sentence Tokenization

+
import nltk
+nltk.download('punkt')
+from nltk.tokenize import sent_tokenize
+
+text = "Hello World. This is NLTK. It is great for text processing."
+sentences = sent_tokenize(text)
+print(sentences)
+
+

Word Tokenization

+
from nltk.tokenize import word_tokenize
+
+words = word_tokenize(text)
+print(words)
+
+

2. Removing Stop Words

+

Stop words are common words that may not be useful for text analysis (e.g., "is", "the", "and").

+
from nltk.corpus import stopwords
+nltk.download('stopwords')
+
+stop_words = set(stopwords.words('english'))
+filtered_words = [word for word in words if word.lower() not in stop_words]
+print(filtered_words)
+
+

3. Stemming

+

Stemming reduces words to their root form by chopping off the ends. +

from nltk.stem import PorterStemmer
+stemmer = PorterStemmer()
+stemmed_words = [stemmer.stem(word) for word in filtered_words]
+print(stemmed_words)
+

+

4. Lemmatization

+

Lemmatization reduces words to their base form (lemma), taking into account the meaning of the word. +

from nltk.stem import WordNetLemmatizer
+nltk.download('wordnet')
+
+lemmatizer = WordNetLemmatizer()
+lemmatized_words = [lemmatizer.lemmatize(word) for word in filtered_words]
+print(lemmatized_words)
+

+

5. Part-of-Speech Tagging

+

Tagging words with their parts of speech (POS) helps understand the grammatical structure.

+

The complete POS tag list can be accessed from the Installation and set-up notebook. +

nltk.download('averaged_perceptron_tagger')
+
+pos_tags = nltk.pos_tag(lemmatized_words)
+print(pos_tags)
+

+

6. Named Entity Recognition

+

Identify named entities such as names of people, organizations, locations, etc.

+
# Numpy is required to run this
+%pip install numpy
+
+nltk.download('maxent_ne_chunker')
+nltk.download('words')
+from nltk.chunk import ne_chunk
+
+named_entities = ne_chunk(pos_tags)
+print(named_entities)
+
+

7. Word Frequency Distribution

+

Count the frequency of each word in the text.

+
from nltk.probability import FreqDist
+
+freq_dist = FreqDist(lemmatized_words)
+print(freq_dist.most_common(5))
+
+

8. Removing Punctuation

+

Remove punctuation from the text.

+
import string
+
+no_punct = [word for word in lemmatized_words if word not in string.punctuation]
+print(no_punct)
+
+

9. Lowercasing

+

Convert all words to lowercase.

+
lowercased = [word.lower() for word in no_punct]
+print(lowercased)
+
+

10. Spelling Correction

+

Correct the spelling of words.

+
%pip install pyspellchecker
+
+from nltk.corpus import wordnet
+from spellchecker import SpellChecker
+
+spell = SpellChecker()
+
+def correct_spelling(word):
+    if not wordnet.synsets(word):
+        return spell.correction(word)
+    return word
+
+lemmatized_words = ['hello', 'world', '.', 'klown', 'taxt', 'procass', '.']
+words_with_corrected_spelling = [correct_spelling(word) for word in lemmatized_words]
+print(words_with_corrected_spelling)
+
+

11. Removing Numbers

+

Remove numerical values from the text.

+
lemmatized_words = ['hello', 'world', '88', 'text', 'process', '.']
+
+no_numbers = [word for word in lemmatized_words if not word.isdigit()]
+print(no_numbers)
+
+

12. Word Replacement

+

Replace specific words with other words (e.g., replacing slang with formal words).

+
lemmatized_words = ['hello', 'world', 'gr8', 'text', 'NLTK', '.']
+replacements = {'NLTK': 'Natural Language Toolkit', 'gr8' : 'great'}
+
+replaced_words = [replacements.get(word, word) for word in lemmatized_words]
+print(replaced_words)
+
+

13. Synonym Replacement

+

Replace words with their synonyms.

+
from nltk.corpus import wordnet
+lemmatized_words = ['hello', 'world', 'awesome', 'text', 'great', '.']
+
+def get_synonym(word):
+    synonyms = wordnet.synsets(word)
+    if synonyms:
+        return synonyms[0].lemmas()[0].name()
+    return word
+
+synonym_replaced = [get_synonym(word) for word in lemmatized_words]
+print(synonym_replaced)
+
+

14. Extracting Bigrams and Trigrams

+

Extract bigrams (pairs of consecutive words) and trigrams (triplets of consecutive words).

+
from nltk import bigrams
+
+bigrams_list = list(bigrams(lemmatized_words))
+print(bigrams_list)
+
+from nltk import trigrams
+
+trigrams_list = list(trigrams(lemmatized_words))
+print(trigrams_list)
+
+

15. Sentence Segmentation

+

Split text into sentences while considering abbreviations and other punctuation complexities.

+
import nltk.data
+
+text = 'Hello World. This is NLTK. It is great for text preprocessing.'
+
+# Load the sentence tokenizer
+tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
+
+# Tokenize the text into sentences
+sentences = tokenizer.tokenize(text)
+
+# Print the tokenized sentences
+print(sentences)
+
+

16. Identifying Word Frequencies

+

Identify and display the frequency of words in a text.

+
from nltk.probability import FreqDist
+
+lemmatized_words = ['hello', 'hello', 'awesome', 'text', 'great', '.', '.', '.']
+
+
+word_freq = FreqDist(lemmatized_words)
+for word, freq in word_freq.items():
+    print(f"{word}: {freq}")
+
+

17. Removing HTML tags

+

Remove HTML tags from the text. +

%pip install bs4
+
+from bs4 import BeautifulSoup
+
+html_text = "<p>Hello World. This is NLTK.</p>"
+soup = BeautifulSoup(html_text, "html.parser")
+cleaned_text = soup.get_text()
+print(cleaned_text)
+

+

18. Detecting Language

+

Detect the language of the text. +

%pip install langdetect
+
+from langdetect import detect
+
+language = detect(text)
+print(language) #`en` (for English)
+

+

19. Tokenizing by Regular Expressions

+

Use Regular Expressions to tokenize text. +

text = 'Hello World. This is NLTK. It is great for text preprocessing.'
+
+from nltk.tokenize import regexp_tokenize
+
+pattern = r'\w+'
+regex_tokens = regexp_tokenize(text, pattern)
+print(regex_tokens)
+

+

20. Remove Frequent Words

+

Removes frequent words (also known as “high-frequency words”) from a list of tokens using NLTK, you can use the nltk.FreqDist() function to calculate the frequency of each word and filter out the most common ones. +

import nltk
+
+# input text
+text = "Natural language processing is a field of AI. I love AI."
+
+# tokenize the text
+tokens = nltk.word_tokenize(text)
+
+# calculate the frequency of each word
+fdist = nltk.FreqDist(tokens)
+
+# remove the most common words (e.g., the top 10% of words by frequency)
+filtered_tokens = [token for token in tokens if fdist[token] < fdist.N() * 0.1]
+
+print("Tokens without frequent words:", filtered_tokens)
+

+

21. Remove extra whitespace

+

Tokenizes the input string into individual sentences and remove any leading or trailing whitespace from each sentence. +

import nltk.data
+
+# Text data
+text = 'Hello World. This is NLTK. It is great for text preprocessing.'
+
+# Load the sentence tokenizer
+tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
+
+# Tokenize the text into sentences
+sentences = tokenizer.tokenize(text)
+
+# Remove extra whitespace from each sentence
+sentences = [sentence.strip() for sentence in sentences]
+
+# Print the tokenized sentences
+print(sentences)
+

+

Conclusion 🎉

+

Text preprocessing is a crucial step in natural language processing (NLP) and can significantly impact the performance of your models and applications. With NLTK, we have a powerful toolset that simplifies and streamlines these tasks.

+

I hope this guide has provided you with a solid foundation for text preprocessing with NLTK. As you continue your journey in NLP, remember that preprocessing is just the beginning. There are many more exciting and advanced techniques to explore and apply in your projects.

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Tf_Idf/index.html b/algorithms/natural-language-processing/Tf_Idf/index.html new file mode 100644 index 00000000..a04d6976 --- /dev/null +++ b/algorithms/natural-language-processing/Tf_Idf/index.html @@ -0,0 +1,843 @@ + + + + + + + + + + + + + + + + + + + + + Tf-Idf - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Tf-Idf

+

Introduction

+

The TFIDF class converts a collection of documents into their respective TF-IDF (Term Frequency-Inverse Document Frequency) representations. TF-IDF is a statistical measure used to evaluate the importance of a word in a document relative to a collection of documents (corpus).

+

Attributes

+

The TFIDF class is initialized with two main attributes:

+
    +
  • self.vocabulary: A dictionary that maps words to their indices in the TF-IDF matrix.
  • +
  • self.idf_values: A dictionary that stores the IDF (Inverse Document Frequency) values for each word.
  • +
+

Methods

+

fit Method

+

Input

+
    +
  • documents (list of str): List of documents where each document is a string.
  • +
+

Purpose

+

Calculate the IDF values for all unique words in the corpus.

+

Steps

+
    +
  1. Count Document Occurrences: Determine how many documents contain each word.
  2. +
  3. Compute IDF: Calculate the importance of each word across all documents. Higher values indicate the word is more unique to fewer documents.
  4. +
  5. Build Vocabulary: Create a mapping of words to unique indexes.
  6. +
+

transform Method

+

Input

+
    +
  • documents (list of str): A list where each entry is a document in the form of a string.
  • +
+

Purpose

+

Convert each document into a numerical representation that shows the importance of each word.

+

Steps

+
    +
  1. Compute Term Frequency (TF): Determine how often each word appears in a document relative to the total number of words in that document.
  2. +
  3. Compute TF-IDF: Multiply the term frequency of each word by its IDF to get a measure of its relevance in each document.
  4. +
  5. Store Values: Save these numerical values in a matrix where each row represents a document.
  6. +
+

fit_transform Method

+

Purpose

+

Perform both fitting (computing IDF values) and transforming (converting documents to TF-IDF representation) in one step.

+

Explanation of the Code

+

The TFIDF class includes methods for fitting the model to the data, transforming new data into the TF-IDF representation, and combining these steps. Here's a breakdown of the primary methods:

+
    +
  1. +

    fit Method: Calculates IDF values for all unique words in the corpus. It counts the number of documents containing each word and computes the IDF. The vocabulary is built with a word-to-index mapping.

    +
  2. +
  3. +

    transform Method: Converts each document into a TF-IDF representation. It computes Term Frequency (TF) for each word in the document, calculates TF-IDF by multiplying TF with IDF, and stores these values in a matrix where each row corresponds to a document.

    +
  4. +
  5. +

    fit_transform Method: Combines the fitting and transforming steps into a single method for efficient processing of documents.

    +
  6. +
+

Code

+
    +
  • main.py file
  • +
+
import math
+from collections import Counter
+
+class TFIDF:
+    def __init__(self):
+        self.vocabulary = {}  # Vocabulary to store word indices
+        self.idf_values = {}  # IDF values for words
+
+    def fit(self, documents):
+        """
+        Compute IDF values based on the provided documents.
+
+        Args:
+            documents (list of str): List of documents where each document is a string.
+        """
+        doc_count = len(documents)
+        term_doc_count = Counter()  # To count the number of documents containing each word
+
+        # Count occurrences of words in documents
+        for doc in documents:
+            words = set(doc.split())  # Unique words in the current document
+            for word in words:
+                term_doc_count[word] += 1
+
+        # Compute IDF values
+        self.idf_values = {
+            word: math.log(doc_count / (count + 1))  # +1 to avoid division by zero
+            for word, count in term_doc_count.items()
+        }
+
+        # Build vocabulary
+        self.vocabulary = {word: idx for idx, word in enumerate(self.idf_values.keys())}
+
+    def transform(self, documents):
+        """
+        Transform documents into TF-IDF representation.
+
+        Args:
+            documents (list of str): List of documents where each document is a string.
+
+        Returns:
+            list of list of float: TF-IDF matrix where each row corresponds to a document.
+        """
+        rows = []
+        for doc in documents:
+            words = doc.split()
+            word_count = Counter(words)
+            doc_length = len(words)
+            row = [0] * len(self.vocabulary)
+
+            for word, count in word_count.items():
+                if word in self.vocabulary:
+                    tf = count / doc_length
+                    idf = self.idf_values[word]
+                    index = self.vocabulary[word]
+                    row[index] = tf * idf
+            rows.append(row)
+        return rows
+
+    def fit_transform(self, documents):
+        """
+        Compute IDF values and transform documents into TF-IDF representation.
+
+        Args:
+            documents (list of str): List of documents where each document is a string.
+
+        Returns:
+            list of list of float: TF-IDF matrix where each row corresponds to a document.
+        """
+        self.fit(documents)
+        return self.transform(documents)
+# Example usage
+if __name__ == "__main__":
+    documents = [
+        "the cat sat on the mat",
+        "the dog ate my homework",
+        "the cat ate the dog food",
+        "I love programming in Python",
+        "Machine learning is fun",
+        "Python is a versatile language",
+        "Learning new skills is always beneficial"
+    ]
+
+    # Initialize the TF-IDF model
+    tfidf = TFIDF()
+
+    # Fit the model and transform the documents
+    tfidf_matrix = tfidf.fit_transform(documents)
+
+    # Print the vocabulary
+    print("Vocabulary:", tfidf.vocabulary)
+
+    # Print the TF-IDF representation
+    print("TF-IDF Representation:")
+    for i, vector in enumerate(tfidf_matrix):
+        print(f"Document {i + 1}: {vector}")
+
+    # More example documents with mixed content
+    more_documents = [
+        "the quick brown fox jumps over the lazy dog",
+        "a journey of a thousand miles begins with a single step",
+        "to be or not to be that is the question",
+        "the rain in Spain stays mainly in the plain",
+        "all human beings are born free and equal in dignity and rights"
+    ]
+
+    # Fit the model and transform the new set of documents
+    tfidf_more = TFIDF()
+    tfidf_matrix_more = tfidf_more.fit_transform(more_documents)
+
+    # Print the vocabulary for the new documents
+    print("\nVocabulary for new documents:", tfidf_more.vocabulary)
+
+    # Print the TF-IDF representation for the new documents
+    print("TF-IDF Representation for new documents:")
+    for i, vector in enumerate(tfidf_matrix_more):
+        print(f"Document {i + 1}: {vector}")
+
+

References

+
    +
  1. TF-IDF - Wikipedia
  2. +
  3. Understanding TF-IDF
  4. +
  5. Scikit-learn: TF-IDF
  6. +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Transformers/index.html b/algorithms/natural-language-processing/Transformers/index.html new file mode 100644 index 00000000..1591c717 --- /dev/null +++ b/algorithms/natural-language-processing/Transformers/index.html @@ -0,0 +1,669 @@ + + + + + + + + + + + + + + + + + + + + + Transformers - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Transformers

+

Welcome to the official documentation for the Transformers library! 🚀 This library, developed by Hugging Face, is designed to provide state-of-the-art natural language processing (NLP) models and tools. It's widely used for a variety of NLP tasks, including text classification, translation, summarization, and more.

+

🔍 Overview

+

Transformers are a type of deep learning model that excel in handling sequential data, like text. They rely on mechanisms such as attention to process and generate text in a way that captures long-range dependencies and contextual information.

+

Key Features

+
    +
  • State-of-the-art Models: Access pre-trained models like BERT, GPT, T5, and many more. 🏆
  • +
  • Easy-to-use Interface: Simplify the process of using and fine-tuning models with a user-friendly API. 🎯
  • +
  • Tokenization Tools: Tokenize and preprocess text efficiently for model input. 🧩
  • +
  • Multi-Framework Support: Compatible with PyTorch and TensorFlow, giving you flexibility in your deep learning environment. ⚙️
  • +
  • Extensive Documentation: Detailed guides and tutorials to help you get started and master the library. 📖
  • +
+

🔧 Installation

+

To get started with the Transformers library, you need to install it via pip:

+
pip install transformers
+
+

System Requirements

+
    +
  • Python: Version 3.6 or later.
  • +
  • PyTorch or TensorFlow: Depending on your preferred framework. Visit the official documentation for compatibility details.
  • +
+

🚀 Quick Start

+

Here's a basic example to demonstrate how to use the library for sentiment classification:

+
from transformers import pipeline
+
+# Initialize the pipeline for sentiment analysis
+classifier = pipeline('sentiment-analysis')
+
+# Analyze sentiment of a sample text
+result = classifier("Transformers are amazing for NLP tasks! 🌟")
+
+print(result)
+
+

Common Pipelines

+
    +
  • Text Classification: Classify text into predefined categories.
  • +
  • Named Entity Recognition (NER): Identify entities like names, dates, and locations.
  • +
  • Text Generation: Generate text based on a prompt.
  • +
  • Question Answering: Answer questions based on a given context.
  • +
  • Translation: Translate text between different languages.
  • +
+

📚 Documentation

+

For comprehensive guides, tutorials, and API references, check out the following resources:

+
    +
  • Transformers Documentation: The official site with detailed information on using and customizing the library.
  • +
  • Model Hub: Explore a wide range of pre-trained models available for different NLP tasks.
  • +
  • API Reference: Detailed descriptions of classes and functions in the library.
  • +
+

🛠️ Community and Support

+

Join the vibrant community of Transformers users and contributors to get support, share your work, and stay updated:

+
    +
  • Hugging Face Forums: Engage with other users and experts. Ask questions, share your projects, and participate in discussions.
  • +
  • GitHub Repository: Browse the source code, report issues, and contribute to the project. Check out the issues for ongoing conversations.
  • +
+

🔗 Additional Resources

+
    +
  • Research Papers: Read the research papers behind the models and techniques used in the library.
  • +
  • Blog Posts: Discover insights, tutorials, and updates from the Hugging Face team.
  • +
  • Webinars and Talks: Watch recorded talks and webinars on the latest developments and applications of Transformers.
  • +
+

❓ FAQ

+

Q: What are the main differences between BERT and GPT?

+

A: BERT (Bidirectional Encoder Representations from Transformers) is designed for understanding the context of words in both directions (left and right). GPT (Generative Pre-trained Transformer), on the other hand, is designed for generating text and understanding context in a left-to-right manner.

+

Q: Can I fine-tune a model on my own data?

+

A: Yes, the Transformers library provides tools for fine-tuning pre-trained models on your custom datasets. Check out the fine-tuning guide for more details.

+

Happy Transforming! 🌟

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Word_2_Vec/index.html b/algorithms/natural-language-processing/Word_2_Vec/index.html new file mode 100644 index 00000000..39fc843a --- /dev/null +++ b/algorithms/natural-language-processing/Word_2_Vec/index.html @@ -0,0 +1,862 @@ + + + + + + + + + + + + + + + + + + + + + Word 2 Vec - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Word 2 Vec

+

Introduction

+

Word2Vec is a technique to learn word embeddings using neural networks. The primary goal is to represent words in a continuous vector space where semantically similar words are mapped to nearby points. Word2Vec can be implemented using two main architectures:

+
    +
  1. Continuous Bag of Words (CBOW): Predicts the target word based on the context words (surrounding words).
  2. +
  3. Skip-gram: Predicts the context words based on a given target word.
  4. +
+

In this example, we focus on the Skip-gram approach, which is more commonly used in practice. The Skip-gram model tries to maximize the probability of context words given a target word.

+

Installation

+

Ensure you have Python installed. You can install the necessary dependencies using pip:

+
pip install numpy
+
+

Usage

+

Initialization

+

Define the parameters for the Word2Vec model:

+
    +
  • window_size: Defines the size of the context window around the target word.
  • +
  • embedding_dim: Dimension of the word vectors (embedding space).
  • +
  • learning_rate: Rate at which weights are updated.
  • +
+

Tokenization

+

The tokenize method creates a vocabulary from the documents and builds mappings between words and their indices.

+

Generate Training Data

+

The generate_training_data method creates pairs of target words and context words based on the window size.

+

Training

+

The train method initializes the weight matrices and updates them using gradient descent.

+

For each word-context pair, it computes the hidden layer representation, predicts context probabilities, calculates the error, and updates the weights.

+

Retrieve Word Vector

+

The get_word_vector method retrieves the embedding of a specific word.

+

Explanation of the Code

+

Initialization

+
    +
  • Parameters:
  • +
  • window_size: Size of the context window around the target word.
  • +
  • embedding_dim: Dimension of the word vectors (embedding space).
  • +
  • learning_rate: Rate at which weights are updated.
  • +
+

Tokenization

+
    +
  • The tokenize method creates a vocabulary from the documents.
  • +
  • Builds mappings between words and their indices.
  • +
+

Generate Training Data

+
    +
  • The generate_training_data method creates pairs of target words and context words based on the window size.
  • +
+

Training

+
    +
  • The train method initializes the weight matrices.
  • +
  • Updates the weights using gradient descent.
  • +
  • For each word-context pair:
  • +
  • Computes the hidden layer representation.
  • +
  • Predicts context probabilities.
  • +
  • Calculates the error.
  • +
  • Updates the weights.
  • +
+

Softmax Function

+
    +
  • The softmax function converts the output layer scores into probabilities.
  • +
  • Used to compute the error and update the weights.
  • +
+

Retrieve Word Vector

+
    +
  • The get_word_vector method retrieves the embedding of a specific word.
  • +
+

Code

+
    +
  • word2vec.py file
  • +
+
import numpy as np
+
+class Word2Vec:
+    def __init__(self, window_size=2, embedding_dim=10, learning_rate=0.01):
+        # Initialize parameters
+        self.window_size = window_size
+        self.embedding_dim = embedding_dim
+        self.learning_rate = learning_rate
+        self.vocabulary = {}
+        self.word_index = {}
+        self.index_word = {}
+        self.W1 = None
+        self.W2 = None
+
+    def tokenize(self, documents):
+        # Tokenize documents and build vocabulary
+        vocabulary = set()
+        for doc in documents:
+            words = doc.split()
+            vocabulary.update(words)
+
+        self.vocabulary = list(vocabulary)
+        self.word_index = {word: idx for idx, word in enumerate(self.vocabulary)}
+        self.index_word = {idx: word for idx, word in enumerate(self.vocabulary)}
+
+    def generate_training_data(self, documents):
+        # Generate training data for the Skip-gram model
+        training_data = []
+        for doc in documents:
+            words = doc.split()
+            for idx, word in enumerate(words):
+                target_word = self.word_index[word]
+                context = [self.word_index[words[i]] for i in range(max(0, idx - self.window_size), min(len(words), idx + self.window_size + 1)) if i != idx]
+                for context_word in context:
+                    training_data.append((target_word, context_word))
+        return training_data
+
+    def train(self, documents, epochs=1000):
+        # Tokenize the documents and generate training data
+        self.tokenize(documents)
+        training_data = self.generate_training_data(documents)
+
+        # Initialize weight matrices with random values
+        vocab_size = len(self.vocabulary)
+        self.W1 = np.random.uniform(-1, 1, (vocab_size, self.embedding_dim))
+        self.W2 = np.random.uniform(-1, 1, (self.embedding_dim, vocab_size))
+
+        for epoch in range(epochs):
+            loss = 0
+            for target_word, context_word in training_data:
+                # Forward pass
+                h = self.W1[target_word]  # Hidden layer representation of the target word
+                u = np.dot(h, self.W2)    # Output layer scores
+                y_pred = self.softmax(u) # Predicted probabilities
+
+                # Calculate error
+                e = np.zeros(vocab_size)
+                e[context_word] = 1
+                error = y_pred - e
+
+                # Backpropagation
+                self.W1[target_word] -= self.learning_rate * np.dot(self.W2, error)
+                self.W2 -= self.learning_rate * np.outer(h, error)
+
+                # Calculate loss (cross-entropy)
+                loss -= np.log(y_pred[context_word])
+
+            if (epoch + 1) % 100 == 0:
+                print(f'Epoch {epoch + 1}, Loss: {loss}')
+
+    def softmax(self, x):
+        # Softmax function to convert scores into probabilities
+        e_x = np.exp(x - np.max(x))
+        return e_x / e_x.sum(axis=0)
+
+    def get_word_vector(self, word):
+        # Retrieve the vector representation of a word
+        return self.W1[self.word_index[word]]
+
+    def get_vocabulary(self):
+        # Retrieve the vocabulary
+        return self.vocabulary
+# Example usage
+if __name__ == "__main__":
+    # Basic example usage    
+    documents = [
+        "the cat sat on the mat",
+        "the dog ate my homework",
+        "the cat ate the dog food",
+        "I love programming in Python",
+        "Machine learning is fun",
+        "Python is a versatile language",
+        "Learning new skills is always beneficial"
+    ]
+
+    # Initialize and train the Word2Vec model
+    word2vec = Word2Vec()
+    word2vec.train(documents)
+
+    # Print the vocabulary
+    print("Vocabulary:", word2vec.get_vocabulary())
+
+    # Print the word vectors for each word in the vocabulary
+    print("Word Vectors:")
+    for word in word2vec.get_vocabulary():
+        vector = word2vec.get_word_vector(word)
+        print(f"Vector for '{word}':", vector)
+
+    # More example documents with mixed content
+    more_documents = [
+        "the quick brown fox jumps over the lazy dog",
+        "a journey of a thousand miles begins with a single step",
+        "to be or not to be that is the question",
+        "the rain in Spain stays mainly in the plain",
+        "all human beings are born free and equal in dignity and rights"
+    ]
+
+    # Initialize and train the Word2Vec model on new documents
+    word2vec_more = Word2Vec()
+    word2vec_more.train(more_documents)
+
+    # Print the word vectors for selected words
+    print("\nWord Vectors for new documents:")
+    for word in ['quick', 'journey', 'be', 'rain', 'human']:
+        vector = word2vec_more.get_word_vector(word)
+        print(f"Vector for '{word}':", vector)
+
+

References

+
    +
  1. Word2Vec - Google
  2. +
  3. Efficient Estimation of Word Representations in Vector Space
  4. +
  5. Distributed Representations of Words and Phrases and their Compositionality
  6. +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/Word_Embeddings/index.html b/algorithms/natural-language-processing/Word_Embeddings/index.html new file mode 100644 index 00000000..c8e7ec5a --- /dev/null +++ b/algorithms/natural-language-processing/Word_Embeddings/index.html @@ -0,0 +1,846 @@ + + + + + + + + + + + + + + + + + + + + + Word Embeddings - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Word Embeddings

+

Introduction

+

Word embeddings are a type of word representation that allows words to be represented as vectors in a continuous vector space. These embeddings capture semantic meanings of words based on their context and usage in a given corpus. Word embeddings have become a fundamental concept in Natural Language Processing (NLP) and are widely used for various NLP tasks such as sentiment analysis, machine translation, and more.

+

Why Use Word Embeddings?

+

Traditional word representations, such as one-hot encoding, have limitations: +- High Dimensionality: One-hot encoding results in very high-dimensional vectors, which are sparse (mostly zeros). +- Lack of Semantics: One-hot vectors do not capture any semantic relationships between words.

+

Word embeddings address these issues by: +- Dimensionality Reduction: Representing words in a lower-dimensional space. +- Capturing Semantics: Encoding words such that similar words are closer together in the vector space.

+

Types of Word Embeddings

+

1. Word2Vec

+

Developed by Mikolov et al., Word2Vec generates word embeddings using neural networks. There are two main models:

+
    +
  • Continuous Bag of Words (CBOW): Predicts a target word based on its surrounding context words.
  • +
  • Skip-gram: Predicts surrounding context words given a target word.
  • +
+

Advantages

+
    +
  • Efficient and scalable.
  • +
  • Captures semantic similarity.
  • +
+

Limitations

+
    +
  • Context window size and other parameters need tuning.
  • +
  • Does not consider word order beyond the fixed context window.
  • +
+

2. GloVe (Global Vectors for Word Representation)

+

Developed by Pennington et al., GloVe generates embeddings by factorizing the word co-occurrence matrix. The key idea is to use the global statistical information of the corpus.

+

Advantages

+
    +
  • Captures global statistical information.
  • +
  • Produces high-quality embeddings.
  • +
+

Limitations

+
    +
  • Computationally intensive.
  • +
  • Fixed window size and parameters.
  • +
+

3. FastText

+

Developed by Facebook's AI Research (FAIR) lab, FastText is an extension of Word2Vec that represents words as bags of character n-grams. This allows FastText to generate embeddings for out-of-vocabulary words.

+

Advantages

+
    +
  • Handles morphologically rich languages better.
  • +
  • Generates embeddings for out-of-vocabulary words.
  • +
+

Limitations

+
    +
  • Slightly more complex to train than Word2Vec.
  • +
  • Increased training time.
  • +
+

4. ELMo (Embeddings from Language Models)

+

Developed by Peters et al., ELMo generates embeddings using deep contextualized word representations based on a bidirectional LSTM.

+

Advantages

+
    +
  • Contextual embeddings.
  • +
  • Captures polysemy (different meanings of a word).
  • +
+

Limitations

+
    +
  • Computationally expensive.
  • +
  • Not as interpretable as static embeddings.
  • +
+

5. BERT (Bidirectional Encoder Representations from Transformers)

+

Developed by Devlin et al., BERT uses transformers to generate embeddings. BERT's embeddings are contextual and capture bidirectional context.

+

Advantages

+
    +
  • State-of-the-art performance in many NLP tasks.
  • +
  • Contextual embeddings capture richer semantics.
  • +
+

Limitations

+
    +
  • Requires significant computational resources.
  • +
  • Complexity in implementation.
  • +
+

Applications of Word Embeddings

+
    +
  • Sentiment Analysis: Understanding the sentiment of a text by analyzing word embeddings.
  • +
  • Machine Translation: Translating text from one language to another using embeddings.
  • +
  • Text Classification: Categorizing text into predefined categories.
  • +
  • Named Entity Recognition: Identifying and classifying entities in text.
  • +
+

Example Code

+

Here's an example of using Word2Vec with the gensim library in Python:

+
from gensim.models import Word2Vec
+from nltk.tokenize import word_tokenize
+import nltk
+
+nltk.download('punkt')
+
+# Sample corpus
+corpus = [
+    "This is a sample sentence",
+    "Word embeddings are useful in NLP",
+    "Natural Language Processing with embeddings"
+]
+
+# Tokenize the corpus
+tokenized_corpus = [word_tokenize(sentence.lower()) for sentence in corpus]
+
+# Train Word2Vec model
+model = Word2Vec(sentences=tokenized_corpus, vector_size=50, window=3, min_count=1, sg=1)
+
+# Get the embedding for the word 'word'
+word_embedding = model.wv['word']
+print("Embedding for 'word':", word_embedding)
+
+

Conclusion

+

Word embeddings are a powerful technique in NLP that provide a way to represent words in a dense, continuous vector space. By capturing semantic relationships and reducing dimensionality, embeddings improve the performance of various NLP tasks and models.

+

References

+ + + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/natural-language-processing/index.html b/algorithms/natural-language-processing/index.html new file mode 100644 index 00000000..cf3615f5 --- /dev/null +++ b/algorithms/natural-language-processing/index.html @@ -0,0 +1,591 @@ + + + + + + + + + + + + + + + + + + + + + Natural Language Processing 🗣️ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/descriptive/index.html b/algorithms/statistics/descriptive/index.html new file mode 100644 index 00000000..9c038664 --- /dev/null +++ b/algorithms/statistics/descriptive/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Descriptive Statstics 📃 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Descriptive Statstics 📃

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/index.html b/algorithms/statistics/index.html new file mode 100644 index 00000000..293492ca --- /dev/null +++ b/algorithms/statistics/index.html @@ -0,0 +1,536 @@ + + + + + + + + + + + + + + + + + + + + + Statistics 📃 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/inferential/index.html b/algorithms/statistics/inferential/index.html new file mode 100644 index 00000000..d949b0c6 --- /dev/null +++ b/algorithms/statistics/inferential/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Inferential Statstics 📃 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Inferential Statstics 📃

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/errors/Mean_Absolute_Error/index.html b/algorithms/statistics/metrics-and-losses/errors/Mean_Absolute_Error/index.html new file mode 100644 index 00000000..a8173b9d --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/errors/Mean_Absolute_Error/index.html @@ -0,0 +1,503 @@ + + + + + + + + + + + + + + + + + + + + + Mean Absolute Error - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Mean Absolute Error

+
import numpy as np
+
+def mean_absolute_error(y_true, y_pred):
+    """
+    Calculate the mean absolute error between true and predicted values.
+
+    Parameters:
+    - y_true: True target values (numpy array).
+    - y_pred: Predicted values (numpy array).
+
+    Returns:
+    - Mean absolute error (float).
+    """    
+    return (np.absolute(y_true - y_pred)).mean()
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/errors/Mean_Squared_Error/index.html b/algorithms/statistics/metrics-and-losses/errors/Mean_Squared_Error/index.html new file mode 100644 index 00000000..15f3b168 --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/errors/Mean_Squared_Error/index.html @@ -0,0 +1,503 @@ + + + + + + + + + + + + + + + + + + + + + Mean Squared Error - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Mean Squared Error

+
import numpy as np
+
+def mean_squared_error(y_true, y_pred):
+    """
+    Calculate the mean squared error between true and predicted values.
+
+    Parameters:
+    - y_true: True target values (numpy array).
+    - y_pred: Predicted values (numpy array).
+
+    Returns:
+    - Mean squared error (float).
+    """
+    return np.mean((y_true - y_pred) ** 2)
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/errors/R2_Squared_Error/index.html b/algorithms/statistics/metrics-and-losses/errors/R2_Squared_Error/index.html new file mode 100644 index 00000000..2eed8aa3 --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/errors/R2_Squared_Error/index.html @@ -0,0 +1,506 @@ + + + + + + + + + + + + + + + + + + + + + R2 Squared Error - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

R2 Squared Error

+
import numpy as np
+
+def r_squared(y_true, y_pred):
+    """
+    Calculate the R-squared value between true and predicted values.
+
+    Parameters:
+    - y_true: True target values (numpy array).
+    - y_pred: Predicted values (numpy array).
+
+    Returns:
+    - R-squared value (float).
+    """
+    total_variance = np.sum((y_true - np.mean(y_true)) ** 2)
+    explained_variance = np.sum((y_pred - np.mean(y_true)) ** 2)
+    r2 = 1 - (explained_variance / total_variance)
+    return r2
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/errors/Root_Mean_Squared_Error/index.html b/algorithms/statistics/metrics-and-losses/errors/Root_Mean_Squared_Error/index.html new file mode 100644 index 00000000..a1c25d79 --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/errors/Root_Mean_Squared_Error/index.html @@ -0,0 +1,504 @@ + + + + + + + + + + + + + + + + + + + + + Root Mean Squared Error - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Root Mean Squared Error

+
import numpy as np
+import math as mt
+
+def root_mean_squared_error(y_true,y_pred):
+    """
+    Calculate the root mean squared error between true and predicted values.
+
+    Parameters:
+    - y_true: True target values (numpy array).
+    - y_pred: Predicted values (numpy array).
+
+    Returns:
+    - Root Mean squared error (float).
+    """
+    return mt.sqrt(np.mean((y_true - y_pred) ** 2))
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/index.html b/algorithms/statistics/metrics-and-losses/index.html new file mode 100644 index 00000000..422bae55 --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/index.html @@ -0,0 +1,488 @@ + + + + + + + + + + + + + + + + + + + + + Metrics and Losses 📃 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Metrics and Losses 📃

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/loss-functions/Cross_Entropy_Loss/index.html b/algorithms/statistics/metrics-and-losses/loss-functions/Cross_Entropy_Loss/index.html new file mode 100644 index 00000000..a7ea371b --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/loss-functions/Cross_Entropy_Loss/index.html @@ -0,0 +1,655 @@ + + + + + + + + + + + + + + + + + + + + + Cross Entropy Loss - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Cross Entropy Loss

+
import numpy as np
+
+def binary_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:
+    """
+    Calculate the binary cross entropy loss between true and predicted values.
+    It measures the difference between the predicted probability distribution and the actual binary label distribution.
+    The formula for binary cross-entropy loss is as follows:
+
+    L(y, ŷ) = -[y * log(ŷ) + (1 — y) * log(1 — ŷ)]
+
+    where y is the true binary label (0 or 1), ŷ is the predicted probability (ranging from 0 to 1), and log is the natural logarithm.
+
+    Parameters:
+    - y_true: True target values (numpy array).
+    - y_pred: Predicted values (numpy array).
+
+    Returns:
+    - Binary cross entropy loss (float).
+    """
+    if (y_true is not None) and (y_pred is not None):
+        if type(y_true) == list:
+            y_true = np.asarray(y_true)
+        if type(y_pred) == list:
+            y_pred = np.asarray(y_pred)
+        assert y_true.shape == y_pred.shape, f"Shape of y_true ({y_true.shape}) does not match y_pred ({y_pred.shape})"
+        # calculate the binary cross-entropy loss
+        loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)).mean()
+        return loss
+    else:
+        return None
+
+def weighted_binary_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list, w_pos: float, w_neg: float) -> float:
+    """
+    Calculates the weighted binary cross entropy loss between true and predicted values.
+    Weighted Binary Cross-Entropy loss is a variation of the binary cross-entropy loss that allows for assigning different weights to positive and negative examples. This can be useful when dealing with imbalanced datasets, where one class is significantly underrepresented compared to the other.
+    The formula for weighted binary cross-entropy loss is as follows:
+
+    L(y, ŷ) = -[w_pos * y * log(ŷ) + w_neg * (1 — y) * log(1 — ŷ)]
+
+    where y is the true binary label (0 or 1), ŷ is the predicted probability (ranging from 0 to 1), log is the natural logarithm, and w_pos and w_neg are the positive and negative weights, respectively.
+
+    Parameters:
+    - y_true: True target values (numpy array).
+    - y_pred: Predicted values (numpy array).
+
+    Returns:
+    - Weighted binary cross entropy loss (float).
+    """
+    if (y_true is not None) and (y_pred is not None):
+        assert w_pos != 0.0, f"Weight w_pos = {w_pos}"
+        assert w_neg != 0.0, f"Weight w_neg = {w_neg}"
+        if type(y_true) == list:
+            y_true = np.asarray(y_true)
+        if type(y_pred) == list:
+            y_pred = np.asarray(y_pred)
+        assert y_true.shape == y_pred.shape, f"Shape of y_true ({y_true.shape}) does not match y_pred ({y_pred.shape})"
+        # calculate the binary cross-entropy loss
+        loss = -(w_pos * y_true * np.log(y_pred) + w_neg * (1 - y_true) * np.log(1 - y_pred)).mean()
+        return loss
+    else:
+        return None
+
+
+def categorical_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:
+    """
+    Calculate the categorical cross entropy loss between true and predicted values.
+    It measures the difference between the predicted probability distribution and the actual one-hot encoded label distribution.
+    The formula for categorical cross-entropy loss is as follows:
+
+    L(y, ŷ) = -1/N * Σ[Σ{y * log(ŷ)}]
+
+    where y is the true one-hot encoded label vector, ŷ is the predicted probability distribution, and log is the natural logarithm.
+
+    Parameters:
+    - y_true: True target values (numpy array) (one-hot encoded).
+    - y_pred: Predicted values (numpy array) (probabilities).
+
+    Returns:
+    - Categorical cross entropy loss (float).
+    """
+    if (y_true is not None) and (y_pred is not None):
+        if type(y_true) == list:
+            y_true = np.asarray(y_true)
+        if type(y_pred) == list:
+            y_pred = np.asarray(y_pred)
+        assert y_pred.ndim == 2, f"Shape of y_pred should be (N, C), got {y_pred.shape}"
+        assert y_true.shape == y_pred.shape, f"Shape of y_true ({y_true.shape}) does not match y_pred ({y_pred.shape})"
+
+        # Ensure numerical stability
+        y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
+
+        # calculate the categorical cross-entropy loss
+        loss = -1/len(y_true) * np.sum(np.sum(y_true * np.log(y_pred)))
+        return loss.mean()
+    else:
+        return None
+
+def sparse_categorical_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:
+    """
+    Calculate the sparse categorical cross entropy loss between true and predicted values.
+    It measures the difference between the predicted probability distribution and the actual class indices.
+    The formula for sparse categorical cross-entropy loss is as follows:
+
+    L(y, ŷ) = -Σ[log(ŷ[range(N), y])]
+
+    where y is the true class indices, ŷ is the predicted probability distribution, and log is the natural logarithm.
+
+    Parameters:
+    - y_true: True target values (numpy array) (class indices).
+    - y_pred: Predicted values (numpy array) (probabilities).
+
+    Returns:
+    - Sparse categorical cross entropy loss (float).
+    """
+    if (y_true is not None) and (y_pred is not None):
+        if type(y_true) == list:
+            y_true = np.asarray(y_true)
+        if type(y_pred) == list:
+            y_pred = np.asarray(y_pred)
+        assert y_true.shape[0] == y_pred.shape[0], f"Batch size of y_true ({y_true.shape[0]}) does not match y_pred ({y_pred.shape[0]})"
+
+        # convert true labels to one-hot encoding
+        y_true_onehot = np.zeros_like(y_pred)
+        y_true_onehot[np.arange(len(y_true)), y_true] = 1
+
+        # Ensure numerical stability
+        y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
+
+        # calculate loss
+        loss = -np.mean(np.sum(y_true_onehot * np.log(y_pred), axis=-1))
+        return loss
+    else:
+        return None
+
+
+if __name__ == "__main__":
+    # define true labels and predicted probabilities
+    y_true = np.array([0, 1, 1, 0])
+    y_pred = np.array([0.1, 0.9, 0.8, 0.3])
+
+    print("\nTesting Binary Cross Entropy Loss")
+    print("Y_True: ", y_true)
+    print("Y_Pred:", y_pred)
+    print("Binary Cross Entropy Loss: ", binary_cross_entropy_loss(y_true, y_pred))
+
+    positive_weight = 0.7
+    negative_weight = 0.3
+
+    print("\nTesting Weighted Binary Cross Entropy Loss")
+    print("Y_True: ", y_true)
+    print("Y_Pred:", y_pred)
+    print("Weighted Binary Cross Entropy Loss: ", weighted_binary_cross_entropy_loss(y_true, y_pred, positive_weight, negative_weight))
+
+    y_true = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
+    y_pred = np.array([[0.8, 0.1, 0.1], [0.2, 0.3, 0.5], [0.1, 0.6, 0.3]])
+    print("\nTesting Categorical Cross Entropy Loss")
+    print("Y_True: ", y_true)
+    print("Y_Pred:", y_pred)
+    print("Categorical Cross Entropy Loss: ", categorical_cross_entropy_loss(y_true, y_pred))
+
+    y_true = np.array([1, 2, 0])
+    y_pred = np.array([[0.1, 0.8, 0.1], [0.3, 0.2, 0.5], [0.4, 0.3, 0.3]])
+    print("\nTesting Sparse Categorical Cross Entropy Loss")
+    print("Y_True: ", y_true)
+    print("Y_Pred:", y_pred)
+    print("Sparse Categorical Cross Entropy Loss: ", sparse_categorical_cross_entropy_loss(y_true, y_pred))
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/loss-functions/Hinge_Loss/index.html b/algorithms/statistics/metrics-and-losses/loss-functions/Hinge_Loss/index.html new file mode 100644 index 00000000..8157ad19 --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/loss-functions/Hinge_Loss/index.html @@ -0,0 +1,524 @@ + + + + + + + + + + + + + + + + + + + + + Hinge Loss - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Hinge Loss

+
import numpy as np
+
+def hinge_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list)-> float:
+    """
+    Calculates the hinge loss between true and predicted values.
+
+    The formula for hinge loss is as follows:
+
+    L(y, ŷ) = max(0, 1 - y * ŷ)
+
+    """
+    if (y_true is not None) and (y_pred is not None):
+        if type(y_true) == list:
+            y_true = np.asarray(y_true)
+        if type(y_pred) == list:
+            y_pred = np.asarray(y_pred)
+        assert y_true.shape[0] == y_pred.shape[0], f"Batch size of y_true ({y_true.shape[0]}) does not match y_pred ({y_pred.shape[0]})"
+
+    # replacing 0 values to -1
+    y_pred = np.where(y_pred == 0, -1, 1)
+    y_true = np.where(y_true == 0, -1, 1)
+
+     # Calculate loss
+    loss = np.maximum(0, 1 - y_true * y_pred).mean()
+    return loss
+
+if __name__ == "__main__":
+    # define true labels and predicted probabilities
+    actual = np.array([1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1])
+    predicted = np.array([0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1])
+
+    print("\nTesting Hinge Loss")
+    print("Y_True: ", actual)
+    print("Y_Pred:", predicted)
+    print("Hinge Loss: ", hinge_loss(actual, predicted))
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/loss-functions/Kullback_Leibler_Divergence_Loss/index.html b/algorithms/statistics/metrics-and-losses/loss-functions/Kullback_Leibler_Divergence_Loss/index.html new file mode 100644 index 00000000..6e65c2af --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/loss-functions/Kullback_Leibler_Divergence_Loss/index.html @@ -0,0 +1,539 @@ + + + + + + + + + + + + + + + + + + + + + KL Divergence Loss - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

KL Divergence Loss

+
Kullback Leibler Divergence Loss
+
+
import numpy as np
+
+def kl_divergence_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:
+    """
+    Calculate the Kullback-Leibler (KL) divergence between two probability distributions.
+    KL divergence measures how one probability distribution diverges from another reference probability distribution.
+
+    The formula for KL divergence is:
+    D_KL(P || Q) = Σ P(x) * log(P(x) / Q(x))
+
+    where P is the true probability distribution and Q is the predicted probability distribution.
+
+    Parameters:
+    - y_true: True probability distribution (numpy array or list).
+    - y_pred: Predicted probability distribution (numpy array or list).
+
+    Returns:
+    - KL divergence loss (float).
+    """
+    if (y_true is not None) and (y_pred is not None):
+        if type(y_true) == list:
+            y_true = np.asarray(y_true)
+        if type(y_pred) == list:
+            y_pred = np.asarray(y_pred)
+        assert y_true.shape == y_pred.shape, f"Shape of p_true ({y_true.shape}) does not match q_pred ({y_pred.shape})"
+
+        # Ensure numerical stability by clipping the probabilities
+        y_true = np.clip(y_true, 1e-15, 1)
+        y_pred = np.clip(y_pred, 1e-15, 1)
+
+        # Normalize the distributions
+        y_true /= y_true.sum(axis=-1, keepdims=True)
+        y_pred /= y_pred.sum(axis=-1, keepdims=True)
+
+        # Calculate KL divergence
+        kl_div = np.sum(y_true * np.log(y_true / y_pred), axis=-1)
+        return kl_div.mean()
+    else:
+        return None
+
+if __name__ == "__main__":
+    y_true = np.array([[0.2, 0.5, 0.3], [0.1, 0.7, 0.2]]) # True probability distributions
+    y_pred = np.array([[0.1, 0.6, 0.3], [0.2, 0.5, 0.3]]) # Predicted probability distributions
+
+    print("\nTesting Kullback Leibler Divergence Loss")
+    print("Y_True: ", y_true)
+    print("Y_Pred:", y_pred)
+    print("Kullback Leibler Divergence Loss: ", kl_divergence_loss(y_true, y_pred))
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/index.html b/algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/index.html new file mode 100644 index 00000000..88df1c88 --- /dev/null +++ b/algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/index.html @@ -0,0 +1,571 @@ + + + + + + + + + + + + + + + + + + + + + Ranking Losses - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Ranking Losses

+

Pair Wise Ranking Loss

+
import tensorflow as tf
+from typing import Tuple
+
+def pairwise_ranking_loss(y_true: tf.Tensor, y_pred: tf.Tensor, margin: float = 1.0) -> tf.Tensor:
+    """
+    Computes the pairwise ranking loss for a batch of pairs.
+
+    Args:
+        y_true: Tensor of true labels (0 for negative pairs, 1 for positive pairs).
+        y_pred: Tensor of predicted similarities/distances, expected to be a tensor of shape (batch_size, 2, embedding_dim) where 
+                y_pred[:, 0] is the anchor and y_pred[:, 1] is the positive/negative.
+        margin: Margin parameter for the pairwise ranking loss.
+
+    Returns:
+        loss: Computed pairwise ranking loss as a scalar tensor.
+    """
+    anchor, positive_or_negative = y_pred[:, 0], y_pred[:, 1]
+
+    distances = tf.reduce_sum(tf.square(anchor - positive_or_negative), axis=-1)
+    positive_loss = y_true * distances
+    negative_loss = (1 - y_true) * tf.maximum(margin - distances, 0.0)
+
+    loss = positive_loss + negative_loss
+    return tf.reduce_mean(loss)
+
+# Example usage:
+# model.compile(optimizer='adam', loss=pairwise_ranking_loss)
+
+

Triplet Loss

+
import tensorflow as tf
+from typing import Tuple
+
+def triplet_loss_func(y_true: tf.Tensor, y_pred: tf.Tensor, alpha: float = 0.3) -> tf.Tensor:
+    """
+    Computes the triplet loss for a batch of triplets.
+
+    Args:
+        y_true: True values of classification (unused in this implementation, typically required for compatibility with Keras).
+        y_pred: Predicted values, expected to be a tensor of shape (batch_size, 3, embedding_dim) where 
+                y_pred[:, 0] is the anchor, y_pred[:, 1] is the positive, and y_pred[:, 2] is the negative.
+        alpha: Margin parameter for the triplet loss.
+
+    Returns:
+        loss: Computed triplet loss as a scalar tensor.
+    """
+    anchor, positive, negative = y_pred[:, 0], y_pred[:, 1], y_pred[:, 2]
+
+    positive_dist = tf.reduce_sum(tf.square(anchor - positive), axis=-1)
+    negative_dist = tf.reduce_sum(tf.square(anchor - negative), axis=-1)
+
+    loss = tf.maximum(positive_dist - negative_dist + alpha, 0.0)
+    return tf.reduce_mean(loss)
+
+# Example usage:
+# model.compile(optimizer='adam', loss=triplet_loss_func)
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/statistics/probability/index.html b/algorithms/statistics/probability/index.html new file mode 100644 index 00000000..76322cfe --- /dev/null +++ b/algorithms/statistics/probability/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Probability 📃 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Skip to content + + +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Probability 📃

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/assets/images/favicon.png b/assets/images/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..1cf13b9f9d978896599290a74f77d5dbe7d1655c GIT binary patch literal 1870 zcmV-U2eJ5xP)Gc)JR9QMau)O=X#!i9;T z37kk-upj^(fsR36MHs_+1RCI)NNu9}lD0S{B^g8PN?Ww(5|~L#Ng*g{WsqleV}|#l zz8@ri&cTzw_h33bHI+12+kK6WN$h#n5cD8OQt`5kw6p~9H3()bUQ8OS4Q4HTQ=1Ol z_JAocz`fLbT2^{`8n~UAo=#AUOf=SOq4pYkt;XbC&f#7lb$*7=$na!mWCQ`dBQsO0 zLFBSPj*N?#u5&pf2t4XjEGH|=pPQ8xh7tpx;US5Cx_Ju;!O`ya-yF`)b%TEt5>eP1ZX~}sjjA%FJF?h7cX8=b!DZl<6%Cv z*G0uvvU+vmnpLZ2paivG-(cd*y3$hCIcsZcYOGh{$&)A6*XX&kXZd3G8m)G$Zz-LV z^GF3VAW^Mdv!)4OM8EgqRiz~*Cji;uzl2uC9^=8I84vNp;ltJ|q-*uQwGp2ma6cY7 z;`%`!9UXO@fr&Ebapfs34OmS9^u6$)bJxrucutf>`dKPKT%%*d3XlFVKunp9 zasduxjrjs>f8V=D|J=XNZp;_Zy^WgQ$9WDjgY=z@stwiEBm9u5*|34&1Na8BMjjgf3+SHcr`5~>oz1Y?SW^=K z^bTyO6>Gar#P_W2gEMwq)ot3; zREHn~U&Dp0l6YT0&k-wLwYjb?5zGK`W6S2v+K>AM(95m2C20L|3m~rN8dprPr@t)5lsk9Hu*W z?pS990s;Ez=+Rj{x7p``4>+c0G5^pYnB1^!TL=(?HLHZ+HicG{~4F1d^5Awl_2!1jICM-!9eoLhbbT^;yHcefyTAaqRcY zmuctDopPT!%k+}x%lZRKnzykr2}}XfG_ne?nRQO~?%hkzo;@RN{P6o`&mMUWBYMTe z6i8ChtjX&gXl`nvrU>jah)2iNM%JdjqoaeaU%yVn!^70x-flljp6Q5tK}5}&X8&&G zX3fpb3E(!rH=zVI_9Gjl45w@{(ITqngWFe7@9{mX;tO25Z_8 zQHEpI+FkTU#4xu>RkN>b3Tnc3UpWzPXWm#o55GKF09j^Mh~)K7{QqbO_~(@CVq! zS<8954|P8mXN2MRs86xZ&Q4EfM@JB94b=(YGuk)s&^jiSF=t3*oNK3`rD{H`yQ?d; ztE=laAUoZx5?RC8*WKOj`%LXEkgDd>&^Q4M^z`%u0rg-It=hLCVsq!Z%^6eB-OvOT zFZ28TN&cRmgU}Elrnk43)!>Z1FCPL2K$7}gwzIc48NX}#!A1BpJP?#v5wkNprhV** z?Cpalt1oH&{r!o3eSKc&ap)iz2BTn_VV`4>9M^b3;(YY}4>#ML6{~(4mH+?%07*qo IM6N<$f(jP3KmY&$ literal 0 HcmV?d00001 diff --git a/assets/javascripts/bundle.60a45f97.min.js b/assets/javascripts/bundle.60a45f97.min.js new file mode 100644 index 00000000..f8f18f6c --- /dev/null +++ b/assets/javascripts/bundle.60a45f97.min.js @@ -0,0 +1,16 @@ +"use strict";(()=>{var Wi=Object.create;var gr=Object.defineProperty;var Di=Object.getOwnPropertyDescriptor;var Vi=Object.getOwnPropertyNames,Vt=Object.getOwnPropertySymbols,Ni=Object.getPrototypeOf,yr=Object.prototype.hasOwnProperty,ao=Object.prototype.propertyIsEnumerable;var io=(e,t,r)=>t in e?gr(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,$=(e,t)=>{for(var r in t||(t={}))yr.call(t,r)&&io(e,r,t[r]);if(Vt)for(var r of Vt(t))ao.call(t,r)&&io(e,r,t[r]);return e};var so=(e,t)=>{var r={};for(var o in e)yr.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(e!=null&&Vt)for(var o of Vt(e))t.indexOf(o)<0&&ao.call(e,o)&&(r[o]=e[o]);return r};var xr=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var zi=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of Vi(t))!yr.call(e,n)&&n!==r&&gr(e,n,{get:()=>t[n],enumerable:!(o=Di(t,n))||o.enumerable});return e};var Mt=(e,t,r)=>(r=e!=null?Wi(Ni(e)):{},zi(t||!e||!e.__esModule?gr(r,"default",{value:e,enumerable:!0}):r,e));var co=(e,t,r)=>new Promise((o,n)=>{var i=p=>{try{s(r.next(p))}catch(c){n(c)}},a=p=>{try{s(r.throw(p))}catch(c){n(c)}},s=p=>p.done?o(p.value):Promise.resolve(p.value).then(i,a);s((r=r.apply(e,t)).next())});var lo=xr((Er,po)=>{(function(e,t){typeof Er=="object"&&typeof po!="undefined"?t():typeof define=="function"&&define.amd?define(t):t()})(Er,function(){"use strict";function e(r){var o=!0,n=!1,i=null,a={text:!0,search:!0,url:!0,tel:!0,email:!0,password:!0,number:!0,date:!0,month:!0,week:!0,time:!0,datetime:!0,"datetime-local":!0};function s(k){return!!(k&&k!==document&&k.nodeName!=="HTML"&&k.nodeName!=="BODY"&&"classList"in k&&"contains"in k.classList)}function p(k){var ft=k.type,qe=k.tagName;return!!(qe==="INPUT"&&a[ft]&&!k.readOnly||qe==="TEXTAREA"&&!k.readOnly||k.isContentEditable)}function c(k){k.classList.contains("focus-visible")||(k.classList.add("focus-visible"),k.setAttribute("data-focus-visible-added",""))}function l(k){k.hasAttribute("data-focus-visible-added")&&(k.classList.remove("focus-visible"),k.removeAttribute("data-focus-visible-added"))}function f(k){k.metaKey||k.altKey||k.ctrlKey||(s(r.activeElement)&&c(r.activeElement),o=!0)}function u(k){o=!1}function d(k){s(k.target)&&(o||p(k.target))&&c(k.target)}function y(k){s(k.target)&&(k.target.classList.contains("focus-visible")||k.target.hasAttribute("data-focus-visible-added"))&&(n=!0,window.clearTimeout(i),i=window.setTimeout(function(){n=!1},100),l(k.target))}function L(k){document.visibilityState==="hidden"&&(n&&(o=!0),X())}function X(){document.addEventListener("mousemove",J),document.addEventListener("mousedown",J),document.addEventListener("mouseup",J),document.addEventListener("pointermove",J),document.addEventListener("pointerdown",J),document.addEventListener("pointerup",J),document.addEventListener("touchmove",J),document.addEventListener("touchstart",J),document.addEventListener("touchend",J)}function te(){document.removeEventListener("mousemove",J),document.removeEventListener("mousedown",J),document.removeEventListener("mouseup",J),document.removeEventListener("pointermove",J),document.removeEventListener("pointerdown",J),document.removeEventListener("pointerup",J),document.removeEventListener("touchmove",J),document.removeEventListener("touchstart",J),document.removeEventListener("touchend",J)}function J(k){k.target.nodeName&&k.target.nodeName.toLowerCase()==="html"||(o=!1,te())}document.addEventListener("keydown",f,!0),document.addEventListener("mousedown",u,!0),document.addEventListener("pointerdown",u,!0),document.addEventListener("touchstart",u,!0),document.addEventListener("visibilitychange",L,!0),X(),r.addEventListener("focus",d,!0),r.addEventListener("blur",y,!0),r.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&r.host?r.host.setAttribute("data-js-focus-visible",""):r.nodeType===Node.DOCUMENT_NODE&&(document.documentElement.classList.add("js-focus-visible"),document.documentElement.setAttribute("data-js-focus-visible",""))}if(typeof window!="undefined"&&typeof document!="undefined"){window.applyFocusVisiblePolyfill=e;var t;try{t=new CustomEvent("focus-visible-polyfill-ready")}catch(r){t=document.createEvent("CustomEvent"),t.initCustomEvent("focus-visible-polyfill-ready",!1,!1,{})}window.dispatchEvent(t)}typeof document!="undefined"&&e(document)})});var qr=xr((hy,On)=>{"use strict";/*! + * escape-html + * Copyright(c) 2012-2013 TJ Holowaychuk + * Copyright(c) 2015 Andreas Lubbe + * Copyright(c) 2015 Tiancheng "Timothy" Gu + * MIT Licensed + */var $a=/["'&<>]/;On.exports=Pa;function Pa(e){var t=""+e,r=$a.exec(t);if(!r)return t;var o,n="",i=0,a=0;for(i=r.index;i{/*! + * clipboard.js v2.0.11 + * https://clipboardjs.com/ + * + * Licensed MIT © Zeno Rocha + */(function(t,r){typeof It=="object"&&typeof Yr=="object"?Yr.exports=r():typeof define=="function"&&define.amd?define([],r):typeof It=="object"?It.ClipboardJS=r():t.ClipboardJS=r()})(It,function(){return function(){var e={686:function(o,n,i){"use strict";i.d(n,{default:function(){return Ui}});var a=i(279),s=i.n(a),p=i(370),c=i.n(p),l=i(817),f=i.n(l);function u(V){try{return document.execCommand(V)}catch(A){return!1}}var d=function(A){var M=f()(A);return u("cut"),M},y=d;function L(V){var A=document.documentElement.getAttribute("dir")==="rtl",M=document.createElement("textarea");M.style.fontSize="12pt",M.style.border="0",M.style.padding="0",M.style.margin="0",M.style.position="absolute",M.style[A?"right":"left"]="-9999px";var F=window.pageYOffset||document.documentElement.scrollTop;return M.style.top="".concat(F,"px"),M.setAttribute("readonly",""),M.value=V,M}var X=function(A,M){var F=L(A);M.container.appendChild(F);var D=f()(F);return u("copy"),F.remove(),D},te=function(A){var M=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{container:document.body},F="";return typeof A=="string"?F=X(A,M):A instanceof HTMLInputElement&&!["text","search","url","tel","password"].includes(A==null?void 0:A.type)?F=X(A.value,M):(F=f()(A),u("copy")),F},J=te;function k(V){"@babel/helpers - typeof";return typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?k=function(M){return typeof M}:k=function(M){return M&&typeof Symbol=="function"&&M.constructor===Symbol&&M!==Symbol.prototype?"symbol":typeof M},k(V)}var ft=function(){var A=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{},M=A.action,F=M===void 0?"copy":M,D=A.container,Y=A.target,$e=A.text;if(F!=="copy"&&F!=="cut")throw new Error('Invalid "action" value, use either "copy" or "cut"');if(Y!==void 0)if(Y&&k(Y)==="object"&&Y.nodeType===1){if(F==="copy"&&Y.hasAttribute("disabled"))throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');if(F==="cut"&&(Y.hasAttribute("readonly")||Y.hasAttribute("disabled")))throw new Error(`Invalid "target" attribute. You can't cut text from elements with "readonly" or "disabled" attributes`)}else throw new Error('Invalid "target" value, use a valid Element');if($e)return J($e,{container:D});if(Y)return F==="cut"?y(Y):J(Y,{container:D})},qe=ft;function Fe(V){"@babel/helpers - typeof";return typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?Fe=function(M){return typeof M}:Fe=function(M){return M&&typeof Symbol=="function"&&M.constructor===Symbol&&M!==Symbol.prototype?"symbol":typeof M},Fe(V)}function ki(V,A){if(!(V instanceof A))throw new TypeError("Cannot call a class as a function")}function no(V,A){for(var M=0;M0&&arguments[0]!==void 0?arguments[0]:{};this.action=typeof D.action=="function"?D.action:this.defaultAction,this.target=typeof D.target=="function"?D.target:this.defaultTarget,this.text=typeof D.text=="function"?D.text:this.defaultText,this.container=Fe(D.container)==="object"?D.container:document.body}},{key:"listenClick",value:function(D){var Y=this;this.listener=c()(D,"click",function($e){return Y.onClick($e)})}},{key:"onClick",value:function(D){var Y=D.delegateTarget||D.currentTarget,$e=this.action(Y)||"copy",Dt=qe({action:$e,container:this.container,target:this.target(Y),text:this.text(Y)});this.emit(Dt?"success":"error",{action:$e,text:Dt,trigger:Y,clearSelection:function(){Y&&Y.focus(),window.getSelection().removeAllRanges()}})}},{key:"defaultAction",value:function(D){return vr("action",D)}},{key:"defaultTarget",value:function(D){var Y=vr("target",D);if(Y)return document.querySelector(Y)}},{key:"defaultText",value:function(D){return vr("text",D)}},{key:"destroy",value:function(){this.listener.destroy()}}],[{key:"copy",value:function(D){var Y=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{container:document.body};return J(D,Y)}},{key:"cut",value:function(D){return y(D)}},{key:"isSupported",value:function(){var D=arguments.length>0&&arguments[0]!==void 0?arguments[0]:["copy","cut"],Y=typeof D=="string"?[D]:D,$e=!!document.queryCommandSupported;return Y.forEach(function(Dt){$e=$e&&!!document.queryCommandSupported(Dt)}),$e}}]),M}(s()),Ui=Fi},828:function(o){var n=9;if(typeof Element!="undefined"&&!Element.prototype.matches){var i=Element.prototype;i.matches=i.matchesSelector||i.mozMatchesSelector||i.msMatchesSelector||i.oMatchesSelector||i.webkitMatchesSelector}function a(s,p){for(;s&&s.nodeType!==n;){if(typeof s.matches=="function"&&s.matches(p))return s;s=s.parentNode}}o.exports=a},438:function(o,n,i){var a=i(828);function s(l,f,u,d,y){var L=c.apply(this,arguments);return l.addEventListener(u,L,y),{destroy:function(){l.removeEventListener(u,L,y)}}}function p(l,f,u,d,y){return typeof l.addEventListener=="function"?s.apply(null,arguments):typeof u=="function"?s.bind(null,document).apply(null,arguments):(typeof l=="string"&&(l=document.querySelectorAll(l)),Array.prototype.map.call(l,function(L){return s(L,f,u,d,y)}))}function c(l,f,u,d){return function(y){y.delegateTarget=a(y.target,f),y.delegateTarget&&d.call(l,y)}}o.exports=p},879:function(o,n){n.node=function(i){return i!==void 0&&i instanceof HTMLElement&&i.nodeType===1},n.nodeList=function(i){var a=Object.prototype.toString.call(i);return i!==void 0&&(a==="[object NodeList]"||a==="[object HTMLCollection]")&&"length"in i&&(i.length===0||n.node(i[0]))},n.string=function(i){return typeof i=="string"||i instanceof String},n.fn=function(i){var a=Object.prototype.toString.call(i);return a==="[object Function]"}},370:function(o,n,i){var a=i(879),s=i(438);function p(u,d,y){if(!u&&!d&&!y)throw new Error("Missing required arguments");if(!a.string(d))throw new TypeError("Second argument must be a String");if(!a.fn(y))throw new TypeError("Third argument must be a Function");if(a.node(u))return c(u,d,y);if(a.nodeList(u))return l(u,d,y);if(a.string(u))return f(u,d,y);throw new TypeError("First argument must be a String, HTMLElement, HTMLCollection, or NodeList")}function c(u,d,y){return u.addEventListener(d,y),{destroy:function(){u.removeEventListener(d,y)}}}function l(u,d,y){return Array.prototype.forEach.call(u,function(L){L.addEventListener(d,y)}),{destroy:function(){Array.prototype.forEach.call(u,function(L){L.removeEventListener(d,y)})}}}function f(u,d,y){return s(document.body,u,d,y)}o.exports=p},817:function(o){function n(i){var a;if(i.nodeName==="SELECT")i.focus(),a=i.value;else if(i.nodeName==="INPUT"||i.nodeName==="TEXTAREA"){var s=i.hasAttribute("readonly");s||i.setAttribute("readonly",""),i.select(),i.setSelectionRange(0,i.value.length),s||i.removeAttribute("readonly"),a=i.value}else{i.hasAttribute("contenteditable")&&i.focus();var p=window.getSelection(),c=document.createRange();c.selectNodeContents(i),p.removeAllRanges(),p.addRange(c),a=p.toString()}return a}o.exports=n},279:function(o){function n(){}n.prototype={on:function(i,a,s){var p=this.e||(this.e={});return(p[i]||(p[i]=[])).push({fn:a,ctx:s}),this},once:function(i,a,s){var p=this;function c(){p.off(i,c),a.apply(s,arguments)}return c._=a,this.on(i,c,s)},emit:function(i){var a=[].slice.call(arguments,1),s=((this.e||(this.e={}))[i]||[]).slice(),p=0,c=s.length;for(p;p0&&i[i.length-1])&&(c[0]===6||c[0]===2)){r=0;continue}if(c[0]===3&&(!i||c[1]>i[0]&&c[1]=e.length&&(e=void 0),{value:e&&e[o++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function N(e,t){var r=typeof Symbol=="function"&&e[Symbol.iterator];if(!r)return e;var o=r.call(e),n,i=[],a;try{for(;(t===void 0||t-- >0)&&!(n=o.next()).done;)i.push(n.value)}catch(s){a={error:s}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(a)throw a.error}}return i}function q(e,t,r){if(r||arguments.length===2)for(var o=0,n=t.length,i;o1||p(d,L)})},y&&(n[d]=y(n[d])))}function p(d,y){try{c(o[d](y))}catch(L){u(i[0][3],L)}}function c(d){d.value instanceof nt?Promise.resolve(d.value.v).then(l,f):u(i[0][2],d)}function l(d){p("next",d)}function f(d){p("throw",d)}function u(d,y){d(y),i.shift(),i.length&&p(i[0][0],i[0][1])}}function uo(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t=e[Symbol.asyncIterator],r;return t?t.call(e):(e=typeof he=="function"?he(e):e[Symbol.iterator](),r={},o("next"),o("throw"),o("return"),r[Symbol.asyncIterator]=function(){return this},r);function o(i){r[i]=e[i]&&function(a){return new Promise(function(s,p){a=e[i](a),n(s,p,a.done,a.value)})}}function n(i,a,s,p){Promise.resolve(p).then(function(c){i({value:c,done:s})},a)}}function H(e){return typeof e=="function"}function ut(e){var t=function(o){Error.call(o),o.stack=new Error().stack},r=e(t);return r.prototype=Object.create(Error.prototype),r.prototype.constructor=r,r}var zt=ut(function(e){return function(r){e(this),this.message=r?r.length+` errors occurred during unsubscription: +`+r.map(function(o,n){return n+1+") "+o.toString()}).join(` + `):"",this.name="UnsubscriptionError",this.errors=r}});function Qe(e,t){if(e){var r=e.indexOf(t);0<=r&&e.splice(r,1)}}var Ue=function(){function e(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return e.prototype.unsubscribe=function(){var t,r,o,n,i;if(!this.closed){this.closed=!0;var a=this._parentage;if(a)if(this._parentage=null,Array.isArray(a))try{for(var s=he(a),p=s.next();!p.done;p=s.next()){var c=p.value;c.remove(this)}}catch(L){t={error:L}}finally{try{p&&!p.done&&(r=s.return)&&r.call(s)}finally{if(t)throw t.error}}else a.remove(this);var l=this.initialTeardown;if(H(l))try{l()}catch(L){i=L instanceof zt?L.errors:[L]}var f=this._finalizers;if(f){this._finalizers=null;try{for(var u=he(f),d=u.next();!d.done;d=u.next()){var y=d.value;try{ho(y)}catch(L){i=i!=null?i:[],L instanceof zt?i=q(q([],N(i)),N(L.errors)):i.push(L)}}}catch(L){o={error:L}}finally{try{d&&!d.done&&(n=u.return)&&n.call(u)}finally{if(o)throw o.error}}}if(i)throw new zt(i)}},e.prototype.add=function(t){var r;if(t&&t!==this)if(this.closed)ho(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(r=this._finalizers)!==null&&r!==void 0?r:[]).push(t)}},e.prototype._hasParent=function(t){var r=this._parentage;return r===t||Array.isArray(r)&&r.includes(t)},e.prototype._addParent=function(t){var r=this._parentage;this._parentage=Array.isArray(r)?(r.push(t),r):r?[r,t]:t},e.prototype._removeParent=function(t){var r=this._parentage;r===t?this._parentage=null:Array.isArray(r)&&Qe(r,t)},e.prototype.remove=function(t){var r=this._finalizers;r&&Qe(r,t),t instanceof e&&t._removeParent(this)},e.EMPTY=function(){var t=new e;return t.closed=!0,t}(),e}();var Tr=Ue.EMPTY;function qt(e){return e instanceof Ue||e&&"closed"in e&&H(e.remove)&&H(e.add)&&H(e.unsubscribe)}function ho(e){H(e)?e():e.unsubscribe()}var Pe={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1};var dt={setTimeout:function(e,t){for(var r=[],o=2;o0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(r){return this._throwIfClosed(),e.prototype._trySubscribe.call(this,r)},t.prototype._subscribe=function(r){return this._throwIfClosed(),this._checkFinalizedStatuses(r),this._innerSubscribe(r)},t.prototype._innerSubscribe=function(r){var o=this,n=this,i=n.hasError,a=n.isStopped,s=n.observers;return i||a?Tr:(this.currentObservers=null,s.push(r),new Ue(function(){o.currentObservers=null,Qe(s,r)}))},t.prototype._checkFinalizedStatuses=function(r){var o=this,n=o.hasError,i=o.thrownError,a=o.isStopped;n?r.error(i):a&&r.complete()},t.prototype.asObservable=function(){var r=new j;return r.source=this,r},t.create=function(r,o){return new To(r,o)},t}(j);var To=function(e){oe(t,e);function t(r,o){var n=e.call(this)||this;return n.destination=r,n.source=o,n}return t.prototype.next=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.next)===null||n===void 0||n.call(o,r)},t.prototype.error=function(r){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.error)===null||n===void 0||n.call(o,r)},t.prototype.complete=function(){var r,o;(o=(r=this.destination)===null||r===void 0?void 0:r.complete)===null||o===void 0||o.call(r)},t.prototype._subscribe=function(r){var o,n;return(n=(o=this.source)===null||o===void 0?void 0:o.subscribe(r))!==null&&n!==void 0?n:Tr},t}(g);var _r=function(e){oe(t,e);function t(r){var o=e.call(this)||this;return o._value=r,o}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(r){var o=e.prototype._subscribe.call(this,r);return!o.closed&&r.next(this._value),o},t.prototype.getValue=function(){var r=this,o=r.hasError,n=r.thrownError,i=r._value;if(o)throw n;return this._throwIfClosed(),i},t.prototype.next=function(r){e.prototype.next.call(this,this._value=r)},t}(g);var At={now:function(){return(At.delegate||Date).now()},delegate:void 0};var Ct=function(e){oe(t,e);function t(r,o,n){r===void 0&&(r=1/0),o===void 0&&(o=1/0),n===void 0&&(n=At);var i=e.call(this)||this;return i._bufferSize=r,i._windowTime=o,i._timestampProvider=n,i._buffer=[],i._infiniteTimeWindow=!0,i._infiniteTimeWindow=o===1/0,i._bufferSize=Math.max(1,r),i._windowTime=Math.max(1,o),i}return t.prototype.next=function(r){var o=this,n=o.isStopped,i=o._buffer,a=o._infiniteTimeWindow,s=o._timestampProvider,p=o._windowTime;n||(i.push(r),!a&&i.push(s.now()+p)),this._trimBuffer(),e.prototype.next.call(this,r)},t.prototype._subscribe=function(r){this._throwIfClosed(),this._trimBuffer();for(var o=this._innerSubscribe(r),n=this,i=n._infiniteTimeWindow,a=n._buffer,s=a.slice(),p=0;p0?e.prototype.schedule.call(this,r,o):(this.delay=o,this.state=r,this.scheduler.flush(this),this)},t.prototype.execute=function(r,o){return o>0||this.closed?e.prototype.execute.call(this,r,o):this._execute(r,o)},t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!=null&&n>0||n==null&&this.delay>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.flush(this),0)},t}(gt);var Lo=function(e){oe(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t}(yt);var kr=new Lo(Oo);var Mo=function(e){oe(t,e);function t(r,o){var n=e.call(this,r,o)||this;return n.scheduler=r,n.work=o,n}return t.prototype.requestAsyncId=function(r,o,n){return n===void 0&&(n=0),n!==null&&n>0?e.prototype.requestAsyncId.call(this,r,o,n):(r.actions.push(this),r._scheduled||(r._scheduled=vt.requestAnimationFrame(function(){return r.flush(void 0)})))},t.prototype.recycleAsyncId=function(r,o,n){var i;if(n===void 0&&(n=0),n!=null?n>0:this.delay>0)return e.prototype.recycleAsyncId.call(this,r,o,n);var a=r.actions;o!=null&&((i=a[a.length-1])===null||i===void 0?void 0:i.id)!==o&&(vt.cancelAnimationFrame(o),r._scheduled=void 0)},t}(gt);var _o=function(e){oe(t,e);function t(){return e!==null&&e.apply(this,arguments)||this}return t.prototype.flush=function(r){this._active=!0;var o=this._scheduled;this._scheduled=void 0;var n=this.actions,i;r=r||n.shift();do if(i=r.execute(r.state,r.delay))break;while((r=n[0])&&r.id===o&&n.shift());if(this._active=!1,i){for(;(r=n[0])&&r.id===o&&n.shift();)r.unsubscribe();throw i}},t}(yt);var me=new _o(Mo);var S=new j(function(e){return e.complete()});function Yt(e){return e&&H(e.schedule)}function Hr(e){return e[e.length-1]}function Xe(e){return H(Hr(e))?e.pop():void 0}function ke(e){return Yt(Hr(e))?e.pop():void 0}function Bt(e,t){return typeof Hr(e)=="number"?e.pop():t}var xt=function(e){return e&&typeof e.length=="number"&&typeof e!="function"};function Gt(e){return H(e==null?void 0:e.then)}function Jt(e){return H(e[bt])}function Xt(e){return Symbol.asyncIterator&&H(e==null?void 0:e[Symbol.asyncIterator])}function Zt(e){return new TypeError("You provided "+(e!==null&&typeof e=="object"?"an invalid object":"'"+e+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function Zi(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var er=Zi();function tr(e){return H(e==null?void 0:e[er])}function rr(e){return fo(this,arguments,function(){var r,o,n,i;return Nt(this,function(a){switch(a.label){case 0:r=e.getReader(),a.label=1;case 1:a.trys.push([1,,9,10]),a.label=2;case 2:return[4,nt(r.read())];case 3:return o=a.sent(),n=o.value,i=o.done,i?[4,nt(void 0)]:[3,5];case 4:return[2,a.sent()];case 5:return[4,nt(n)];case 6:return[4,a.sent()];case 7:return a.sent(),[3,2];case 8:return[3,10];case 9:return r.releaseLock(),[7];case 10:return[2]}})})}function or(e){return H(e==null?void 0:e.getReader)}function U(e){if(e instanceof j)return e;if(e!=null){if(Jt(e))return ea(e);if(xt(e))return ta(e);if(Gt(e))return ra(e);if(Xt(e))return Ao(e);if(tr(e))return oa(e);if(or(e))return na(e)}throw Zt(e)}function ea(e){return new j(function(t){var r=e[bt]();if(H(r.subscribe))return r.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function ta(e){return new j(function(t){for(var r=0;r=2;return function(o){return o.pipe(e?b(function(n,i){return e(n,i,o)}):le,Te(1),r?De(t):Qo(function(){return new ir}))}}function jr(e){return e<=0?function(){return S}:E(function(t,r){var o=[];t.subscribe(T(r,function(n){o.push(n),e=2,!0))}function pe(e){e===void 0&&(e={});var t=e.connector,r=t===void 0?function(){return new g}:t,o=e.resetOnError,n=o===void 0?!0:o,i=e.resetOnComplete,a=i===void 0?!0:i,s=e.resetOnRefCountZero,p=s===void 0?!0:s;return function(c){var l,f,u,d=0,y=!1,L=!1,X=function(){f==null||f.unsubscribe(),f=void 0},te=function(){X(),l=u=void 0,y=L=!1},J=function(){var k=l;te(),k==null||k.unsubscribe()};return E(function(k,ft){d++,!L&&!y&&X();var qe=u=u!=null?u:r();ft.add(function(){d--,d===0&&!L&&!y&&(f=Ur(J,p))}),qe.subscribe(ft),!l&&d>0&&(l=new at({next:function(Fe){return qe.next(Fe)},error:function(Fe){L=!0,X(),f=Ur(te,n,Fe),qe.error(Fe)},complete:function(){y=!0,X(),f=Ur(te,a),qe.complete()}}),U(k).subscribe(l))})(c)}}function Ur(e,t){for(var r=[],o=2;oe.next(document)),e}function P(e,t=document){return Array.from(t.querySelectorAll(e))}function R(e,t=document){let r=fe(e,t);if(typeof r=="undefined")throw new ReferenceError(`Missing element: expected "${e}" to be present`);return r}function fe(e,t=document){return t.querySelector(e)||void 0}function Ie(){var e,t,r,o;return(o=(r=(t=(e=document.activeElement)==null?void 0:e.shadowRoot)==null?void 0:t.activeElement)!=null?r:document.activeElement)!=null?o:void 0}var wa=O(h(document.body,"focusin"),h(document.body,"focusout")).pipe(_e(1),Q(void 0),m(()=>Ie()||document.body),G(1));function et(e){return wa.pipe(m(t=>e.contains(t)),K())}function $t(e,t){return C(()=>O(h(e,"mouseenter").pipe(m(()=>!0)),h(e,"mouseleave").pipe(m(()=>!1))).pipe(t?Ht(r=>Le(+!r*t)):le,Q(e.matches(":hover"))))}function Jo(e,t){if(typeof t=="string"||typeof t=="number")e.innerHTML+=t.toString();else if(t instanceof Node)e.appendChild(t);else if(Array.isArray(t))for(let r of t)Jo(e,r)}function x(e,t,...r){let o=document.createElement(e);if(t)for(let n of Object.keys(t))typeof t[n]!="undefined"&&(typeof t[n]!="boolean"?o.setAttribute(n,t[n]):o.setAttribute(n,""));for(let n of r)Jo(o,n);return o}function sr(e){if(e>999){let t=+((e-950)%1e3>99);return`${((e+1e-6)/1e3).toFixed(t)}k`}else return e.toString()}function Tt(e){let t=x("script",{src:e});return C(()=>(document.head.appendChild(t),O(h(t,"load"),h(t,"error").pipe(v(()=>$r(()=>new ReferenceError(`Invalid script: ${e}`))))).pipe(m(()=>{}),_(()=>document.head.removeChild(t)),Te(1))))}var Xo=new g,Ta=C(()=>typeof ResizeObserver=="undefined"?Tt("https://unpkg.com/resize-observer-polyfill"):I(void 0)).pipe(m(()=>new ResizeObserver(e=>e.forEach(t=>Xo.next(t)))),v(e=>O(Ye,I(e)).pipe(_(()=>e.disconnect()))),G(1));function ce(e){return{width:e.offsetWidth,height:e.offsetHeight}}function ge(e){let t=e;for(;t.clientWidth===0&&t.parentElement;)t=t.parentElement;return Ta.pipe(w(r=>r.observe(t)),v(r=>Xo.pipe(b(o=>o.target===t),_(()=>r.unobserve(t)))),m(()=>ce(e)),Q(ce(e)))}function St(e){return{width:e.scrollWidth,height:e.scrollHeight}}function cr(e){let t=e.parentElement;for(;t&&(e.scrollWidth<=t.scrollWidth&&e.scrollHeight<=t.scrollHeight);)t=(e=t).parentElement;return t?e:void 0}function Zo(e){let t=[],r=e.parentElement;for(;r;)(e.clientWidth>r.clientWidth||e.clientHeight>r.clientHeight)&&t.push(r),r=(e=r).parentElement;return t.length===0&&t.push(document.documentElement),t}function Ve(e){return{x:e.offsetLeft,y:e.offsetTop}}function en(e){let t=e.getBoundingClientRect();return{x:t.x+window.scrollX,y:t.y+window.scrollY}}function tn(e){return O(h(window,"load"),h(window,"resize")).pipe(Me(0,me),m(()=>Ve(e)),Q(Ve(e)))}function pr(e){return{x:e.scrollLeft,y:e.scrollTop}}function Ne(e){return O(h(e,"scroll"),h(window,"scroll"),h(window,"resize")).pipe(Me(0,me),m(()=>pr(e)),Q(pr(e)))}var rn=new g,Sa=C(()=>I(new IntersectionObserver(e=>{for(let t of e)rn.next(t)},{threshold:0}))).pipe(v(e=>O(Ye,I(e)).pipe(_(()=>e.disconnect()))),G(1));function tt(e){return Sa.pipe(w(t=>t.observe(e)),v(t=>rn.pipe(b(({target:r})=>r===e),_(()=>t.unobserve(e)),m(({isIntersecting:r})=>r))))}function on(e,t=16){return Ne(e).pipe(m(({y:r})=>{let o=ce(e),n=St(e);return r>=n.height-o.height-t}),K())}var lr={drawer:R("[data-md-toggle=drawer]"),search:R("[data-md-toggle=search]")};function nn(e){return lr[e].checked}function Je(e,t){lr[e].checked!==t&&lr[e].click()}function ze(e){let t=lr[e];return h(t,"change").pipe(m(()=>t.checked),Q(t.checked))}function Oa(e,t){switch(e.constructor){case HTMLInputElement:return e.type==="radio"?/^Arrow/.test(t):!0;case HTMLSelectElement:case HTMLTextAreaElement:return!0;default:return e.isContentEditable}}function La(){return O(h(window,"compositionstart").pipe(m(()=>!0)),h(window,"compositionend").pipe(m(()=>!1))).pipe(Q(!1))}function an(){let e=h(window,"keydown").pipe(b(t=>!(t.metaKey||t.ctrlKey)),m(t=>({mode:nn("search")?"search":"global",type:t.key,claim(){t.preventDefault(),t.stopPropagation()}})),b(({mode:t,type:r})=>{if(t==="global"){let o=Ie();if(typeof o!="undefined")return!Oa(o,r)}return!0}),pe());return La().pipe(v(t=>t?S:e))}function ye(){return new URL(location.href)}function lt(e,t=!1){if(B("navigation.instant")&&!t){let r=x("a",{href:e.href});document.body.appendChild(r),r.click(),r.remove()}else location.href=e.href}function sn(){return new g}function cn(){return location.hash.slice(1)}function pn(e){let t=x("a",{href:e});t.addEventListener("click",r=>r.stopPropagation()),t.click()}function Ma(e){return O(h(window,"hashchange"),e).pipe(m(cn),Q(cn()),b(t=>t.length>0),G(1))}function ln(e){return Ma(e).pipe(m(t=>fe(`[id="${t}"]`)),b(t=>typeof t!="undefined"))}function Pt(e){let t=matchMedia(e);return ar(r=>t.addListener(()=>r(t.matches))).pipe(Q(t.matches))}function mn(){let e=matchMedia("print");return O(h(window,"beforeprint").pipe(m(()=>!0)),h(window,"afterprint").pipe(m(()=>!1))).pipe(Q(e.matches))}function Nr(e,t){return e.pipe(v(r=>r?t():S))}function zr(e,t){return new j(r=>{let o=new XMLHttpRequest;return o.open("GET",`${e}`),o.responseType="blob",o.addEventListener("load",()=>{o.status>=200&&o.status<300?(r.next(o.response),r.complete()):r.error(new Error(o.statusText))}),o.addEventListener("error",()=>{r.error(new Error("Network error"))}),o.addEventListener("abort",()=>{r.complete()}),typeof(t==null?void 0:t.progress$)!="undefined"&&(o.addEventListener("progress",n=>{var i;if(n.lengthComputable)t.progress$.next(n.loaded/n.total*100);else{let a=(i=o.getResponseHeader("Content-Length"))!=null?i:0;t.progress$.next(n.loaded/+a*100)}}),t.progress$.next(5)),o.send(),()=>o.abort()})}function je(e,t){return zr(e,t).pipe(v(r=>r.text()),m(r=>JSON.parse(r)),G(1))}function fn(e,t){let r=new DOMParser;return zr(e,t).pipe(v(o=>o.text()),m(o=>r.parseFromString(o,"text/html")),G(1))}function un(e,t){let r=new DOMParser;return zr(e,t).pipe(v(o=>o.text()),m(o=>r.parseFromString(o,"text/xml")),G(1))}function dn(){return{x:Math.max(0,scrollX),y:Math.max(0,scrollY)}}function hn(){return O(h(window,"scroll",{passive:!0}),h(window,"resize",{passive:!0})).pipe(m(dn),Q(dn()))}function bn(){return{width:innerWidth,height:innerHeight}}function vn(){return h(window,"resize",{passive:!0}).pipe(m(bn),Q(bn()))}function gn(){return z([hn(),vn()]).pipe(m(([e,t])=>({offset:e,size:t})),G(1))}function mr(e,{viewport$:t,header$:r}){let o=t.pipe(ee("size")),n=z([o,r]).pipe(m(()=>Ve(e)));return z([r,t,n]).pipe(m(([{height:i},{offset:a,size:s},{x:p,y:c}])=>({offset:{x:a.x-p,y:a.y-c+i},size:s})))}function _a(e){return h(e,"message",t=>t.data)}function Aa(e){let t=new g;return t.subscribe(r=>e.postMessage(r)),t}function yn(e,t=new Worker(e)){let r=_a(t),o=Aa(t),n=new g;n.subscribe(o);let i=o.pipe(Z(),ie(!0));return n.pipe(Z(),Re(r.pipe(W(i))),pe())}var Ca=R("#__config"),Ot=JSON.parse(Ca.textContent);Ot.base=`${new URL(Ot.base,ye())}`;function xe(){return Ot}function B(e){return Ot.features.includes(e)}function Ee(e,t){return typeof t!="undefined"?Ot.translations[e].replace("#",t.toString()):Ot.translations[e]}function Se(e,t=document){return R(`[data-md-component=${e}]`,t)}function ae(e,t=document){return P(`[data-md-component=${e}]`,t)}function ka(e){let t=R(".md-typeset > :first-child",e);return h(t,"click",{once:!0}).pipe(m(()=>R(".md-typeset",e)),m(r=>({hash:__md_hash(r.innerHTML)})))}function xn(e){if(!B("announce.dismiss")||!e.childElementCount)return S;if(!e.hidden){let t=R(".md-typeset",e);__md_hash(t.innerHTML)===__md_get("__announce")&&(e.hidden=!0)}return C(()=>{let t=new g;return t.subscribe(({hash:r})=>{e.hidden=!0,__md_set("__announce",r)}),ka(e).pipe(w(r=>t.next(r)),_(()=>t.complete()),m(r=>$({ref:e},r)))})}function Ha(e,{target$:t}){return t.pipe(m(r=>({hidden:r!==e})))}function En(e,t){let r=new g;return r.subscribe(({hidden:o})=>{e.hidden=o}),Ha(e,t).pipe(w(o=>r.next(o)),_(()=>r.complete()),m(o=>$({ref:e},o)))}function Rt(e,t){return t==="inline"?x("div",{class:"md-tooltip md-tooltip--inline",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"})):x("div",{class:"md-tooltip",id:e,role:"tooltip"},x("div",{class:"md-tooltip__inner md-typeset"}))}function wn(...e){return x("div",{class:"md-tooltip2",role:"tooltip"},x("div",{class:"md-tooltip2__inner md-typeset"},e))}function Tn(e,t){if(t=t?`${t}_annotation_${e}`:void 0,t){let r=t?`#${t}`:void 0;return x("aside",{class:"md-annotation",tabIndex:0},Rt(t),x("a",{href:r,class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}else return x("aside",{class:"md-annotation",tabIndex:0},Rt(t),x("span",{class:"md-annotation__index",tabIndex:-1},x("span",{"data-md-annotation-id":e})))}function Sn(e){return x("button",{class:"md-clipboard md-icon",title:Ee("clipboard.copy"),"data-clipboard-target":`#${e} > code`})}var Ln=Mt(qr());function Qr(e,t){let r=t&2,o=t&1,n=Object.keys(e.terms).filter(p=>!e.terms[p]).reduce((p,c)=>[...p,x("del",null,(0,Ln.default)(c))," "],[]).slice(0,-1),i=xe(),a=new URL(e.location,i.base);B("search.highlight")&&a.searchParams.set("h",Object.entries(e.terms).filter(([,p])=>p).reduce((p,[c])=>`${p} ${c}`.trim(),""));let{tags:s}=xe();return x("a",{href:`${a}`,class:"md-search-result__link",tabIndex:-1},x("article",{class:"md-search-result__article md-typeset","data-md-score":e.score.toFixed(2)},r>0&&x("div",{class:"md-search-result__icon md-icon"}),r>0&&x("h1",null,e.title),r<=0&&x("h2",null,e.title),o>0&&e.text.length>0&&e.text,e.tags&&x("nav",{class:"md-tags"},e.tags.map(p=>{let c=s?p in s?`md-tag-icon md-tag--${s[p]}`:"md-tag-icon":"";return x("span",{class:`md-tag ${c}`},p)})),o>0&&n.length>0&&x("p",{class:"md-search-result__terms"},Ee("search.result.term.missing"),": ",...n)))}function Mn(e){let t=e[0].score,r=[...e],o=xe(),n=r.findIndex(l=>!`${new URL(l.location,o.base)}`.includes("#")),[i]=r.splice(n,1),a=r.findIndex(l=>l.scoreQr(l,1)),...p.length?[x("details",{class:"md-search-result__more"},x("summary",{tabIndex:-1},x("div",null,p.length>0&&p.length===1?Ee("search.result.more.one"):Ee("search.result.more.other",p.length))),...p.map(l=>Qr(l,1)))]:[]];return x("li",{class:"md-search-result__item"},c)}function _n(e){return x("ul",{class:"md-source__facts"},Object.entries(e).map(([t,r])=>x("li",{class:`md-source__fact md-source__fact--${t}`},typeof r=="number"?sr(r):r)))}function Kr(e){let t=`tabbed-control tabbed-control--${e}`;return x("div",{class:t,hidden:!0},x("button",{class:"tabbed-button",tabIndex:-1,"aria-hidden":"true"}))}function An(e){return x("div",{class:"md-typeset__scrollwrap"},x("div",{class:"md-typeset__table"},e))}function Ra(e){var o;let t=xe(),r=new URL(`../${e.version}/`,t.base);return x("li",{class:"md-version__item"},x("a",{href:`${r}`,class:"md-version__link"},e.title,((o=t.version)==null?void 0:o.alias)&&e.aliases.length>0&&x("span",{class:"md-version__alias"},e.aliases[0])))}function Cn(e,t){var o;let r=xe();return e=e.filter(n=>{var i;return!((i=n.properties)!=null&&i.hidden)}),x("div",{class:"md-version"},x("button",{class:"md-version__current","aria-label":Ee("select.version")},t.title,((o=r.version)==null?void 0:o.alias)&&t.aliases.length>0&&x("span",{class:"md-version__alias"},t.aliases[0])),x("ul",{class:"md-version__list"},e.map(Ra)))}var Ia=0;function ja(e){let t=z([et(e),$t(e)]).pipe(m(([o,n])=>o||n),K()),r=C(()=>Zo(e)).pipe(ne(Ne),pt(1),He(t),m(()=>en(e)));return t.pipe(Ae(o=>o),v(()=>z([t,r])),m(([o,n])=>({active:o,offset:n})),pe())}function Fa(e,t){let{content$:r,viewport$:o}=t,n=`__tooltip2_${Ia++}`;return C(()=>{let i=new g,a=new _r(!1);i.pipe(Z(),ie(!1)).subscribe(a);let s=a.pipe(Ht(c=>Le(+!c*250,kr)),K(),v(c=>c?r:S),w(c=>c.id=n),pe());z([i.pipe(m(({active:c})=>c)),s.pipe(v(c=>$t(c,250)),Q(!1))]).pipe(m(c=>c.some(l=>l))).subscribe(a);let p=a.pipe(b(c=>c),re(s,o),m(([c,l,{size:f}])=>{let u=e.getBoundingClientRect(),d=u.width/2;if(l.role==="tooltip")return{x:d,y:8+u.height};if(u.y>=f.height/2){let{height:y}=ce(l);return{x:d,y:-16-y}}else return{x:d,y:16+u.height}}));return z([s,i,p]).subscribe(([c,{offset:l},f])=>{c.style.setProperty("--md-tooltip-host-x",`${l.x}px`),c.style.setProperty("--md-tooltip-host-y",`${l.y}px`),c.style.setProperty("--md-tooltip-x",`${f.x}px`),c.style.setProperty("--md-tooltip-y",`${f.y}px`),c.classList.toggle("md-tooltip2--top",f.y<0),c.classList.toggle("md-tooltip2--bottom",f.y>=0)}),a.pipe(b(c=>c),re(s,(c,l)=>l),b(c=>c.role==="tooltip")).subscribe(c=>{let l=ce(R(":scope > *",c));c.style.setProperty("--md-tooltip-width",`${l.width}px`),c.style.setProperty("--md-tooltip-tail","0px")}),a.pipe(K(),ve(me),re(s)).subscribe(([c,l])=>{l.classList.toggle("md-tooltip2--active",c)}),z([a.pipe(b(c=>c)),s]).subscribe(([c,l])=>{l.role==="dialog"?(e.setAttribute("aria-controls",n),e.setAttribute("aria-haspopup","dialog")):e.setAttribute("aria-describedby",n)}),a.pipe(b(c=>!c)).subscribe(()=>{e.removeAttribute("aria-controls"),e.removeAttribute("aria-describedby"),e.removeAttribute("aria-haspopup")}),ja(e).pipe(w(c=>i.next(c)),_(()=>i.complete()),m(c=>$({ref:e},c)))})}function mt(e,{viewport$:t},r=document.body){return Fa(e,{content$:new j(o=>{let n=e.title,i=wn(n);return o.next(i),e.removeAttribute("title"),r.append(i),()=>{i.remove(),e.setAttribute("title",n)}}),viewport$:t})}function Ua(e,t){let r=C(()=>z([tn(e),Ne(t)])).pipe(m(([{x:o,y:n},i])=>{let{width:a,height:s}=ce(e);return{x:o-i.x+a/2,y:n-i.y+s/2}}));return et(e).pipe(v(o=>r.pipe(m(n=>({active:o,offset:n})),Te(+!o||1/0))))}function kn(e,t,{target$:r}){let[o,n]=Array.from(e.children);return C(()=>{let i=new g,a=i.pipe(Z(),ie(!0));return i.subscribe({next({offset:s}){e.style.setProperty("--md-tooltip-x",`${s.x}px`),e.style.setProperty("--md-tooltip-y",`${s.y}px`)},complete(){e.style.removeProperty("--md-tooltip-x"),e.style.removeProperty("--md-tooltip-y")}}),tt(e).pipe(W(a)).subscribe(s=>{e.toggleAttribute("data-md-visible",s)}),O(i.pipe(b(({active:s})=>s)),i.pipe(_e(250),b(({active:s})=>!s))).subscribe({next({active:s}){s?e.prepend(o):o.remove()},complete(){e.prepend(o)}}),i.pipe(Me(16,me)).subscribe(({active:s})=>{o.classList.toggle("md-tooltip--active",s)}),i.pipe(pt(125,me),b(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:s})=>s)).subscribe({next(s){s?e.style.setProperty("--md-tooltip-0",`${-s}px`):e.style.removeProperty("--md-tooltip-0")},complete(){e.style.removeProperty("--md-tooltip-0")}}),h(n,"click").pipe(W(a),b(s=>!(s.metaKey||s.ctrlKey))).subscribe(s=>{s.stopPropagation(),s.preventDefault()}),h(n,"mousedown").pipe(W(a),re(i)).subscribe(([s,{active:p}])=>{var c;if(s.button!==0||s.metaKey||s.ctrlKey)s.preventDefault();else if(p){s.preventDefault();let l=e.parentElement.closest(".md-annotation");l instanceof HTMLElement?l.focus():(c=Ie())==null||c.blur()}}),r.pipe(W(a),b(s=>s===o),Ge(125)).subscribe(()=>e.focus()),Ua(e,t).pipe(w(s=>i.next(s)),_(()=>i.complete()),m(s=>$({ref:e},s)))})}function Wa(e){return e.tagName==="CODE"?P(".c, .c1, .cm",e):[e]}function Da(e){let t=[];for(let r of Wa(e)){let o=[],n=document.createNodeIterator(r,NodeFilter.SHOW_TEXT);for(let i=n.nextNode();i;i=n.nextNode())o.push(i);for(let i of o){let a;for(;a=/(\(\d+\))(!)?/.exec(i.textContent);){let[,s,p]=a;if(typeof p=="undefined"){let c=i.splitText(a.index);i=c.splitText(s.length),t.push(c)}else{i.textContent=s,t.push(i);break}}}}return t}function Hn(e,t){t.append(...Array.from(e.childNodes))}function fr(e,t,{target$:r,print$:o}){let n=t.closest("[id]"),i=n==null?void 0:n.id,a=new Map;for(let s of Da(t)){let[,p]=s.textContent.match(/\((\d+)\)/);fe(`:scope > li:nth-child(${p})`,e)&&(a.set(p,Tn(p,i)),s.replaceWith(a.get(p)))}return a.size===0?S:C(()=>{let s=new g,p=s.pipe(Z(),ie(!0)),c=[];for(let[l,f]of a)c.push([R(".md-typeset",f),R(`:scope > li:nth-child(${l})`,e)]);return o.pipe(W(p)).subscribe(l=>{e.hidden=!l,e.classList.toggle("md-annotation-list",l);for(let[f,u]of c)l?Hn(f,u):Hn(u,f)}),O(...[...a].map(([,l])=>kn(l,t,{target$:r}))).pipe(_(()=>s.complete()),pe())})}function $n(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return $n(t)}}function Pn(e,t){return C(()=>{let r=$n(e);return typeof r!="undefined"?fr(r,e,t):S})}var Rn=Mt(Br());var Va=0;function In(e){if(e.nextElementSibling){let t=e.nextElementSibling;if(t.tagName==="OL")return t;if(t.tagName==="P"&&!t.children.length)return In(t)}}function Na(e){return ge(e).pipe(m(({width:t})=>({scrollable:St(e).width>t})),ee("scrollable"))}function jn(e,t){let{matches:r}=matchMedia("(hover)"),o=C(()=>{let n=new g,i=n.pipe(jr(1));n.subscribe(({scrollable:c})=>{c&&r?e.setAttribute("tabindex","0"):e.removeAttribute("tabindex")});let a=[];if(Rn.default.isSupported()&&(e.closest(".copy")||B("content.code.copy")&&!e.closest(".no-copy"))){let c=e.closest("pre");c.id=`__code_${Va++}`;let l=Sn(c.id);c.insertBefore(l,e),B("content.tooltips")&&a.push(mt(l,{viewport$}))}let s=e.closest(".highlight");if(s instanceof HTMLElement){let c=In(s);if(typeof c!="undefined"&&(s.classList.contains("annotate")||B("content.code.annotate"))){let l=fr(c,e,t);a.push(ge(s).pipe(W(i),m(({width:f,height:u})=>f&&u),K(),v(f=>f?l:S)))}}return P(":scope > span[id]",e).length&&e.classList.add("md-code__content"),Na(e).pipe(w(c=>n.next(c)),_(()=>n.complete()),m(c=>$({ref:e},c)),Re(...a))});return B("content.lazy")?tt(e).pipe(b(n=>n),Te(1),v(()=>o)):o}function za(e,{target$:t,print$:r}){let o=!0;return O(t.pipe(m(n=>n.closest("details:not([open])")),b(n=>e===n),m(()=>({action:"open",reveal:!0}))),r.pipe(b(n=>n||!o),w(()=>o=e.open),m(n=>({action:n?"open":"close"}))))}function Fn(e,t){return C(()=>{let r=new g;return r.subscribe(({action:o,reveal:n})=>{e.toggleAttribute("open",o==="open"),n&&e.scrollIntoView()}),za(e,t).pipe(w(o=>r.next(o)),_(()=>r.complete()),m(o=>$({ref:e},o)))})}var Un=".node circle,.node ellipse,.node path,.node polygon,.node rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}marker{fill:var(--md-mermaid-edge-color)!important}.edgeLabel .label rect{fill:#0000}.flowchartTitleText{fill:var(--md-mermaid-label-fg-color)}.label{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.label foreignObject{line-height:normal;overflow:visible}.label div .edgeLabel{color:var(--md-mermaid-label-fg-color)}.edgeLabel,.edgeLabel p,.label div .edgeLabel{background-color:var(--md-mermaid-label-bg-color)}.edgeLabel,.edgeLabel p{fill:var(--md-mermaid-label-bg-color);color:var(--md-mermaid-edge-color)}.edgePath .path,.flowchart-link{stroke:var(--md-mermaid-edge-color);stroke-width:.05rem}.edgePath .arrowheadPath{fill:var(--md-mermaid-edge-color);stroke:none}.cluster rect{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}.cluster span{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}g #flowchart-circleEnd,g #flowchart-circleStart,g #flowchart-crossEnd,g #flowchart-crossStart,g #flowchart-pointEnd,g #flowchart-pointStart{stroke:none}.classDiagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.classGroup line,g.classGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.classGroup text{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.classLabel .box{fill:var(--md-mermaid-label-bg-color);background-color:var(--md-mermaid-label-bg-color);opacity:1}.classLabel .label{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.node .divider{stroke:var(--md-mermaid-node-fg-color)}.relation{stroke:var(--md-mermaid-edge-color)}.cardinality{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.cardinality text{fill:inherit!important}defs #classDiagram-compositionEnd,defs #classDiagram-compositionStart,defs #classDiagram-dependencyEnd,defs #classDiagram-dependencyStart,defs #classDiagram-extensionEnd,defs #classDiagram-extensionStart{fill:var(--md-mermaid-edge-color)!important;stroke:var(--md-mermaid-edge-color)!important}defs #classDiagram-aggregationEnd,defs #classDiagram-aggregationStart{fill:var(--md-mermaid-label-bg-color)!important;stroke:var(--md-mermaid-edge-color)!important}.statediagramTitleText{fill:var(--md-mermaid-label-fg-color)}g.stateGroup rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}g.stateGroup .state-title{fill:var(--md-mermaid-label-fg-color)!important;font-family:var(--md-mermaid-font-family)}g.stateGroup .composit{fill:var(--md-mermaid-label-bg-color)}.nodeLabel,.nodeLabel p{color:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}a .nodeLabel{text-decoration:underline}.node circle.state-end,.node circle.state-start,.start-state{fill:var(--md-mermaid-edge-color);stroke:none}.end-state-inner,.end-state-outer{fill:var(--md-mermaid-edge-color)}.end-state-inner,.node circle.state-end{stroke:var(--md-mermaid-label-bg-color)}.transition{stroke:var(--md-mermaid-edge-color)}[id^=state-fork] rect,[id^=state-join] rect{fill:var(--md-mermaid-edge-color)!important;stroke:none!important}.statediagram-cluster.statediagram-cluster .inner{fill:var(--md-default-bg-color)}.statediagram-cluster rect{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}.statediagram-state rect.divider{fill:var(--md-default-fg-color--lightest);stroke:var(--md-default-fg-color--lighter)}defs #statediagram-barbEnd{stroke:var(--md-mermaid-edge-color)}.entityTitleText{fill:var(--md-mermaid-label-fg-color)}.attributeBoxEven,.attributeBoxOdd{fill:var(--md-mermaid-node-bg-color);stroke:var(--md-mermaid-node-fg-color)}.entityBox{fill:var(--md-mermaid-label-bg-color);stroke:var(--md-mermaid-node-fg-color)}.entityLabel{fill:var(--md-mermaid-label-fg-color);font-family:var(--md-mermaid-font-family)}.relationshipLabelBox{fill:var(--md-mermaid-label-bg-color);fill-opacity:1;background-color:var(--md-mermaid-label-bg-color);opacity:1}.relationshipLabel{fill:var(--md-mermaid-label-fg-color)}.relationshipLine{stroke:var(--md-mermaid-edge-color)}defs #ONE_OR_MORE_END *,defs #ONE_OR_MORE_START *,defs #ONLY_ONE_END *,defs #ONLY_ONE_START *,defs #ZERO_OR_MORE_END *,defs #ZERO_OR_MORE_START *,defs #ZERO_OR_ONE_END *,defs #ZERO_OR_ONE_START *{stroke:var(--md-mermaid-edge-color)!important}defs #ZERO_OR_MORE_END circle,defs #ZERO_OR_MORE_START circle{fill:var(--md-mermaid-label-bg-color)}text:not([class]):last-child{fill:var(--md-mermaid-label-fg-color)}.actor{fill:var(--md-mermaid-sequence-actor-bg-color);stroke:var(--md-mermaid-sequence-actor-border-color)}text.actor>tspan{fill:var(--md-mermaid-sequence-actor-fg-color);font-family:var(--md-mermaid-font-family)}line{stroke:var(--md-mermaid-sequence-actor-line-color)}.actor-man circle,.actor-man line{fill:var(--md-mermaid-sequence-actorman-bg-color);stroke:var(--md-mermaid-sequence-actorman-line-color)}.messageLine0,.messageLine1{stroke:var(--md-mermaid-sequence-message-line-color)}.note{fill:var(--md-mermaid-sequence-note-bg-color);stroke:var(--md-mermaid-sequence-note-border-color)}.loopText,.loopText>tspan,.messageText,.noteText>tspan{stroke:none;font-family:var(--md-mermaid-font-family)!important}.messageText{fill:var(--md-mermaid-sequence-message-fg-color)}.loopText,.loopText>tspan{fill:var(--md-mermaid-sequence-loop-fg-color)}.noteText>tspan{fill:var(--md-mermaid-sequence-note-fg-color)}#arrowhead path{fill:var(--md-mermaid-sequence-message-line-color);stroke:none}.loopLine{fill:var(--md-mermaid-sequence-loop-bg-color);stroke:var(--md-mermaid-sequence-loop-border-color)}.labelBox{fill:var(--md-mermaid-sequence-label-bg-color);stroke:none}.labelText,.labelText>span{fill:var(--md-mermaid-sequence-label-fg-color);font-family:var(--md-mermaid-font-family)}.sequenceNumber{fill:var(--md-mermaid-sequence-number-fg-color)}rect.rect{fill:var(--md-mermaid-sequence-box-bg-color);stroke:none}rect.rect+text.text{fill:var(--md-mermaid-sequence-box-fg-color)}defs #sequencenumber{fill:var(--md-mermaid-sequence-number-bg-color)!important}";var Gr,Qa=0;function Ka(){return typeof mermaid=="undefined"||mermaid instanceof Element?Tt("https://unpkg.com/mermaid@11/dist/mermaid.min.js"):I(void 0)}function Wn(e){return e.classList.remove("mermaid"),Gr||(Gr=Ka().pipe(w(()=>mermaid.initialize({startOnLoad:!1,themeCSS:Un,sequence:{actorFontSize:"16px",messageFontSize:"16px",noteFontSize:"16px"}})),m(()=>{}),G(1))),Gr.subscribe(()=>co(this,null,function*(){e.classList.add("mermaid");let t=`__mermaid_${Qa++}`,r=x("div",{class:"mermaid"}),o=e.textContent,{svg:n,fn:i}=yield mermaid.render(t,o),a=r.attachShadow({mode:"closed"});a.innerHTML=n,e.replaceWith(r),i==null||i(a)})),Gr.pipe(m(()=>({ref:e})))}var Dn=x("table");function Vn(e){return e.replaceWith(Dn),Dn.replaceWith(An(e)),I({ref:e})}function Ya(e){let t=e.find(r=>r.checked)||e[0];return O(...e.map(r=>h(r,"change").pipe(m(()=>R(`label[for="${r.id}"]`))))).pipe(Q(R(`label[for="${t.id}"]`)),m(r=>({active:r})))}function Nn(e,{viewport$:t,target$:r}){let o=R(".tabbed-labels",e),n=P(":scope > input",e),i=Kr("prev");e.append(i);let a=Kr("next");return e.append(a),C(()=>{let s=new g,p=s.pipe(Z(),ie(!0));z([s,ge(e),tt(e)]).pipe(W(p),Me(1,me)).subscribe({next([{active:c},l]){let f=Ve(c),{width:u}=ce(c);e.style.setProperty("--md-indicator-x",`${f.x}px`),e.style.setProperty("--md-indicator-width",`${u}px`);let d=pr(o);(f.xd.x+l.width)&&o.scrollTo({left:Math.max(0,f.x-16),behavior:"smooth"})},complete(){e.style.removeProperty("--md-indicator-x"),e.style.removeProperty("--md-indicator-width")}}),z([Ne(o),ge(o)]).pipe(W(p)).subscribe(([c,l])=>{let f=St(o);i.hidden=c.x<16,a.hidden=c.x>f.width-l.width-16}),O(h(i,"click").pipe(m(()=>-1)),h(a,"click").pipe(m(()=>1))).pipe(W(p)).subscribe(c=>{let{width:l}=ce(o);o.scrollBy({left:l*c,behavior:"smooth"})}),r.pipe(W(p),b(c=>n.includes(c))).subscribe(c=>c.click()),o.classList.add("tabbed-labels--linked");for(let c of n){let l=R(`label[for="${c.id}"]`);l.replaceChildren(x("a",{href:`#${l.htmlFor}`,tabIndex:-1},...Array.from(l.childNodes))),h(l.firstElementChild,"click").pipe(W(p),b(f=>!(f.metaKey||f.ctrlKey)),w(f=>{f.preventDefault(),f.stopPropagation()})).subscribe(()=>{history.replaceState({},"",`#${l.htmlFor}`),l.click()})}return B("content.tabs.link")&&s.pipe(Ce(1),re(t)).subscribe(([{active:c},{offset:l}])=>{let f=c.innerText.trim();if(c.hasAttribute("data-md-switching"))c.removeAttribute("data-md-switching");else{let u=e.offsetTop-l.y;for(let y of P("[data-tabs]"))for(let L of P(":scope > input",y)){let X=R(`label[for="${L.id}"]`);if(X!==c&&X.innerText.trim()===f){X.setAttribute("data-md-switching",""),L.click();break}}window.scrollTo({top:e.offsetTop-u});let d=__md_get("__tabs")||[];__md_set("__tabs",[...new Set([f,...d])])}}),s.pipe(W(p)).subscribe(()=>{for(let c of P("audio, video",e))c.pause()}),Ya(n).pipe(w(c=>s.next(c)),_(()=>s.complete()),m(c=>$({ref:e},c)))}).pipe(Ke(se))}function zn(e,{viewport$:t,target$:r,print$:o}){return O(...P(".annotate:not(.highlight)",e).map(n=>Pn(n,{target$:r,print$:o})),...P("pre:not(.mermaid) > code",e).map(n=>jn(n,{target$:r,print$:o})),...P("pre.mermaid",e).map(n=>Wn(n)),...P("table:not([class])",e).map(n=>Vn(n)),...P("details",e).map(n=>Fn(n,{target$:r,print$:o})),...P("[data-tabs]",e).map(n=>Nn(n,{viewport$:t,target$:r})),...P("[title]",e).filter(()=>B("content.tooltips")).map(n=>mt(n,{viewport$:t})))}function Ba(e,{alert$:t}){return t.pipe(v(r=>O(I(!0),I(!1).pipe(Ge(2e3))).pipe(m(o=>({message:r,active:o})))))}function qn(e,t){let r=R(".md-typeset",e);return C(()=>{let o=new g;return o.subscribe(({message:n,active:i})=>{e.classList.toggle("md-dialog--active",i),r.textContent=n}),Ba(e,t).pipe(w(n=>o.next(n)),_(()=>o.complete()),m(n=>$({ref:e},n)))})}var Ga=0;function Ja(e,t){document.body.append(e);let{width:r}=ce(e);e.style.setProperty("--md-tooltip-width",`${r}px`),e.remove();let o=cr(t),n=typeof o!="undefined"?Ne(o):I({x:0,y:0}),i=O(et(t),$t(t)).pipe(K());return z([i,n]).pipe(m(([a,s])=>{let{x:p,y:c}=Ve(t),l=ce(t),f=t.closest("table");return f&&t.parentElement&&(p+=f.offsetLeft+t.parentElement.offsetLeft,c+=f.offsetTop+t.parentElement.offsetTop),{active:a,offset:{x:p-s.x+l.width/2-r/2,y:c-s.y+l.height+8}}}))}function Qn(e){let t=e.title;if(!t.length)return S;let r=`__tooltip_${Ga++}`,o=Rt(r,"inline"),n=R(".md-typeset",o);return n.innerHTML=t,C(()=>{let i=new g;return i.subscribe({next({offset:a}){o.style.setProperty("--md-tooltip-x",`${a.x}px`),o.style.setProperty("--md-tooltip-y",`${a.y}px`)},complete(){o.style.removeProperty("--md-tooltip-x"),o.style.removeProperty("--md-tooltip-y")}}),O(i.pipe(b(({active:a})=>a)),i.pipe(_e(250),b(({active:a})=>!a))).subscribe({next({active:a}){a?(e.insertAdjacentElement("afterend",o),e.setAttribute("aria-describedby",r),e.removeAttribute("title")):(o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t))},complete(){o.remove(),e.removeAttribute("aria-describedby"),e.setAttribute("title",t)}}),i.pipe(Me(16,me)).subscribe(({active:a})=>{o.classList.toggle("md-tooltip--active",a)}),i.pipe(pt(125,me),b(()=>!!e.offsetParent),m(()=>e.offsetParent.getBoundingClientRect()),m(({x:a})=>a)).subscribe({next(a){a?o.style.setProperty("--md-tooltip-0",`${-a}px`):o.style.removeProperty("--md-tooltip-0")},complete(){o.style.removeProperty("--md-tooltip-0")}}),Ja(o,e).pipe(w(a=>i.next(a)),_(()=>i.complete()),m(a=>$({ref:e},a)))}).pipe(Ke(se))}function Xa({viewport$:e}){if(!B("header.autohide"))return I(!1);let t=e.pipe(m(({offset:{y:n}})=>n),Be(2,1),m(([n,i])=>[nMath.abs(i-n.y)>100),m(([,[n]])=>n),K()),o=ze("search");return z([e,o]).pipe(m(([{offset:n},i])=>n.y>400&&!i),K(),v(n=>n?r:I(!1)),Q(!1))}function Kn(e,t){return C(()=>z([ge(e),Xa(t)])).pipe(m(([{height:r},o])=>({height:r,hidden:o})),K((r,o)=>r.height===o.height&&r.hidden===o.hidden),G(1))}function Yn(e,{header$:t,main$:r}){return C(()=>{let o=new g,n=o.pipe(Z(),ie(!0));o.pipe(ee("active"),He(t)).subscribe(([{active:a},{hidden:s}])=>{e.classList.toggle("md-header--shadow",a&&!s),e.hidden=s});let i=ue(P("[title]",e)).pipe(b(()=>B("content.tooltips")),ne(a=>Qn(a)));return r.subscribe(o),t.pipe(W(n),m(a=>$({ref:e},a)),Re(i.pipe(W(n))))})}function Za(e,{viewport$:t,header$:r}){return mr(e,{viewport$:t,header$:r}).pipe(m(({offset:{y:o}})=>{let{height:n}=ce(e);return{active:o>=n}}),ee("active"))}function Bn(e,t){return C(()=>{let r=new g;r.subscribe({next({active:n}){e.classList.toggle("md-header__title--active",n)},complete(){e.classList.remove("md-header__title--active")}});let o=fe(".md-content h1");return typeof o=="undefined"?S:Za(o,t).pipe(w(n=>r.next(n)),_(()=>r.complete()),m(n=>$({ref:e},n)))})}function Gn(e,{viewport$:t,header$:r}){let o=r.pipe(m(({height:i})=>i),K()),n=o.pipe(v(()=>ge(e).pipe(m(({height:i})=>({top:e.offsetTop,bottom:e.offsetTop+i})),ee("bottom"))));return z([o,n,t]).pipe(m(([i,{top:a,bottom:s},{offset:{y:p},size:{height:c}}])=>(c=Math.max(0,c-Math.max(0,a-p,i)-Math.max(0,c+p-s)),{offset:a-i,height:c,active:a-i<=p})),K((i,a)=>i.offset===a.offset&&i.height===a.height&&i.active===a.active))}function es(e){let t=__md_get("__palette")||{index:e.findIndex(o=>matchMedia(o.getAttribute("data-md-color-media")).matches)},r=Math.max(0,Math.min(t.index,e.length-1));return I(...e).pipe(ne(o=>h(o,"change").pipe(m(()=>o))),Q(e[r]),m(o=>({index:e.indexOf(o),color:{media:o.getAttribute("data-md-color-media"),scheme:o.getAttribute("data-md-color-scheme"),primary:o.getAttribute("data-md-color-primary"),accent:o.getAttribute("data-md-color-accent")}})),G(1))}function Jn(e){let t=P("input",e),r=x("meta",{name:"theme-color"});document.head.appendChild(r);let o=x("meta",{name:"color-scheme"});document.head.appendChild(o);let n=Pt("(prefers-color-scheme: light)");return C(()=>{let i=new g;return i.subscribe(a=>{if(document.body.setAttribute("data-md-color-switching",""),a.color.media==="(prefers-color-scheme)"){let s=matchMedia("(prefers-color-scheme: light)"),p=document.querySelector(s.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");a.color.scheme=p.getAttribute("data-md-color-scheme"),a.color.primary=p.getAttribute("data-md-color-primary"),a.color.accent=p.getAttribute("data-md-color-accent")}for(let[s,p]of Object.entries(a.color))document.body.setAttribute(`data-md-color-${s}`,p);for(let s=0;sa.key==="Enter"),re(i,(a,s)=>s)).subscribe(({index:a})=>{a=(a+1)%t.length,t[a].click(),t[a].focus()}),i.pipe(m(()=>{let a=Se("header"),s=window.getComputedStyle(a);return o.content=s.colorScheme,s.backgroundColor.match(/\d+/g).map(p=>(+p).toString(16).padStart(2,"0")).join("")})).subscribe(a=>r.content=`#${a}`),i.pipe(ve(se)).subscribe(()=>{document.body.removeAttribute("data-md-color-switching")}),es(t).pipe(W(n.pipe(Ce(1))),ct(),w(a=>i.next(a)),_(()=>i.complete()),m(a=>$({ref:e},a)))})}function Xn(e,{progress$:t}){return C(()=>{let r=new g;return r.subscribe(({value:o})=>{e.style.setProperty("--md-progress-value",`${o}`)}),t.pipe(w(o=>r.next({value:o})),_(()=>r.complete()),m(o=>({ref:e,value:o})))})}var Jr=Mt(Br());function ts(e){e.setAttribute("data-md-copying","");let t=e.closest("[data-copy]"),r=t?t.getAttribute("data-copy"):e.innerText;return e.removeAttribute("data-md-copying"),r.trimEnd()}function Zn({alert$:e}){Jr.default.isSupported()&&new j(t=>{new Jr.default("[data-clipboard-target], [data-clipboard-text]",{text:r=>r.getAttribute("data-clipboard-text")||ts(R(r.getAttribute("data-clipboard-target")))}).on("success",r=>t.next(r))}).pipe(w(t=>{t.trigger.focus()}),m(()=>Ee("clipboard.copied"))).subscribe(e)}function ei(e,t){return e.protocol=t.protocol,e.hostname=t.hostname,e}function rs(e,t){let r=new Map;for(let o of P("url",e)){let n=R("loc",o),i=[ei(new URL(n.textContent),t)];r.set(`${i[0]}`,i);for(let a of P("[rel=alternate]",o)){let s=a.getAttribute("href");s!=null&&i.push(ei(new URL(s),t))}}return r}function ur(e){return un(new URL("sitemap.xml",e)).pipe(m(t=>rs(t,new URL(e))),de(()=>I(new Map)))}function os(e,t){if(!(e.target instanceof Element))return S;let r=e.target.closest("a");if(r===null)return S;if(r.target||e.metaKey||e.ctrlKey)return S;let o=new URL(r.href);return o.search=o.hash="",t.has(`${o}`)?(e.preventDefault(),I(new URL(r.href))):S}function ti(e){let t=new Map;for(let r of P(":scope > *",e.head))t.set(r.outerHTML,r);return t}function ri(e){for(let t of P("[href], [src]",e))for(let r of["href","src"]){let o=t.getAttribute(r);if(o&&!/^(?:[a-z]+:)?\/\//i.test(o)){t[r]=t[r];break}}return I(e)}function ns(e){for(let o of["[data-md-component=announce]","[data-md-component=container]","[data-md-component=header-topic]","[data-md-component=outdated]","[data-md-component=logo]","[data-md-component=skip]",...B("navigation.tabs.sticky")?["[data-md-component=tabs]"]:[]]){let n=fe(o),i=fe(o,e);typeof n!="undefined"&&typeof i!="undefined"&&n.replaceWith(i)}let t=ti(document);for(let[o,n]of ti(e))t.has(o)?t.delete(o):document.head.appendChild(n);for(let o of t.values()){let n=o.getAttribute("name");n!=="theme-color"&&n!=="color-scheme"&&o.remove()}let r=Se("container");return We(P("script",r)).pipe(v(o=>{let n=e.createElement("script");if(o.src){for(let i of o.getAttributeNames())n.setAttribute(i,o.getAttribute(i));return o.replaceWith(n),new j(i=>{n.onload=()=>i.complete()})}else return n.textContent=o.textContent,o.replaceWith(n),S}),Z(),ie(document))}function oi({location$:e,viewport$:t,progress$:r}){let o=xe();if(location.protocol==="file:")return S;let n=ur(o.base);I(document).subscribe(ri);let i=h(document.body,"click").pipe(He(n),v(([p,c])=>os(p,c)),pe()),a=h(window,"popstate").pipe(m(ye),pe());i.pipe(re(t)).subscribe(([p,{offset:c}])=>{history.replaceState(c,""),history.pushState(null,"",p)}),O(i,a).subscribe(e);let s=e.pipe(ee("pathname"),v(p=>fn(p,{progress$:r}).pipe(de(()=>(lt(p,!0),S)))),v(ri),v(ns),pe());return O(s.pipe(re(e,(p,c)=>c)),s.pipe(v(()=>e),ee("pathname"),v(()=>e),ee("hash")),e.pipe(K((p,c)=>p.pathname===c.pathname&&p.hash===c.hash),v(()=>i),w(()=>history.back()))).subscribe(p=>{var c,l;history.state!==null||!p.hash?window.scrollTo(0,(l=(c=history.state)==null?void 0:c.y)!=null?l:0):(history.scrollRestoration="auto",pn(p.hash),history.scrollRestoration="manual")}),e.subscribe(()=>{history.scrollRestoration="manual"}),h(window,"beforeunload").subscribe(()=>{history.scrollRestoration="auto"}),t.pipe(ee("offset"),_e(100)).subscribe(({offset:p})=>{history.replaceState(p,"")}),s}var ni=Mt(qr());function ii(e){let t=e.separator.split("|").map(n=>n.replace(/(\(\?[!=<][^)]+\))/g,"").length===0?"\uFFFD":n).join("|"),r=new RegExp(t,"img"),o=(n,i,a)=>`${i}${a}`;return n=>{n=n.replace(/[\s*+\-:~^]+/g," ").trim();let i=new RegExp(`(^|${e.separator}|)(${n.replace(/[|\\{}()[\]^$+*?.-]/g,"\\$&").replace(r,"|")})`,"img");return a=>(0,ni.default)(a).replace(i,o).replace(/<\/mark>(\s+)]*>/img,"$1")}}function jt(e){return e.type===1}function dr(e){return e.type===3}function ai(e,t){let r=yn(e);return O(I(location.protocol!=="file:"),ze("search")).pipe(Ae(o=>o),v(()=>t)).subscribe(({config:o,docs:n})=>r.next({type:0,data:{config:o,docs:n,options:{suggest:B("search.suggest")}}})),r}function si(e){var l;let{selectedVersionSitemap:t,selectedVersionBaseURL:r,currentLocation:o,currentBaseURL:n}=e,i=(l=Xr(n))==null?void 0:l.pathname;if(i===void 0)return;let a=ss(o.pathname,i);if(a===void 0)return;let s=ps(t.keys());if(!t.has(s))return;let p=Xr(a,s);if(!p||!t.has(p.href))return;let c=Xr(a,r);if(c)return c.hash=o.hash,c.search=o.search,c}function Xr(e,t){try{return new URL(e,t)}catch(r){return}}function ss(e,t){if(e.startsWith(t))return e.slice(t.length)}function cs(e,t){let r=Math.min(e.length,t.length),o;for(o=0;oS)),o=r.pipe(m(n=>{let[,i]=t.base.match(/([^/]+)\/?$/);return n.find(({version:a,aliases:s})=>a===i||s.includes(i))||n[0]}));r.pipe(m(n=>new Map(n.map(i=>[`${new URL(`../${i.version}/`,t.base)}`,i]))),v(n=>h(document.body,"click").pipe(b(i=>!i.metaKey&&!i.ctrlKey),re(o),v(([i,a])=>{if(i.target instanceof Element){let s=i.target.closest("a");if(s&&!s.target&&n.has(s.href)){let p=s.href;return!i.target.closest(".md-version")&&n.get(p)===a?S:(i.preventDefault(),I(new URL(p)))}}return S}),v(i=>ur(i).pipe(m(a=>{var s;return(s=si({selectedVersionSitemap:a,selectedVersionBaseURL:i,currentLocation:ye(),currentBaseURL:t.base}))!=null?s:i})))))).subscribe(n=>lt(n,!0)),z([r,o]).subscribe(([n,i])=>{R(".md-header__topic").appendChild(Cn(n,i))}),e.pipe(v(()=>o)).subscribe(n=>{var a;let i=__md_get("__outdated",sessionStorage);if(i===null){i=!0;let s=((a=t.version)==null?void 0:a.default)||"latest";Array.isArray(s)||(s=[s]);e:for(let p of s)for(let c of n.aliases.concat(n.version))if(new RegExp(p,"i").test(c)){i=!1;break e}__md_set("__outdated",i,sessionStorage)}if(i)for(let s of ae("outdated"))s.hidden=!1})}function ls(e,{worker$:t}){let{searchParams:r}=ye();r.has("q")&&(Je("search",!0),e.value=r.get("q"),e.focus(),ze("search").pipe(Ae(i=>!i)).subscribe(()=>{let i=ye();i.searchParams.delete("q"),history.replaceState({},"",`${i}`)}));let o=et(e),n=O(t.pipe(Ae(jt)),h(e,"keyup"),o).pipe(m(()=>e.value),K());return z([n,o]).pipe(m(([i,a])=>({value:i,focus:a})),G(1))}function pi(e,{worker$:t}){let r=new g,o=r.pipe(Z(),ie(!0));z([t.pipe(Ae(jt)),r],(i,a)=>a).pipe(ee("value")).subscribe(({value:i})=>t.next({type:2,data:i})),r.pipe(ee("focus")).subscribe(({focus:i})=>{i&&Je("search",i)}),h(e.form,"reset").pipe(W(o)).subscribe(()=>e.focus());let n=R("header [for=__search]");return h(n,"click").subscribe(()=>e.focus()),ls(e,{worker$:t}).pipe(w(i=>r.next(i)),_(()=>r.complete()),m(i=>$({ref:e},i)),G(1))}function li(e,{worker$:t,query$:r}){let o=new g,n=on(e.parentElement).pipe(b(Boolean)),i=e.parentElement,a=R(":scope > :first-child",e),s=R(":scope > :last-child",e);ze("search").subscribe(l=>s.setAttribute("role",l?"list":"presentation")),o.pipe(re(r),Wr(t.pipe(Ae(jt)))).subscribe(([{items:l},{value:f}])=>{switch(l.length){case 0:a.textContent=f.length?Ee("search.result.none"):Ee("search.result.placeholder");break;case 1:a.textContent=Ee("search.result.one");break;default:let u=sr(l.length);a.textContent=Ee("search.result.other",u)}});let p=o.pipe(w(()=>s.innerHTML=""),v(({items:l})=>O(I(...l.slice(0,10)),I(...l.slice(10)).pipe(Be(4),Vr(n),v(([f])=>f)))),m(Mn),pe());return p.subscribe(l=>s.appendChild(l)),p.pipe(ne(l=>{let f=fe("details",l);return typeof f=="undefined"?S:h(f,"toggle").pipe(W(o),m(()=>f))})).subscribe(l=>{l.open===!1&&l.offsetTop<=i.scrollTop&&i.scrollTo({top:l.offsetTop})}),t.pipe(b(dr),m(({data:l})=>l)).pipe(w(l=>o.next(l)),_(()=>o.complete()),m(l=>$({ref:e},l)))}function ms(e,{query$:t}){return t.pipe(m(({value:r})=>{let o=ye();return o.hash="",r=r.replace(/\s+/g,"+").replace(/&/g,"%26").replace(/=/g,"%3D"),o.search=`q=${r}`,{url:o}}))}function mi(e,t){let r=new g,o=r.pipe(Z(),ie(!0));return r.subscribe(({url:n})=>{e.setAttribute("data-clipboard-text",e.href),e.href=`${n}`}),h(e,"click").pipe(W(o)).subscribe(n=>n.preventDefault()),ms(e,t).pipe(w(n=>r.next(n)),_(()=>r.complete()),m(n=>$({ref:e},n)))}function fi(e,{worker$:t,keyboard$:r}){let o=new g,n=Se("search-query"),i=O(h(n,"keydown"),h(n,"focus")).pipe(ve(se),m(()=>n.value),K());return o.pipe(He(i),m(([{suggest:s},p])=>{let c=p.split(/([\s-]+)/);if(s!=null&&s.length&&c[c.length-1]){let l=s[s.length-1];l.startsWith(c[c.length-1])&&(c[c.length-1]=l)}else c.length=0;return c})).subscribe(s=>e.innerHTML=s.join("").replace(/\s/g," ")),r.pipe(b(({mode:s})=>s==="search")).subscribe(s=>{switch(s.type){case"ArrowRight":e.innerText.length&&n.selectionStart===n.value.length&&(n.value=e.innerText);break}}),t.pipe(b(dr),m(({data:s})=>s)).pipe(w(s=>o.next(s)),_(()=>o.complete()),m(()=>({ref:e})))}function ui(e,{index$:t,keyboard$:r}){let o=xe();try{let n=ai(o.search,t),i=Se("search-query",e),a=Se("search-result",e);h(e,"click").pipe(b(({target:p})=>p instanceof Element&&!!p.closest("a"))).subscribe(()=>Je("search",!1)),r.pipe(b(({mode:p})=>p==="search")).subscribe(p=>{let c=Ie();switch(p.type){case"Enter":if(c===i){let l=new Map;for(let f of P(":first-child [href]",a)){let u=f.firstElementChild;l.set(f,parseFloat(u.getAttribute("data-md-score")))}if(l.size){let[[f]]=[...l].sort(([,u],[,d])=>d-u);f.click()}p.claim()}break;case"Escape":case"Tab":Je("search",!1),i.blur();break;case"ArrowUp":case"ArrowDown":if(typeof c=="undefined")i.focus();else{let l=[i,...P(":not(details) > [href], summary, details[open] [href]",a)],f=Math.max(0,(Math.max(0,l.indexOf(c))+l.length+(p.type==="ArrowUp"?-1:1))%l.length);l[f].focus()}p.claim();break;default:i!==Ie()&&i.focus()}}),r.pipe(b(({mode:p})=>p==="global")).subscribe(p=>{switch(p.type){case"f":case"s":case"/":i.focus(),i.select(),p.claim();break}});let s=pi(i,{worker$:n});return O(s,li(a,{worker$:n,query$:s})).pipe(Re(...ae("search-share",e).map(p=>mi(p,{query$:s})),...ae("search-suggest",e).map(p=>fi(p,{worker$:n,keyboard$:r}))))}catch(n){return e.hidden=!0,Ye}}function di(e,{index$:t,location$:r}){return z([t,r.pipe(Q(ye()),b(o=>!!o.searchParams.get("h")))]).pipe(m(([o,n])=>ii(o.config)(n.searchParams.get("h"))),m(o=>{var a;let n=new Map,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);for(let s=i.nextNode();s;s=i.nextNode())if((a=s.parentElement)!=null&&a.offsetHeight){let p=s.textContent,c=o(p);c.length>p.length&&n.set(s,c)}for(let[s,p]of n){let{childNodes:c}=x("span",null,p);s.replaceWith(...Array.from(c))}return{ref:e,nodes:n}}))}function fs(e,{viewport$:t,main$:r}){let o=e.closest(".md-grid"),n=o.offsetTop-o.parentElement.offsetTop;return z([r,t]).pipe(m(([{offset:i,height:a},{offset:{y:s}}])=>(a=a+Math.min(n,Math.max(0,s-i))-n,{height:a,locked:s>=i+n})),K((i,a)=>i.height===a.height&&i.locked===a.locked))}function Zr(e,o){var n=o,{header$:t}=n,r=so(n,["header$"]);let i=R(".md-sidebar__scrollwrap",e),{y:a}=Ve(i);return C(()=>{let s=new g,p=s.pipe(Z(),ie(!0)),c=s.pipe(Me(0,me));return c.pipe(re(t)).subscribe({next([{height:l},{height:f}]){i.style.height=`${l-2*a}px`,e.style.top=`${f}px`},complete(){i.style.height="",e.style.top=""}}),c.pipe(Ae()).subscribe(()=>{for(let l of P(".md-nav__link--active[href]",e)){if(!l.clientHeight)continue;let f=l.closest(".md-sidebar__scrollwrap");if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=ce(f);f.scrollTo({top:u-d/2})}}}),ue(P("label[tabindex]",e)).pipe(ne(l=>h(l,"click").pipe(ve(se),m(()=>l),W(p)))).subscribe(l=>{let f=R(`[id="${l.htmlFor}"]`);R(`[aria-labelledby="${l.id}"]`).setAttribute("aria-expanded",`${f.checked}`)}),fs(e,r).pipe(w(l=>s.next(l)),_(()=>s.complete()),m(l=>$({ref:e},l)))})}function hi(e,t){if(typeof t!="undefined"){let r=`https://api.github.com/repos/${e}/${t}`;return st(je(`${r}/releases/latest`).pipe(de(()=>S),m(o=>({version:o.tag_name})),De({})),je(r).pipe(de(()=>S),m(o=>({stars:o.stargazers_count,forks:o.forks_count})),De({}))).pipe(m(([o,n])=>$($({},o),n)))}else{let r=`https://api.github.com/users/${e}`;return je(r).pipe(m(o=>({repositories:o.public_repos})),De({}))}}function bi(e,t){let r=`https://${e}/api/v4/projects/${encodeURIComponent(t)}`;return st(je(`${r}/releases/permalink/latest`).pipe(de(()=>S),m(({tag_name:o})=>({version:o})),De({})),je(r).pipe(de(()=>S),m(({star_count:o,forks_count:n})=>({stars:o,forks:n})),De({}))).pipe(m(([o,n])=>$($({},o),n)))}function vi(e){let t=e.match(/^.+github\.com\/([^/]+)\/?([^/]+)?/i);if(t){let[,r,o]=t;return hi(r,o)}if(t=e.match(/^.+?([^/]*gitlab[^/]+)\/(.+?)\/?$/i),t){let[,r,o]=t;return bi(r,o)}return S}var us;function ds(e){return us||(us=C(()=>{let t=__md_get("__source",sessionStorage);if(t)return I(t);if(ae("consent").length){let o=__md_get("__consent");if(!(o&&o.github))return S}return vi(e.href).pipe(w(o=>__md_set("__source",o,sessionStorage)))}).pipe(de(()=>S),b(t=>Object.keys(t).length>0),m(t=>({facts:t})),G(1)))}function gi(e){let t=R(":scope > :last-child",e);return C(()=>{let r=new g;return r.subscribe(({facts:o})=>{t.appendChild(_n(o)),t.classList.add("md-source__repository--active")}),ds(e).pipe(w(o=>r.next(o)),_(()=>r.complete()),m(o=>$({ref:e},o)))})}function hs(e,{viewport$:t,header$:r}){return ge(document.body).pipe(v(()=>mr(e,{header$:r,viewport$:t})),m(({offset:{y:o}})=>({hidden:o>=10})),ee("hidden"))}function yi(e,t){return C(()=>{let r=new g;return r.subscribe({next({hidden:o}){e.hidden=o},complete(){e.hidden=!1}}),(B("navigation.tabs.sticky")?I({hidden:!1}):hs(e,t)).pipe(w(o=>r.next(o)),_(()=>r.complete()),m(o=>$({ref:e},o)))})}function bs(e,{viewport$:t,header$:r}){let o=new Map,n=P(".md-nav__link",e);for(let s of n){let p=decodeURIComponent(s.hash.substring(1)),c=fe(`[id="${p}"]`);typeof c!="undefined"&&o.set(s,c)}let i=r.pipe(ee("height"),m(({height:s})=>{let p=Se("main"),c=R(":scope > :first-child",p);return s+.8*(c.offsetTop-p.offsetTop)}),pe());return ge(document.body).pipe(ee("height"),v(s=>C(()=>{let p=[];return I([...o].reduce((c,[l,f])=>{for(;p.length&&o.get(p[p.length-1]).tagName>=f.tagName;)p.pop();let u=f.offsetTop;for(;!u&&f.parentElement;)f=f.parentElement,u=f.offsetTop;let d=f.offsetParent;for(;d;d=d.offsetParent)u+=d.offsetTop;return c.set([...p=[...p,l]].reverse(),u)},new Map))}).pipe(m(p=>new Map([...p].sort(([,c],[,l])=>c-l))),He(i),v(([p,c])=>t.pipe(Fr(([l,f],{offset:{y:u},size:d})=>{let y=u+d.height>=Math.floor(s.height);for(;f.length;){let[,L]=f[0];if(L-c=u&&!y)f=[l.pop(),...f];else break}return[l,f]},[[],[...p]]),K((l,f)=>l[0]===f[0]&&l[1]===f[1])))))).pipe(m(([s,p])=>({prev:s.map(([c])=>c),next:p.map(([c])=>c)})),Q({prev:[],next:[]}),Be(2,1),m(([s,p])=>s.prev.length{let i=new g,a=i.pipe(Z(),ie(!0));if(i.subscribe(({prev:s,next:p})=>{for(let[c]of p)c.classList.remove("md-nav__link--passed"),c.classList.remove("md-nav__link--active");for(let[c,[l]]of s.entries())l.classList.add("md-nav__link--passed"),l.classList.toggle("md-nav__link--active",c===s.length-1)}),B("toc.follow")){let s=O(t.pipe(_e(1),m(()=>{})),t.pipe(_e(250),m(()=>"smooth")));i.pipe(b(({prev:p})=>p.length>0),He(o.pipe(ve(se))),re(s)).subscribe(([[{prev:p}],c])=>{let[l]=p[p.length-1];if(l.offsetHeight){let f=cr(l);if(typeof f!="undefined"){let u=l.offsetTop-f.offsetTop,{height:d}=ce(f);f.scrollTo({top:u-d/2,behavior:c})}}})}return B("navigation.tracking")&&t.pipe(W(a),ee("offset"),_e(250),Ce(1),W(n.pipe(Ce(1))),ct({delay:250}),re(i)).subscribe(([,{prev:s}])=>{let p=ye(),c=s[s.length-1];if(c&&c.length){let[l]=c,{hash:f}=new URL(l.href);p.hash!==f&&(p.hash=f,history.replaceState({},"",`${p}`))}else p.hash="",history.replaceState({},"",`${p}`)}),bs(e,{viewport$:t,header$:r}).pipe(w(s=>i.next(s)),_(()=>i.complete()),m(s=>$({ref:e},s)))})}function vs(e,{viewport$:t,main$:r,target$:o}){let n=t.pipe(m(({offset:{y:a}})=>a),Be(2,1),m(([a,s])=>a>s&&s>0),K()),i=r.pipe(m(({active:a})=>a));return z([i,n]).pipe(m(([a,s])=>!(a&&s)),K(),W(o.pipe(Ce(1))),ie(!0),ct({delay:250}),m(a=>({hidden:a})))}function Ei(e,{viewport$:t,header$:r,main$:o,target$:n}){let i=new g,a=i.pipe(Z(),ie(!0));return i.subscribe({next({hidden:s}){e.hidden=s,s?(e.setAttribute("tabindex","-1"),e.blur()):e.removeAttribute("tabindex")},complete(){e.style.top="",e.hidden=!0,e.removeAttribute("tabindex")}}),r.pipe(W(a),ee("height")).subscribe(({height:s})=>{e.style.top=`${s+16}px`}),h(e,"click").subscribe(s=>{s.preventDefault(),window.scrollTo({top:0})}),vs(e,{viewport$:t,main$:o,target$:n}).pipe(w(s=>i.next(s)),_(()=>i.complete()),m(s=>$({ref:e},s)))}function wi({document$:e,viewport$:t}){e.pipe(v(()=>P(".md-ellipsis")),ne(r=>tt(r).pipe(W(e.pipe(Ce(1))),b(o=>o),m(()=>r),Te(1))),b(r=>r.offsetWidth{let o=r.innerText,n=r.closest("a")||r;return n.title=o,B("content.tooltips")?mt(n,{viewport$:t}).pipe(W(e.pipe(Ce(1))),_(()=>n.removeAttribute("title"))):S})).subscribe(),B("content.tooltips")&&e.pipe(v(()=>P(".md-status")),ne(r=>mt(r,{viewport$:t}))).subscribe()}function Ti({document$:e,tablet$:t}){e.pipe(v(()=>P(".md-toggle--indeterminate")),w(r=>{r.indeterminate=!0,r.checked=!1}),ne(r=>h(r,"change").pipe(Dr(()=>r.classList.contains("md-toggle--indeterminate")),m(()=>r))),re(t)).subscribe(([r,o])=>{r.classList.remove("md-toggle--indeterminate"),o&&(r.checked=!1)})}function gs(){return/(iPad|iPhone|iPod)/.test(navigator.userAgent)}function Si({document$:e}){e.pipe(v(()=>P("[data-md-scrollfix]")),w(t=>t.removeAttribute("data-md-scrollfix")),b(gs),ne(t=>h(t,"touchstart").pipe(m(()=>t)))).subscribe(t=>{let r=t.scrollTop;r===0?t.scrollTop=1:r+t.offsetHeight===t.scrollHeight&&(t.scrollTop=r-1)})}function Oi({viewport$:e,tablet$:t}){z([ze("search"),t]).pipe(m(([r,o])=>r&&!o),v(r=>I(r).pipe(Ge(r?400:100))),re(e)).subscribe(([r,{offset:{y:o}}])=>{if(r)document.body.setAttribute("data-md-scrolllock",""),document.body.style.top=`-${o}px`;else{let n=-1*parseInt(document.body.style.top,10);document.body.removeAttribute("data-md-scrolllock"),document.body.style.top="",n&&window.scrollTo(0,n)}})}Object.entries||(Object.entries=function(e){let t=[];for(let r of Object.keys(e))t.push([r,e[r]]);return t});Object.values||(Object.values=function(e){let t=[];for(let r of Object.keys(e))t.push(e[r]);return t});typeof Element!="undefined"&&(Element.prototype.scrollTo||(Element.prototype.scrollTo=function(e,t){typeof e=="object"?(this.scrollLeft=e.left,this.scrollTop=e.top):(this.scrollLeft=e,this.scrollTop=t)}),Element.prototype.replaceWith||(Element.prototype.replaceWith=function(...e){let t=this.parentNode;if(t){e.length===0&&t.removeChild(this);for(let r=e.length-1;r>=0;r--){let o=e[r];typeof o=="string"?o=document.createTextNode(o):o.parentNode&&o.parentNode.removeChild(o),r?t.insertBefore(this.previousSibling,o):t.replaceChild(o,this)}}}));function ys(){return location.protocol==="file:"?Tt(`${new URL("search/search_index.js",eo.base)}`).pipe(m(()=>__index),G(1)):je(new URL("search/search_index.json",eo.base))}document.documentElement.classList.remove("no-js");document.documentElement.classList.add("js");var ot=Go(),Ut=sn(),Lt=ln(Ut),to=an(),Oe=gn(),hr=Pt("(min-width: 960px)"),Mi=Pt("(min-width: 1220px)"),_i=mn(),eo=xe(),Ai=document.forms.namedItem("search")?ys():Ye,ro=new g;Zn({alert$:ro});var oo=new g;B("navigation.instant")&&oi({location$:Ut,viewport$:Oe,progress$:oo}).subscribe(ot);var Li;((Li=eo.version)==null?void 0:Li.provider)==="mike"&&ci({document$:ot});O(Ut,Lt).pipe(Ge(125)).subscribe(()=>{Je("drawer",!1),Je("search",!1)});to.pipe(b(({mode:e})=>e==="global")).subscribe(e=>{switch(e.type){case"p":case",":let t=fe("link[rel=prev]");typeof t!="undefined"&<(t);break;case"n":case".":let r=fe("link[rel=next]");typeof r!="undefined"&<(r);break;case"Enter":let o=Ie();o instanceof HTMLLabelElement&&o.click()}});wi({viewport$:Oe,document$:ot});Ti({document$:ot,tablet$:hr});Si({document$:ot});Oi({viewport$:Oe,tablet$:hr});var rt=Kn(Se("header"),{viewport$:Oe}),Ft=ot.pipe(m(()=>Se("main")),v(e=>Gn(e,{viewport$:Oe,header$:rt})),G(1)),xs=O(...ae("consent").map(e=>En(e,{target$:Lt})),...ae("dialog").map(e=>qn(e,{alert$:ro})),...ae("palette").map(e=>Jn(e)),...ae("progress").map(e=>Xn(e,{progress$:oo})),...ae("search").map(e=>ui(e,{index$:Ai,keyboard$:to})),...ae("source").map(e=>gi(e))),Es=C(()=>O(...ae("announce").map(e=>xn(e)),...ae("content").map(e=>zn(e,{viewport$:Oe,target$:Lt,print$:_i})),...ae("content").map(e=>B("search.highlight")?di(e,{index$:Ai,location$:Ut}):S),...ae("header").map(e=>Yn(e,{viewport$:Oe,header$:rt,main$:Ft})),...ae("header-title").map(e=>Bn(e,{viewport$:Oe,header$:rt})),...ae("sidebar").map(e=>e.getAttribute("data-md-type")==="navigation"?Nr(Mi,()=>Zr(e,{viewport$:Oe,header$:rt,main$:Ft})):Nr(hr,()=>Zr(e,{viewport$:Oe,header$:rt,main$:Ft}))),...ae("tabs").map(e=>yi(e,{viewport$:Oe,header$:rt})),...ae("toc").map(e=>xi(e,{viewport$:Oe,header$:rt,main$:Ft,target$:Lt})),...ae("top").map(e=>Ei(e,{viewport$:Oe,header$:rt,main$:Ft,target$:Lt})))),Ci=ot.pipe(v(()=>Es),Re(xs),G(1));Ci.subscribe();window.document$=ot;window.location$=Ut;window.target$=Lt;window.keyboard$=to;window.viewport$=Oe;window.tablet$=hr;window.screen$=Mi;window.print$=_i;window.alert$=ro;window.progress$=oo;window.component$=Ci;})(); +//# sourceMappingURL=bundle.60a45f97.min.js.map + diff --git a/assets/javascripts/bundle.60a45f97.min.js.map b/assets/javascripts/bundle.60a45f97.min.js.map new file mode 100644 index 00000000..7edbc6f4 --- /dev/null +++ b/assets/javascripts/bundle.60a45f97.min.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["node_modules/focus-visible/dist/focus-visible.js", "node_modules/escape-html/index.js", "node_modules/clipboard/dist/clipboard.js", "src/templates/assets/javascripts/bundle.ts", "node_modules/tslib/tslib.es6.mjs", "node_modules/rxjs/src/internal/util/isFunction.ts", "node_modules/rxjs/src/internal/util/createErrorClass.ts", "node_modules/rxjs/src/internal/util/UnsubscriptionError.ts", "node_modules/rxjs/src/internal/util/arrRemove.ts", "node_modules/rxjs/src/internal/Subscription.ts", "node_modules/rxjs/src/internal/config.ts", "node_modules/rxjs/src/internal/scheduler/timeoutProvider.ts", "node_modules/rxjs/src/internal/util/reportUnhandledError.ts", "node_modules/rxjs/src/internal/util/noop.ts", "node_modules/rxjs/src/internal/NotificationFactories.ts", "node_modules/rxjs/src/internal/util/errorContext.ts", "node_modules/rxjs/src/internal/Subscriber.ts", "node_modules/rxjs/src/internal/symbol/observable.ts", "node_modules/rxjs/src/internal/util/identity.ts", "node_modules/rxjs/src/internal/util/pipe.ts", "node_modules/rxjs/src/internal/Observable.ts", "node_modules/rxjs/src/internal/util/lift.ts", "node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts", "node_modules/rxjs/src/internal/scheduler/animationFrameProvider.ts", "node_modules/rxjs/src/internal/util/ObjectUnsubscribedError.ts", "node_modules/rxjs/src/internal/Subject.ts", "node_modules/rxjs/src/internal/BehaviorSubject.ts", "node_modules/rxjs/src/internal/scheduler/dateTimestampProvider.ts", "node_modules/rxjs/src/internal/ReplaySubject.ts", "node_modules/rxjs/src/internal/scheduler/Action.ts", "node_modules/rxjs/src/internal/scheduler/intervalProvider.ts", "node_modules/rxjs/src/internal/scheduler/AsyncAction.ts", "node_modules/rxjs/src/internal/Scheduler.ts", "node_modules/rxjs/src/internal/scheduler/AsyncScheduler.ts", "node_modules/rxjs/src/internal/scheduler/async.ts", "node_modules/rxjs/src/internal/scheduler/QueueAction.ts", "node_modules/rxjs/src/internal/scheduler/QueueScheduler.ts", "node_modules/rxjs/src/internal/scheduler/queue.ts", "node_modules/rxjs/src/internal/scheduler/AnimationFrameAction.ts", "node_modules/rxjs/src/internal/scheduler/AnimationFrameScheduler.ts", "node_modules/rxjs/src/internal/scheduler/animationFrame.ts", "node_modules/rxjs/src/internal/observable/empty.ts", "node_modules/rxjs/src/internal/util/isScheduler.ts", "node_modules/rxjs/src/internal/util/args.ts", "node_modules/rxjs/src/internal/util/isArrayLike.ts", "node_modules/rxjs/src/internal/util/isPromise.ts", "node_modules/rxjs/src/internal/util/isInteropObservable.ts", "node_modules/rxjs/src/internal/util/isAsyncIterable.ts", "node_modules/rxjs/src/internal/util/throwUnobservableError.ts", "node_modules/rxjs/src/internal/symbol/iterator.ts", "node_modules/rxjs/src/internal/util/isIterable.ts", "node_modules/rxjs/src/internal/util/isReadableStreamLike.ts", "node_modules/rxjs/src/internal/observable/innerFrom.ts", "node_modules/rxjs/src/internal/util/executeSchedule.ts", "node_modules/rxjs/src/internal/operators/observeOn.ts", "node_modules/rxjs/src/internal/operators/subscribeOn.ts", "node_modules/rxjs/src/internal/scheduled/scheduleObservable.ts", "node_modules/rxjs/src/internal/scheduled/schedulePromise.ts", "node_modules/rxjs/src/internal/scheduled/scheduleArray.ts", "node_modules/rxjs/src/internal/scheduled/scheduleIterable.ts", "node_modules/rxjs/src/internal/scheduled/scheduleAsyncIterable.ts", "node_modules/rxjs/src/internal/scheduled/scheduleReadableStreamLike.ts", "node_modules/rxjs/src/internal/scheduled/scheduled.ts", "node_modules/rxjs/src/internal/observable/from.ts", "node_modules/rxjs/src/internal/observable/of.ts", "node_modules/rxjs/src/internal/observable/throwError.ts", "node_modules/rxjs/src/internal/util/EmptyError.ts", "node_modules/rxjs/src/internal/util/isDate.ts", "node_modules/rxjs/src/internal/operators/map.ts", "node_modules/rxjs/src/internal/util/mapOneOrManyArgs.ts", "node_modules/rxjs/src/internal/util/argsArgArrayOrObject.ts", "node_modules/rxjs/src/internal/util/createObject.ts", "node_modules/rxjs/src/internal/observable/combineLatest.ts", "node_modules/rxjs/src/internal/operators/mergeInternals.ts", "node_modules/rxjs/src/internal/operators/mergeMap.ts", "node_modules/rxjs/src/internal/operators/mergeAll.ts", "node_modules/rxjs/src/internal/operators/concatAll.ts", "node_modules/rxjs/src/internal/observable/concat.ts", "node_modules/rxjs/src/internal/observable/defer.ts", "node_modules/rxjs/src/internal/observable/fromEvent.ts", "node_modules/rxjs/src/internal/observable/fromEventPattern.ts", "node_modules/rxjs/src/internal/observable/timer.ts", "node_modules/rxjs/src/internal/observable/merge.ts", "node_modules/rxjs/src/internal/observable/never.ts", "node_modules/rxjs/src/internal/util/argsOrArgArray.ts", "node_modules/rxjs/src/internal/operators/filter.ts", "node_modules/rxjs/src/internal/observable/zip.ts", "node_modules/rxjs/src/internal/operators/audit.ts", "node_modules/rxjs/src/internal/operators/auditTime.ts", "node_modules/rxjs/src/internal/operators/bufferCount.ts", "node_modules/rxjs/src/internal/operators/catchError.ts", "node_modules/rxjs/src/internal/operators/scanInternals.ts", "node_modules/rxjs/src/internal/operators/combineLatest.ts", "node_modules/rxjs/src/internal/operators/combineLatestWith.ts", "node_modules/rxjs/src/internal/operators/debounce.ts", "node_modules/rxjs/src/internal/operators/debounceTime.ts", "node_modules/rxjs/src/internal/operators/defaultIfEmpty.ts", "node_modules/rxjs/src/internal/operators/take.ts", "node_modules/rxjs/src/internal/operators/ignoreElements.ts", "node_modules/rxjs/src/internal/operators/mapTo.ts", "node_modules/rxjs/src/internal/operators/delayWhen.ts", "node_modules/rxjs/src/internal/operators/delay.ts", "node_modules/rxjs/src/internal/operators/distinctUntilChanged.ts", "node_modules/rxjs/src/internal/operators/distinctUntilKeyChanged.ts", "node_modules/rxjs/src/internal/operators/throwIfEmpty.ts", "node_modules/rxjs/src/internal/operators/endWith.ts", "node_modules/rxjs/src/internal/operators/finalize.ts", "node_modules/rxjs/src/internal/operators/first.ts", "node_modules/rxjs/src/internal/operators/takeLast.ts", "node_modules/rxjs/src/internal/operators/merge.ts", "node_modules/rxjs/src/internal/operators/mergeWith.ts", "node_modules/rxjs/src/internal/operators/repeat.ts", "node_modules/rxjs/src/internal/operators/scan.ts", "node_modules/rxjs/src/internal/operators/share.ts", "node_modules/rxjs/src/internal/operators/shareReplay.ts", "node_modules/rxjs/src/internal/operators/skip.ts", "node_modules/rxjs/src/internal/operators/skipUntil.ts", "node_modules/rxjs/src/internal/operators/startWith.ts", "node_modules/rxjs/src/internal/operators/switchMap.ts", "node_modules/rxjs/src/internal/operators/takeUntil.ts", "node_modules/rxjs/src/internal/operators/takeWhile.ts", "node_modules/rxjs/src/internal/operators/tap.ts", "node_modules/rxjs/src/internal/operators/throttle.ts", "node_modules/rxjs/src/internal/operators/throttleTime.ts", "node_modules/rxjs/src/internal/operators/withLatestFrom.ts", "node_modules/rxjs/src/internal/operators/zip.ts", "node_modules/rxjs/src/internal/operators/zipWith.ts", "src/templates/assets/javascripts/browser/document/index.ts", "src/templates/assets/javascripts/browser/element/_/index.ts", "src/templates/assets/javascripts/browser/element/focus/index.ts", "src/templates/assets/javascripts/browser/element/hover/index.ts", "src/templates/assets/javascripts/utilities/h/index.ts", "src/templates/assets/javascripts/utilities/round/index.ts", "src/templates/assets/javascripts/browser/script/index.ts", "src/templates/assets/javascripts/browser/element/size/_/index.ts", "src/templates/assets/javascripts/browser/element/size/content/index.ts", "src/templates/assets/javascripts/browser/element/offset/_/index.ts", "src/templates/assets/javascripts/browser/element/offset/content/index.ts", "src/templates/assets/javascripts/browser/element/visibility/index.ts", "src/templates/assets/javascripts/browser/toggle/index.ts", "src/templates/assets/javascripts/browser/keyboard/index.ts", "src/templates/assets/javascripts/browser/location/_/index.ts", "src/templates/assets/javascripts/browser/location/hash/index.ts", "src/templates/assets/javascripts/browser/media/index.ts", "src/templates/assets/javascripts/browser/request/index.ts", "src/templates/assets/javascripts/browser/viewport/offset/index.ts", "src/templates/assets/javascripts/browser/viewport/size/index.ts", "src/templates/assets/javascripts/browser/viewport/_/index.ts", "src/templates/assets/javascripts/browser/viewport/at/index.ts", "src/templates/assets/javascripts/browser/worker/index.ts", "src/templates/assets/javascripts/_/index.ts", "src/templates/assets/javascripts/components/_/index.ts", "src/templates/assets/javascripts/components/announce/index.ts", "src/templates/assets/javascripts/components/consent/index.ts", "src/templates/assets/javascripts/templates/tooltip/index.tsx", "src/templates/assets/javascripts/templates/annotation/index.tsx", "src/templates/assets/javascripts/templates/clipboard/index.tsx", "src/templates/assets/javascripts/templates/search/index.tsx", "src/templates/assets/javascripts/templates/source/index.tsx", "src/templates/assets/javascripts/templates/tabbed/index.tsx", "src/templates/assets/javascripts/templates/table/index.tsx", "src/templates/assets/javascripts/templates/version/index.tsx", "src/templates/assets/javascripts/components/tooltip2/index.ts", "src/templates/assets/javascripts/components/content/annotation/_/index.ts", "src/templates/assets/javascripts/components/content/annotation/list/index.ts", "src/templates/assets/javascripts/components/content/annotation/block/index.ts", "src/templates/assets/javascripts/components/content/code/_/index.ts", "src/templates/assets/javascripts/components/content/details/index.ts", "src/templates/assets/javascripts/components/content/mermaid/index.css", "src/templates/assets/javascripts/components/content/mermaid/index.ts", "src/templates/assets/javascripts/components/content/table/index.ts", "src/templates/assets/javascripts/components/content/tabs/index.ts", "src/templates/assets/javascripts/components/content/_/index.ts", "src/templates/assets/javascripts/components/dialog/index.ts", "src/templates/assets/javascripts/components/tooltip/index.ts", "src/templates/assets/javascripts/components/header/_/index.ts", "src/templates/assets/javascripts/components/header/title/index.ts", "src/templates/assets/javascripts/components/main/index.ts", "src/templates/assets/javascripts/components/palette/index.ts", "src/templates/assets/javascripts/components/progress/index.ts", "src/templates/assets/javascripts/integrations/clipboard/index.ts", "src/templates/assets/javascripts/integrations/sitemap/index.ts", "src/templates/assets/javascripts/integrations/instant/index.ts", "src/templates/assets/javascripts/integrations/search/highlighter/index.ts", "src/templates/assets/javascripts/integrations/search/worker/message/index.ts", "src/templates/assets/javascripts/integrations/search/worker/_/index.ts", "src/templates/assets/javascripts/integrations/version/findurl/index.ts", "src/templates/assets/javascripts/integrations/version/index.ts", "src/templates/assets/javascripts/components/search/query/index.ts", "src/templates/assets/javascripts/components/search/result/index.ts", "src/templates/assets/javascripts/components/search/share/index.ts", "src/templates/assets/javascripts/components/search/suggest/index.ts", "src/templates/assets/javascripts/components/search/_/index.ts", "src/templates/assets/javascripts/components/search/highlight/index.ts", "src/templates/assets/javascripts/components/sidebar/index.ts", "src/templates/assets/javascripts/components/source/facts/github/index.ts", "src/templates/assets/javascripts/components/source/facts/gitlab/index.ts", "src/templates/assets/javascripts/components/source/facts/_/index.ts", "src/templates/assets/javascripts/components/source/_/index.ts", "src/templates/assets/javascripts/components/tabs/index.ts", "src/templates/assets/javascripts/components/toc/index.ts", "src/templates/assets/javascripts/components/top/index.ts", "src/templates/assets/javascripts/patches/ellipsis/index.ts", "src/templates/assets/javascripts/patches/indeterminate/index.ts", "src/templates/assets/javascripts/patches/scrollfix/index.ts", "src/templates/assets/javascripts/patches/scrolllock/index.ts", "src/templates/assets/javascripts/polyfills/index.ts"], + "sourcesContent": ["(function (global, factory) {\n typeof exports === 'object' && typeof module !== 'undefined' ? factory() :\n typeof define === 'function' && define.amd ? define(factory) :\n (factory());\n}(this, (function () { 'use strict';\n\n /**\n * Applies the :focus-visible polyfill at the given scope.\n * A scope in this case is either the top-level Document or a Shadow Root.\n *\n * @param {(Document|ShadowRoot)} scope\n * @see https://github.com/WICG/focus-visible\n */\n function applyFocusVisiblePolyfill(scope) {\n var hadKeyboardEvent = true;\n var hadFocusVisibleRecently = false;\n var hadFocusVisibleRecentlyTimeout = null;\n\n var inputTypesAllowlist = {\n text: true,\n search: true,\n url: true,\n tel: true,\n email: true,\n password: true,\n number: true,\n date: true,\n month: true,\n week: true,\n time: true,\n datetime: true,\n 'datetime-local': true\n };\n\n /**\n * Helper function for legacy browsers and iframes which sometimes focus\n * elements like document, body, and non-interactive SVG.\n * @param {Element} el\n */\n function isValidFocusTarget(el) {\n if (\n el &&\n el !== document &&\n el.nodeName !== 'HTML' &&\n el.nodeName !== 'BODY' &&\n 'classList' in el &&\n 'contains' in el.classList\n ) {\n return true;\n }\n return false;\n }\n\n /**\n * Computes whether the given element should automatically trigger the\n * `focus-visible` class being added, i.e. whether it should always match\n * `:focus-visible` when focused.\n * @param {Element} el\n * @return {boolean}\n */\n function focusTriggersKeyboardModality(el) {\n var type = el.type;\n var tagName = el.tagName;\n\n if (tagName === 'INPUT' && inputTypesAllowlist[type] && !el.readOnly) {\n return true;\n }\n\n if (tagName === 'TEXTAREA' && !el.readOnly) {\n return true;\n }\n\n if (el.isContentEditable) {\n return true;\n }\n\n return false;\n }\n\n /**\n * Add the `focus-visible` class to the given element if it was not added by\n * the author.\n * @param {Element} el\n */\n function addFocusVisibleClass(el) {\n if (el.classList.contains('focus-visible')) {\n return;\n }\n el.classList.add('focus-visible');\n el.setAttribute('data-focus-visible-added', '');\n }\n\n /**\n * Remove the `focus-visible` class from the given element if it was not\n * originally added by the author.\n * @param {Element} el\n */\n function removeFocusVisibleClass(el) {\n if (!el.hasAttribute('data-focus-visible-added')) {\n return;\n }\n el.classList.remove('focus-visible');\n el.removeAttribute('data-focus-visible-added');\n }\n\n /**\n * If the most recent user interaction was via the keyboard;\n * and the key press did not include a meta, alt/option, or control key;\n * then the modality is keyboard. Otherwise, the modality is not keyboard.\n * Apply `focus-visible` to any current active element and keep track\n * of our keyboard modality state with `hadKeyboardEvent`.\n * @param {KeyboardEvent} e\n */\n function onKeyDown(e) {\n if (e.metaKey || e.altKey || e.ctrlKey) {\n return;\n }\n\n if (isValidFocusTarget(scope.activeElement)) {\n addFocusVisibleClass(scope.activeElement);\n }\n\n hadKeyboardEvent = true;\n }\n\n /**\n * If at any point a user clicks with a pointing device, ensure that we change\n * the modality away from keyboard.\n * This avoids the situation where a user presses a key on an already focused\n * element, and then clicks on a different element, focusing it with a\n * pointing device, while we still think we're in keyboard modality.\n * @param {Event} e\n */\n function onPointerDown(e) {\n hadKeyboardEvent = false;\n }\n\n /**\n * On `focus`, add the `focus-visible` class to the target if:\n * - the target received focus as a result of keyboard navigation, or\n * - the event target is an element that will likely require interaction\n * via the keyboard (e.g. a text box)\n * @param {Event} e\n */\n function onFocus(e) {\n // Prevent IE from focusing the document or HTML element.\n if (!isValidFocusTarget(e.target)) {\n return;\n }\n\n if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) {\n addFocusVisibleClass(e.target);\n }\n }\n\n /**\n * On `blur`, remove the `focus-visible` class from the target.\n * @param {Event} e\n */\n function onBlur(e) {\n if (!isValidFocusTarget(e.target)) {\n return;\n }\n\n if (\n e.target.classList.contains('focus-visible') ||\n e.target.hasAttribute('data-focus-visible-added')\n ) {\n // To detect a tab/window switch, we look for a blur event followed\n // rapidly by a visibility change.\n // If we don't see a visibility change within 100ms, it's probably a\n // regular focus change.\n hadFocusVisibleRecently = true;\n window.clearTimeout(hadFocusVisibleRecentlyTimeout);\n hadFocusVisibleRecentlyTimeout = window.setTimeout(function() {\n hadFocusVisibleRecently = false;\n }, 100);\n removeFocusVisibleClass(e.target);\n }\n }\n\n /**\n * If the user changes tabs, keep track of whether or not the previously\n * focused element had .focus-visible.\n * @param {Event} e\n */\n function onVisibilityChange(e) {\n if (document.visibilityState === 'hidden') {\n // If the tab becomes active again, the browser will handle calling focus\n // on the element (Safari actually calls it twice).\n // If this tab change caused a blur on an element with focus-visible,\n // re-apply the class when the user switches back to the tab.\n if (hadFocusVisibleRecently) {\n hadKeyboardEvent = true;\n }\n addInitialPointerMoveListeners();\n }\n }\n\n /**\n * Add a group of listeners to detect usage of any pointing devices.\n * These listeners will be added when the polyfill first loads, and anytime\n * the window is blurred, so that they are active when the window regains\n * focus.\n */\n function addInitialPointerMoveListeners() {\n document.addEventListener('mousemove', onInitialPointerMove);\n document.addEventListener('mousedown', onInitialPointerMove);\n document.addEventListener('mouseup', onInitialPointerMove);\n document.addEventListener('pointermove', onInitialPointerMove);\n document.addEventListener('pointerdown', onInitialPointerMove);\n document.addEventListener('pointerup', onInitialPointerMove);\n document.addEventListener('touchmove', onInitialPointerMove);\n document.addEventListener('touchstart', onInitialPointerMove);\n document.addEventListener('touchend', onInitialPointerMove);\n }\n\n function removeInitialPointerMoveListeners() {\n document.removeEventListener('mousemove', onInitialPointerMove);\n document.removeEventListener('mousedown', onInitialPointerMove);\n document.removeEventListener('mouseup', onInitialPointerMove);\n document.removeEventListener('pointermove', onInitialPointerMove);\n document.removeEventListener('pointerdown', onInitialPointerMove);\n document.removeEventListener('pointerup', onInitialPointerMove);\n document.removeEventListener('touchmove', onInitialPointerMove);\n document.removeEventListener('touchstart', onInitialPointerMove);\n document.removeEventListener('touchend', onInitialPointerMove);\n }\n\n /**\n * When the polfyill first loads, assume the user is in keyboard modality.\n * If any event is received from a pointing device (e.g. mouse, pointer,\n * touch), turn off keyboard modality.\n * This accounts for situations where focus enters the page from the URL bar.\n * @param {Event} e\n */\n function onInitialPointerMove(e) {\n // Work around a Safari quirk that fires a mousemove on whenever the\n // window blurs, even if you're tabbing out of the page. \u00AF\\_(\u30C4)_/\u00AF\n if (e.target.nodeName && e.target.nodeName.toLowerCase() === 'html') {\n return;\n }\n\n hadKeyboardEvent = false;\n removeInitialPointerMoveListeners();\n }\n\n // For some kinds of state, we are interested in changes at the global scope\n // only. For example, global pointer input, global key presses and global\n // visibility change should affect the state at every scope:\n document.addEventListener('keydown', onKeyDown, true);\n document.addEventListener('mousedown', onPointerDown, true);\n document.addEventListener('pointerdown', onPointerDown, true);\n document.addEventListener('touchstart', onPointerDown, true);\n document.addEventListener('visibilitychange', onVisibilityChange, true);\n\n addInitialPointerMoveListeners();\n\n // For focus and blur, we specifically care about state changes in the local\n // scope. This is because focus / blur events that originate from within a\n // shadow root are not re-dispatched from the host element if it was already\n // the active element in its own scope:\n scope.addEventListener('focus', onFocus, true);\n scope.addEventListener('blur', onBlur, true);\n\n // We detect that a node is a ShadowRoot by ensuring that it is a\n // DocumentFragment and also has a host property. This check covers native\n // implementation and polyfill implementation transparently. If we only cared\n // about the native implementation, we could just check if the scope was\n // an instance of a ShadowRoot.\n if (scope.nodeType === Node.DOCUMENT_FRAGMENT_NODE && scope.host) {\n // Since a ShadowRoot is a special kind of DocumentFragment, it does not\n // have a root element to add a class to. So, we add this attribute to the\n // host element instead:\n scope.host.setAttribute('data-js-focus-visible', '');\n } else if (scope.nodeType === Node.DOCUMENT_NODE) {\n document.documentElement.classList.add('js-focus-visible');\n document.documentElement.setAttribute('data-js-focus-visible', '');\n }\n }\n\n // It is important to wrap all references to global window and document in\n // these checks to support server-side rendering use cases\n // @see https://github.com/WICG/focus-visible/issues/199\n if (typeof window !== 'undefined' && typeof document !== 'undefined') {\n // Make the polyfill helper globally available. This can be used as a signal\n // to interested libraries that wish to coordinate with the polyfill for e.g.,\n // applying the polyfill to a shadow root:\n window.applyFocusVisiblePolyfill = applyFocusVisiblePolyfill;\n\n // Notify interested libraries of the polyfill's presence, in case the\n // polyfill was loaded lazily:\n var event;\n\n try {\n event = new CustomEvent('focus-visible-polyfill-ready');\n } catch (error) {\n // IE11 does not support using CustomEvent as a constructor directly:\n event = document.createEvent('CustomEvent');\n event.initCustomEvent('focus-visible-polyfill-ready', false, false, {});\n }\n\n window.dispatchEvent(event);\n }\n\n if (typeof document !== 'undefined') {\n // Apply the polyfill to the global document, so that no JavaScript\n // coordination is required to use the polyfill in the top-level document:\n applyFocusVisiblePolyfill(document);\n }\n\n})));\n", "/*!\n * escape-html\n * Copyright(c) 2012-2013 TJ Holowaychuk\n * Copyright(c) 2015 Andreas Lubbe\n * Copyright(c) 2015 Tiancheng \"Timothy\" Gu\n * MIT Licensed\n */\n\n'use strict';\n\n/**\n * Module variables.\n * @private\n */\n\nvar matchHtmlRegExp = /[\"'&<>]/;\n\n/**\n * Module exports.\n * @public\n */\n\nmodule.exports = escapeHtml;\n\n/**\n * Escape special characters in the given string of html.\n *\n * @param {string} string The string to escape for inserting into HTML\n * @return {string}\n * @public\n */\n\nfunction escapeHtml(string) {\n var str = '' + string;\n var match = matchHtmlRegExp.exec(str);\n\n if (!match) {\n return str;\n }\n\n var escape;\n var html = '';\n var index = 0;\n var lastIndex = 0;\n\n for (index = match.index; index < str.length; index++) {\n switch (str.charCodeAt(index)) {\n case 34: // \"\n escape = '"';\n break;\n case 38: // &\n escape = '&';\n break;\n case 39: // '\n escape = ''';\n break;\n case 60: // <\n escape = '<';\n break;\n case 62: // >\n escape = '>';\n break;\n default:\n continue;\n }\n\n if (lastIndex !== index) {\n html += str.substring(lastIndex, index);\n }\n\n lastIndex = index + 1;\n html += escape;\n }\n\n return lastIndex !== index\n ? html + str.substring(lastIndex, index)\n : html;\n}\n", "/*!\n * clipboard.js v2.0.11\n * https://clipboardjs.com/\n *\n * Licensed MIT \u00A9 Zeno Rocha\n */\n(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"ClipboardJS\"] = factory();\n\telse\n\t\troot[\"ClipboardJS\"] = factory();\n})(this, function() {\nreturn /******/ (function() { // webpackBootstrap\n/******/ \tvar __webpack_modules__ = ({\n\n/***/ 686:\n/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {\n\n\"use strict\";\n\n// EXPORTS\n__webpack_require__.d(__webpack_exports__, {\n \"default\": function() { return /* binding */ clipboard; }\n});\n\n// EXTERNAL MODULE: ./node_modules/tiny-emitter/index.js\nvar tiny_emitter = __webpack_require__(279);\nvar tiny_emitter_default = /*#__PURE__*/__webpack_require__.n(tiny_emitter);\n// EXTERNAL MODULE: ./node_modules/good-listener/src/listen.js\nvar listen = __webpack_require__(370);\nvar listen_default = /*#__PURE__*/__webpack_require__.n(listen);\n// EXTERNAL MODULE: ./node_modules/select/src/select.js\nvar src_select = __webpack_require__(817);\nvar select_default = /*#__PURE__*/__webpack_require__.n(src_select);\n;// CONCATENATED MODULE: ./src/common/command.js\n/**\n * Executes a given operation type.\n * @param {String} type\n * @return {Boolean}\n */\nfunction command(type) {\n try {\n return document.execCommand(type);\n } catch (err) {\n return false;\n }\n}\n;// CONCATENATED MODULE: ./src/actions/cut.js\n\n\n/**\n * Cut action wrapper.\n * @param {String|HTMLElement} target\n * @return {String}\n */\n\nvar ClipboardActionCut = function ClipboardActionCut(target) {\n var selectedText = select_default()(target);\n command('cut');\n return selectedText;\n};\n\n/* harmony default export */ var actions_cut = (ClipboardActionCut);\n;// CONCATENATED MODULE: ./src/common/create-fake-element.js\n/**\n * Creates a fake textarea element with a value.\n * @param {String} value\n * @return {HTMLElement}\n */\nfunction createFakeElement(value) {\n var isRTL = document.documentElement.getAttribute('dir') === 'rtl';\n var fakeElement = document.createElement('textarea'); // Prevent zooming on iOS\n\n fakeElement.style.fontSize = '12pt'; // Reset box model\n\n fakeElement.style.border = '0';\n fakeElement.style.padding = '0';\n fakeElement.style.margin = '0'; // Move element out of screen horizontally\n\n fakeElement.style.position = 'absolute';\n fakeElement.style[isRTL ? 'right' : 'left'] = '-9999px'; // Move element to the same position vertically\n\n var yPosition = window.pageYOffset || document.documentElement.scrollTop;\n fakeElement.style.top = \"\".concat(yPosition, \"px\");\n fakeElement.setAttribute('readonly', '');\n fakeElement.value = value;\n return fakeElement;\n}\n;// CONCATENATED MODULE: ./src/actions/copy.js\n\n\n\n/**\n * Create fake copy action wrapper using a fake element.\n * @param {String} target\n * @param {Object} options\n * @return {String}\n */\n\nvar fakeCopyAction = function fakeCopyAction(value, options) {\n var fakeElement = createFakeElement(value);\n options.container.appendChild(fakeElement);\n var selectedText = select_default()(fakeElement);\n command('copy');\n fakeElement.remove();\n return selectedText;\n};\n/**\n * Copy action wrapper.\n * @param {String|HTMLElement} target\n * @param {Object} options\n * @return {String}\n */\n\n\nvar ClipboardActionCopy = function ClipboardActionCopy(target) {\n var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {\n container: document.body\n };\n var selectedText = '';\n\n if (typeof target === 'string') {\n selectedText = fakeCopyAction(target, options);\n } else if (target instanceof HTMLInputElement && !['text', 'search', 'url', 'tel', 'password'].includes(target === null || target === void 0 ? void 0 : target.type)) {\n // If input type doesn't support `setSelectionRange`. Simulate it. https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange\n selectedText = fakeCopyAction(target.value, options);\n } else {\n selectedText = select_default()(target);\n command('copy');\n }\n\n return selectedText;\n};\n\n/* harmony default export */ var actions_copy = (ClipboardActionCopy);\n;// CONCATENATED MODULE: ./src/actions/default.js\nfunction _typeof(obj) { \"@babel/helpers - typeof\"; if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\n\n\n/**\n * Inner function which performs selection from either `text` or `target`\n * properties and then executes copy or cut operations.\n * @param {Object} options\n */\n\nvar ClipboardActionDefault = function ClipboardActionDefault() {\n var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n // Defines base properties passed from constructor.\n var _options$action = options.action,\n action = _options$action === void 0 ? 'copy' : _options$action,\n container = options.container,\n target = options.target,\n text = options.text; // Sets the `action` to be performed which can be either 'copy' or 'cut'.\n\n if (action !== 'copy' && action !== 'cut') {\n throw new Error('Invalid \"action\" value, use either \"copy\" or \"cut\"');\n } // Sets the `target` property using an element that will be have its content copied.\n\n\n if (target !== undefined) {\n if (target && _typeof(target) === 'object' && target.nodeType === 1) {\n if (action === 'copy' && target.hasAttribute('disabled')) {\n throw new Error('Invalid \"target\" attribute. Please use \"readonly\" instead of \"disabled\" attribute');\n }\n\n if (action === 'cut' && (target.hasAttribute('readonly') || target.hasAttribute('disabled'))) {\n throw new Error('Invalid \"target\" attribute. You can\\'t cut text from elements with \"readonly\" or \"disabled\" attributes');\n }\n } else {\n throw new Error('Invalid \"target\" value, use a valid Element');\n }\n } // Define selection strategy based on `text` property.\n\n\n if (text) {\n return actions_copy(text, {\n container: container\n });\n } // Defines which selection strategy based on `target` property.\n\n\n if (target) {\n return action === 'cut' ? actions_cut(target) : actions_copy(target, {\n container: container\n });\n }\n};\n\n/* harmony default export */ var actions_default = (ClipboardActionDefault);\n;// CONCATENATED MODULE: ./src/clipboard.js\nfunction clipboard_typeof(obj) { \"@babel/helpers - typeof\"; if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { clipboard_typeof = function _typeof(obj) { return typeof obj; }; } else { clipboard_typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return clipboard_typeof(obj); }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }\n\nfunction _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function\"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }\n\nfunction _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }\n\nfunction _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }\n\nfunction _possibleConstructorReturn(self, call) { if (call && (clipboard_typeof(call) === \"object\" || typeof call === \"function\")) { return call; } return _assertThisInitialized(self); }\n\nfunction _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return self; }\n\nfunction _isNativeReflectConstruct() { if (typeof Reflect === \"undefined\" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === \"function\") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }\n\nfunction _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }\n\n\n\n\n\n\n/**\n * Helper function to retrieve attribute value.\n * @param {String} suffix\n * @param {Element} element\n */\n\nfunction getAttributeValue(suffix, element) {\n var attribute = \"data-clipboard-\".concat(suffix);\n\n if (!element.hasAttribute(attribute)) {\n return;\n }\n\n return element.getAttribute(attribute);\n}\n/**\n * Base class which takes one or more elements, adds event listeners to them,\n * and instantiates a new `ClipboardAction` on each click.\n */\n\n\nvar Clipboard = /*#__PURE__*/function (_Emitter) {\n _inherits(Clipboard, _Emitter);\n\n var _super = _createSuper(Clipboard);\n\n /**\n * @param {String|HTMLElement|HTMLCollection|NodeList} trigger\n * @param {Object} options\n */\n function Clipboard(trigger, options) {\n var _this;\n\n _classCallCheck(this, Clipboard);\n\n _this = _super.call(this);\n\n _this.resolveOptions(options);\n\n _this.listenClick(trigger);\n\n return _this;\n }\n /**\n * Defines if attributes would be resolved using internal setter functions\n * or custom functions that were passed in the constructor.\n * @param {Object} options\n */\n\n\n _createClass(Clipboard, [{\n key: \"resolveOptions\",\n value: function resolveOptions() {\n var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n this.action = typeof options.action === 'function' ? options.action : this.defaultAction;\n this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;\n this.text = typeof options.text === 'function' ? options.text : this.defaultText;\n this.container = clipboard_typeof(options.container) === 'object' ? options.container : document.body;\n }\n /**\n * Adds a click event listener to the passed trigger.\n * @param {String|HTMLElement|HTMLCollection|NodeList} trigger\n */\n\n }, {\n key: \"listenClick\",\n value: function listenClick(trigger) {\n var _this2 = this;\n\n this.listener = listen_default()(trigger, 'click', function (e) {\n return _this2.onClick(e);\n });\n }\n /**\n * Defines a new `ClipboardAction` on each click event.\n * @param {Event} e\n */\n\n }, {\n key: \"onClick\",\n value: function onClick(e) {\n var trigger = e.delegateTarget || e.currentTarget;\n var action = this.action(trigger) || 'copy';\n var text = actions_default({\n action: action,\n container: this.container,\n target: this.target(trigger),\n text: this.text(trigger)\n }); // Fires an event based on the copy operation result.\n\n this.emit(text ? 'success' : 'error', {\n action: action,\n text: text,\n trigger: trigger,\n clearSelection: function clearSelection() {\n if (trigger) {\n trigger.focus();\n }\n\n window.getSelection().removeAllRanges();\n }\n });\n }\n /**\n * Default `action` lookup function.\n * @param {Element} trigger\n */\n\n }, {\n key: \"defaultAction\",\n value: function defaultAction(trigger) {\n return getAttributeValue('action', trigger);\n }\n /**\n * Default `target` lookup function.\n * @param {Element} trigger\n */\n\n }, {\n key: \"defaultTarget\",\n value: function defaultTarget(trigger) {\n var selector = getAttributeValue('target', trigger);\n\n if (selector) {\n return document.querySelector(selector);\n }\n }\n /**\n * Allow fire programmatically a copy action\n * @param {String|HTMLElement} target\n * @param {Object} options\n * @returns Text copied.\n */\n\n }, {\n key: \"defaultText\",\n\n /**\n * Default `text` lookup function.\n * @param {Element} trigger\n */\n value: function defaultText(trigger) {\n return getAttributeValue('text', trigger);\n }\n /**\n * Destroy lifecycle.\n */\n\n }, {\n key: \"destroy\",\n value: function destroy() {\n this.listener.destroy();\n }\n }], [{\n key: \"copy\",\n value: function copy(target) {\n var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {\n container: document.body\n };\n return actions_copy(target, options);\n }\n /**\n * Allow fire programmatically a cut action\n * @param {String|HTMLElement} target\n * @returns Text cutted.\n */\n\n }, {\n key: \"cut\",\n value: function cut(target) {\n return actions_cut(target);\n }\n /**\n * Returns the support of the given action, or all actions if no action is\n * given.\n * @param {String} [action]\n */\n\n }, {\n key: \"isSupported\",\n value: function isSupported() {\n var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ['copy', 'cut'];\n var actions = typeof action === 'string' ? [action] : action;\n var support = !!document.queryCommandSupported;\n actions.forEach(function (action) {\n support = support && !!document.queryCommandSupported(action);\n });\n return support;\n }\n }]);\n\n return Clipboard;\n}((tiny_emitter_default()));\n\n/* harmony default export */ var clipboard = (Clipboard);\n\n/***/ }),\n\n/***/ 828:\n/***/ (function(module) {\n\nvar DOCUMENT_NODE_TYPE = 9;\n\n/**\n * A polyfill for Element.matches()\n */\nif (typeof Element !== 'undefined' && !Element.prototype.matches) {\n var proto = Element.prototype;\n\n proto.matches = proto.matchesSelector ||\n proto.mozMatchesSelector ||\n proto.msMatchesSelector ||\n proto.oMatchesSelector ||\n proto.webkitMatchesSelector;\n}\n\n/**\n * Finds the closest parent that matches a selector.\n *\n * @param {Element} element\n * @param {String} selector\n * @return {Function}\n */\nfunction closest (element, selector) {\n while (element && element.nodeType !== DOCUMENT_NODE_TYPE) {\n if (typeof element.matches === 'function' &&\n element.matches(selector)) {\n return element;\n }\n element = element.parentNode;\n }\n}\n\nmodule.exports = closest;\n\n\n/***/ }),\n\n/***/ 438:\n/***/ (function(module, __unused_webpack_exports, __webpack_require__) {\n\nvar closest = __webpack_require__(828);\n\n/**\n * Delegates event to a selector.\n *\n * @param {Element} element\n * @param {String} selector\n * @param {String} type\n * @param {Function} callback\n * @param {Boolean} useCapture\n * @return {Object}\n */\nfunction _delegate(element, selector, type, callback, useCapture) {\n var listenerFn = listener.apply(this, arguments);\n\n element.addEventListener(type, listenerFn, useCapture);\n\n return {\n destroy: function() {\n element.removeEventListener(type, listenerFn, useCapture);\n }\n }\n}\n\n/**\n * Delegates event to a selector.\n *\n * @param {Element|String|Array} [elements]\n * @param {String} selector\n * @param {String} type\n * @param {Function} callback\n * @param {Boolean} useCapture\n * @return {Object}\n */\nfunction delegate(elements, selector, type, callback, useCapture) {\n // Handle the regular Element usage\n if (typeof elements.addEventListener === 'function') {\n return _delegate.apply(null, arguments);\n }\n\n // Handle Element-less usage, it defaults to global delegation\n if (typeof type === 'function') {\n // Use `document` as the first parameter, then apply arguments\n // This is a short way to .unshift `arguments` without running into deoptimizations\n return _delegate.bind(null, document).apply(null, arguments);\n }\n\n // Handle Selector-based usage\n if (typeof elements === 'string') {\n elements = document.querySelectorAll(elements);\n }\n\n // Handle Array-like based usage\n return Array.prototype.map.call(elements, function (element) {\n return _delegate(element, selector, type, callback, useCapture);\n });\n}\n\n/**\n * Finds closest match and invokes callback.\n *\n * @param {Element} element\n * @param {String} selector\n * @param {String} type\n * @param {Function} callback\n * @return {Function}\n */\nfunction listener(element, selector, type, callback) {\n return function(e) {\n e.delegateTarget = closest(e.target, selector);\n\n if (e.delegateTarget) {\n callback.call(element, e);\n }\n }\n}\n\nmodule.exports = delegate;\n\n\n/***/ }),\n\n/***/ 879:\n/***/ (function(__unused_webpack_module, exports) {\n\n/**\n * Check if argument is a HTML element.\n *\n * @param {Object} value\n * @return {Boolean}\n */\nexports.node = function(value) {\n return value !== undefined\n && value instanceof HTMLElement\n && value.nodeType === 1;\n};\n\n/**\n * Check if argument is a list of HTML elements.\n *\n * @param {Object} value\n * @return {Boolean}\n */\nexports.nodeList = function(value) {\n var type = Object.prototype.toString.call(value);\n\n return value !== undefined\n && (type === '[object NodeList]' || type === '[object HTMLCollection]')\n && ('length' in value)\n && (value.length === 0 || exports.node(value[0]));\n};\n\n/**\n * Check if argument is a string.\n *\n * @param {Object} value\n * @return {Boolean}\n */\nexports.string = function(value) {\n return typeof value === 'string'\n || value instanceof String;\n};\n\n/**\n * Check if argument is a function.\n *\n * @param {Object} value\n * @return {Boolean}\n */\nexports.fn = function(value) {\n var type = Object.prototype.toString.call(value);\n\n return type === '[object Function]';\n};\n\n\n/***/ }),\n\n/***/ 370:\n/***/ (function(module, __unused_webpack_exports, __webpack_require__) {\n\nvar is = __webpack_require__(879);\nvar delegate = __webpack_require__(438);\n\n/**\n * Validates all params and calls the right\n * listener function based on its target type.\n *\n * @param {String|HTMLElement|HTMLCollection|NodeList} target\n * @param {String} type\n * @param {Function} callback\n * @return {Object}\n */\nfunction listen(target, type, callback) {\n if (!target && !type && !callback) {\n throw new Error('Missing required arguments');\n }\n\n if (!is.string(type)) {\n throw new TypeError('Second argument must be a String');\n }\n\n if (!is.fn(callback)) {\n throw new TypeError('Third argument must be a Function');\n }\n\n if (is.node(target)) {\n return listenNode(target, type, callback);\n }\n else if (is.nodeList(target)) {\n return listenNodeList(target, type, callback);\n }\n else if (is.string(target)) {\n return listenSelector(target, type, callback);\n }\n else {\n throw new TypeError('First argument must be a String, HTMLElement, HTMLCollection, or NodeList');\n }\n}\n\n/**\n * Adds an event listener to a HTML element\n * and returns a remove listener function.\n *\n * @param {HTMLElement} node\n * @param {String} type\n * @param {Function} callback\n * @return {Object}\n */\nfunction listenNode(node, type, callback) {\n node.addEventListener(type, callback);\n\n return {\n destroy: function() {\n node.removeEventListener(type, callback);\n }\n }\n}\n\n/**\n * Add an event listener to a list of HTML elements\n * and returns a remove listener function.\n *\n * @param {NodeList|HTMLCollection} nodeList\n * @param {String} type\n * @param {Function} callback\n * @return {Object}\n */\nfunction listenNodeList(nodeList, type, callback) {\n Array.prototype.forEach.call(nodeList, function(node) {\n node.addEventListener(type, callback);\n });\n\n return {\n destroy: function() {\n Array.prototype.forEach.call(nodeList, function(node) {\n node.removeEventListener(type, callback);\n });\n }\n }\n}\n\n/**\n * Add an event listener to a selector\n * and returns a remove listener function.\n *\n * @param {String} selector\n * @param {String} type\n * @param {Function} callback\n * @return {Object}\n */\nfunction listenSelector(selector, type, callback) {\n return delegate(document.body, selector, type, callback);\n}\n\nmodule.exports = listen;\n\n\n/***/ }),\n\n/***/ 817:\n/***/ (function(module) {\n\nfunction select(element) {\n var selectedText;\n\n if (element.nodeName === 'SELECT') {\n element.focus();\n\n selectedText = element.value;\n }\n else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {\n var isReadOnly = element.hasAttribute('readonly');\n\n if (!isReadOnly) {\n element.setAttribute('readonly', '');\n }\n\n element.select();\n element.setSelectionRange(0, element.value.length);\n\n if (!isReadOnly) {\n element.removeAttribute('readonly');\n }\n\n selectedText = element.value;\n }\n else {\n if (element.hasAttribute('contenteditable')) {\n element.focus();\n }\n\n var selection = window.getSelection();\n var range = document.createRange();\n\n range.selectNodeContents(element);\n selection.removeAllRanges();\n selection.addRange(range);\n\n selectedText = selection.toString();\n }\n\n return selectedText;\n}\n\nmodule.exports = select;\n\n\n/***/ }),\n\n/***/ 279:\n/***/ (function(module) {\n\nfunction E () {\n // Keep this empty so it's easier to inherit from\n // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)\n}\n\nE.prototype = {\n on: function (name, callback, ctx) {\n var e = this.e || (this.e = {});\n\n (e[name] || (e[name] = [])).push({\n fn: callback,\n ctx: ctx\n });\n\n return this;\n },\n\n once: function (name, callback, ctx) {\n var self = this;\n function listener () {\n self.off(name, listener);\n callback.apply(ctx, arguments);\n };\n\n listener._ = callback\n return this.on(name, listener, ctx);\n },\n\n emit: function (name) {\n var data = [].slice.call(arguments, 1);\n var evtArr = ((this.e || (this.e = {}))[name] || []).slice();\n var i = 0;\n var len = evtArr.length;\n\n for (i; i < len; i++) {\n evtArr[i].fn.apply(evtArr[i].ctx, data);\n }\n\n return this;\n },\n\n off: function (name, callback) {\n var e = this.e || (this.e = {});\n var evts = e[name];\n var liveEvents = [];\n\n if (evts && callback) {\n for (var i = 0, len = evts.length; i < len; i++) {\n if (evts[i].fn !== callback && evts[i].fn._ !== callback)\n liveEvents.push(evts[i]);\n }\n }\n\n // Remove event from queue to prevent memory leak\n // Suggested by https://github.com/lazd\n // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910\n\n (liveEvents.length)\n ? e[name] = liveEvents\n : delete e[name];\n\n return this;\n }\n};\n\nmodule.exports = E;\nmodule.exports.TinyEmitter = E;\n\n\n/***/ })\n\n/******/ \t});\n/************************************************************************/\n/******/ \t// The module cache\n/******/ \tvar __webpack_module_cache__ = {};\n/******/ \t\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(__webpack_module_cache__[moduleId]) {\n/******/ \t\t\treturn __webpack_module_cache__[moduleId].exports;\n/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = __webpack_module_cache__[moduleId] = {\n/******/ \t\t\t// no module.id needed\n/******/ \t\t\t// no module.loaded needed\n/******/ \t\t\texports: {}\n/******/ \t\t};\n/******/ \t\n/******/ \t\t// Execute the module function\n/******/ \t\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n/******/ \t\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/ \t\n/************************************************************************/\n/******/ \t/* webpack/runtime/compat get default export */\n/******/ \t!function() {\n/******/ \t\t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t\t__webpack_require__.n = function(module) {\n/******/ \t\t\tvar getter = module && module.__esModule ?\n/******/ \t\t\t\tfunction() { return module['default']; } :\n/******/ \t\t\t\tfunction() { return module; };\n/******/ \t\t\t__webpack_require__.d(getter, { a: getter });\n/******/ \t\t\treturn getter;\n/******/ \t\t};\n/******/ \t}();\n/******/ \t\n/******/ \t/* webpack/runtime/define property getters */\n/******/ \t!function() {\n/******/ \t\t// define getter functions for harmony exports\n/******/ \t\t__webpack_require__.d = function(exports, definition) {\n/******/ \t\t\tfor(var key in definition) {\n/******/ \t\t\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n/******/ \t\t\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n/******/ \t\t\t\t}\n/******/ \t\t\t}\n/******/ \t\t};\n/******/ \t}();\n/******/ \t\n/******/ \t/* webpack/runtime/hasOwnProperty shorthand */\n/******/ \t!function() {\n/******/ \t\t__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }\n/******/ \t}();\n/******/ \t\n/************************************************************************/\n/******/ \t// module exports must be returned from runtime so entry inlining is disabled\n/******/ \t// startup\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(686);\n/******/ })()\n.default;\n});", "/*\n * Copyright (c) 2016-2025 Martin Donath \n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to\n * deal in the Software without restriction, including without limitation the\n * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or\n * sell copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n * IN THE SOFTWARE.\n */\n\nimport \"focus-visible\"\n\nimport {\n EMPTY,\n NEVER,\n Observable,\n Subject,\n defer,\n delay,\n filter,\n map,\n merge,\n mergeWith,\n shareReplay,\n switchMap\n} from \"rxjs\"\n\nimport { configuration, feature } from \"./_\"\nimport {\n at,\n getActiveElement,\n getOptionalElement,\n requestJSON,\n setLocation,\n setToggle,\n watchDocument,\n watchKeyboard,\n watchLocation,\n watchLocationTarget,\n watchMedia,\n watchPrint,\n watchScript,\n watchViewport\n} from \"./browser\"\nimport {\n getComponentElement,\n getComponentElements,\n mountAnnounce,\n mountBackToTop,\n mountConsent,\n mountContent,\n mountDialog,\n mountHeader,\n mountHeaderTitle,\n mountPalette,\n mountProgress,\n mountSearch,\n mountSearchHiglight,\n mountSidebar,\n mountSource,\n mountTableOfContents,\n mountTabs,\n watchHeader,\n watchMain\n} from \"./components\"\nimport {\n SearchIndex,\n setupClipboardJS,\n setupInstantNavigation,\n setupVersionSelector\n} from \"./integrations\"\nimport {\n patchEllipsis,\n patchIndeterminate,\n patchScrollfix,\n patchScrolllock\n} from \"./patches\"\nimport \"./polyfills\"\n\n/* ----------------------------------------------------------------------------\n * Functions - @todo refactor\n * ------------------------------------------------------------------------- */\n\n/**\n * Fetch search index\n *\n * @returns Search index observable\n */\nfunction fetchSearchIndex(): Observable {\n if (location.protocol === \"file:\") {\n return watchScript(\n `${new URL(\"search/search_index.js\", config.base)}`\n )\n .pipe(\n // @ts-ignore - @todo fix typings\n map(() => __index),\n shareReplay(1)\n )\n } else {\n return requestJSON(\n new URL(\"search/search_index.json\", config.base)\n )\n }\n}\n\n/* ----------------------------------------------------------------------------\n * Application\n * ------------------------------------------------------------------------- */\n\n/* Yay, JavaScript is available */\ndocument.documentElement.classList.remove(\"no-js\")\ndocument.documentElement.classList.add(\"js\")\n\n/* Set up navigation observables and subjects */\nconst document$ = watchDocument()\nconst location$ = watchLocation()\nconst target$ = watchLocationTarget(location$)\nconst keyboard$ = watchKeyboard()\n\n/* Set up media observables */\nconst viewport$ = watchViewport()\nconst tablet$ = watchMedia(\"(min-width: 960px)\")\nconst screen$ = watchMedia(\"(min-width: 1220px)\")\nconst print$ = watchPrint()\n\n/* Retrieve search index, if search is enabled */\nconst config = configuration()\nconst index$ = document.forms.namedItem(\"search\")\n ? fetchSearchIndex()\n : NEVER\n\n/* Set up Clipboard.js integration */\nconst alert$ = new Subject()\nsetupClipboardJS({ alert$ })\n\n/* Set up progress indicator */\nconst progress$ = new Subject()\n\n/* Set up instant navigation, if enabled */\nif (feature(\"navigation.instant\"))\n setupInstantNavigation({ location$, viewport$, progress$ })\n .subscribe(document$)\n\n/* Set up version selector */\nif (config.version?.provider === \"mike\")\n setupVersionSelector({ document$ })\n\n/* Always close drawer and search on navigation */\nmerge(location$, target$)\n .pipe(\n delay(125)\n )\n .subscribe(() => {\n setToggle(\"drawer\", false)\n setToggle(\"search\", false)\n })\n\n/* Set up global keyboard handlers */\nkeyboard$\n .pipe(\n filter(({ mode }) => mode === \"global\")\n )\n .subscribe(key => {\n switch (key.type) {\n\n /* Go to previous page */\n case \"p\":\n case \",\":\n const prev = getOptionalElement(\"link[rel=prev]\")\n if (typeof prev !== \"undefined\")\n setLocation(prev)\n break\n\n /* Go to next page */\n case \"n\":\n case \".\":\n const next = getOptionalElement(\"link[rel=next]\")\n if (typeof next !== \"undefined\")\n setLocation(next)\n break\n\n /* Expand navigation, see https://bit.ly/3ZjG5io */\n case \"Enter\":\n const active = getActiveElement()\n if (active instanceof HTMLLabelElement)\n active.click()\n }\n })\n\n/* Set up patches */\npatchEllipsis({ viewport$, document$ })\npatchIndeterminate({ document$, tablet$ })\npatchScrollfix({ document$ })\npatchScrolllock({ viewport$, tablet$ })\n\n/* Set up header and main area observable */\nconst header$ = watchHeader(getComponentElement(\"header\"), { viewport$ })\nconst main$ = document$\n .pipe(\n map(() => getComponentElement(\"main\")),\n switchMap(el => watchMain(el, { viewport$, header$ })),\n shareReplay(1)\n )\n\n/* Set up control component observables */\nconst control$ = merge(\n\n /* Consent */\n ...getComponentElements(\"consent\")\n .map(el => mountConsent(el, { target$ })),\n\n /* Dialog */\n ...getComponentElements(\"dialog\")\n .map(el => mountDialog(el, { alert$ })),\n\n /* Color palette */\n ...getComponentElements(\"palette\")\n .map(el => mountPalette(el)),\n\n /* Progress bar */\n ...getComponentElements(\"progress\")\n .map(el => mountProgress(el, { progress$ })),\n\n /* Search */\n ...getComponentElements(\"search\")\n .map(el => mountSearch(el, { index$, keyboard$ })),\n\n /* Repository information */\n ...getComponentElements(\"source\")\n .map(el => mountSource(el))\n)\n\n/* Set up content component observables */\nconst content$ = defer(() => merge(\n\n /* Announcement bar */\n ...getComponentElements(\"announce\")\n .map(el => mountAnnounce(el)),\n\n /* Content */\n ...getComponentElements(\"content\")\n .map(el => mountContent(el, { viewport$, target$, print$ })),\n\n /* Search highlighting */\n ...getComponentElements(\"content\")\n .map(el => feature(\"search.highlight\")\n ? mountSearchHiglight(el, { index$, location$ })\n : EMPTY\n ),\n\n /* Header */\n ...getComponentElements(\"header\")\n .map(el => mountHeader(el, { viewport$, header$, main$ })),\n\n /* Header title */\n ...getComponentElements(\"header-title\")\n .map(el => mountHeaderTitle(el, { viewport$, header$ })),\n\n /* Sidebar */\n ...getComponentElements(\"sidebar\")\n .map(el => el.getAttribute(\"data-md-type\") === \"navigation\"\n ? at(screen$, () => mountSidebar(el, { viewport$, header$, main$ }))\n : at(tablet$, () => mountSidebar(el, { viewport$, header$, main$ }))\n ),\n\n /* Navigation tabs */\n ...getComponentElements(\"tabs\")\n .map(el => mountTabs(el, { viewport$, header$ })),\n\n /* Table of contents */\n ...getComponentElements(\"toc\")\n .map(el => mountTableOfContents(el, {\n viewport$, header$, main$, target$\n })),\n\n /* Back-to-top button */\n ...getComponentElements(\"top\")\n .map(el => mountBackToTop(el, { viewport$, header$, main$, target$ }))\n))\n\n/* Set up component observables */\nconst component$ = document$\n .pipe(\n switchMap(() => content$),\n mergeWith(control$),\n shareReplay(1)\n )\n\n/* Subscribe to all components */\ncomponent$.subscribe()\n\n/* ----------------------------------------------------------------------------\n * Exports\n * ------------------------------------------------------------------------- */\n\nwindow.document$ = document$ /* Document observable */\nwindow.location$ = location$ /* Location subject */\nwindow.target$ = target$ /* Location target observable */\nwindow.keyboard$ = keyboard$ /* Keyboard observable */\nwindow.viewport$ = viewport$ /* Viewport observable */\nwindow.tablet$ = tablet$ /* Media tablet observable */\nwindow.screen$ = screen$ /* Media screen observable */\nwindow.print$ = print$ /* Media print observable */\nwindow.alert$ = alert$ /* Alert subject */\nwindow.progress$ = progress$ /* Progress indicator subject */\nwindow.component$ = component$ /* Component observable */\n", "/******************************************************************************\nCopyright (c) Microsoft Corporation.\n\nPermission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\nPERFORMANCE OF THIS SOFTWARE.\n***************************************************************************** */\n/* global Reflect, Promise, SuppressedError, Symbol, Iterator */\n\nvar extendStatics = function(d, b) {\n extendStatics = Object.setPrototypeOf ||\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\n return extendStatics(d, b);\n};\n\nexport function __extends(d, b) {\n if (typeof b !== \"function\" && b !== null)\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\n extendStatics(d, b);\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n}\n\nexport var __assign = function() {\n __assign = Object.assign || function __assign(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\n }\n return t;\n }\n return __assign.apply(this, arguments);\n}\n\nexport function __rest(s, e) {\n var t = {};\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\n t[p] = s[p];\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\n t[p[i]] = s[p[i]];\n }\n return t;\n}\n\nexport function __decorate(decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n}\n\nexport function __param(paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n}\n\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\n var _, done = false;\n for (var i = decorators.length - 1; i >= 0; i--) {\n var context = {};\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\n if (kind === \"accessor\") {\n if (result === void 0) continue;\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\n if (_ = accept(result.get)) descriptor.get = _;\n if (_ = accept(result.set)) descriptor.set = _;\n if (_ = accept(result.init)) initializers.unshift(_);\n }\n else if (_ = accept(result)) {\n if (kind === \"field\") initializers.unshift(_);\n else descriptor[key] = _;\n }\n }\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\n done = true;\n};\n\nexport function __runInitializers(thisArg, initializers, value) {\n var useValue = arguments.length > 2;\n for (var i = 0; i < initializers.length; i++) {\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\n }\n return useValue ? value : void 0;\n};\n\nexport function __propKey(x) {\n return typeof x === \"symbol\" ? x : \"\".concat(x);\n};\n\nexport function __setFunctionName(f, name, prefix) {\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\n};\n\nexport function __metadata(metadataKey, metadataValue) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\n}\n\nexport function __awaiter(thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n}\n\nexport function __generator(thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === \"function\" ? Iterator : Object).prototype);\n return g.next = verb(0), g[\"throw\"] = verb(1), g[\"return\"] = verb(2), typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n}\n\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n});\n\nexport function __exportStar(m, o) {\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\n}\n\nexport function __values(o) {\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\n if (m) return m.call(o);\n if (o && typeof o.length === \"number\") return {\n next: function () {\n if (o && i >= o.length) o = void 0;\n return { value: o && o[i++], done: !o };\n }\n };\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\n}\n\nexport function __read(o, n) {\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\n if (!m) return o;\n var i = m.call(o), r, ar = [], e;\n try {\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\n }\n catch (error) { e = { error: error }; }\n finally {\n try {\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\n }\n finally { if (e) throw e.error; }\n }\n return ar;\n}\n\n/** @deprecated */\nexport function __spread() {\n for (var ar = [], i = 0; i < arguments.length; i++)\n ar = ar.concat(__read(arguments[i]));\n return ar;\n}\n\n/** @deprecated */\nexport function __spreadArrays() {\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\n r[k] = a[j];\n return r;\n}\n\nexport function __spreadArray(to, from, pack) {\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\n if (ar || !(i in from)) {\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\n ar[i] = from[i];\n }\n }\n return to.concat(ar || Array.prototype.slice.call(from));\n}\n\nexport function __await(v) {\n return this instanceof __await ? (this.v = v, this) : new __await(v);\n}\n\nexport function __asyncGenerator(thisArg, _arguments, generator) {\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\n return i = Object.create((typeof AsyncIterator === \"function\" ? AsyncIterator : Object).prototype), verb(\"next\"), verb(\"throw\"), verb(\"return\", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;\n function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }\n function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\n function fulfill(value) { resume(\"next\", value); }\n function reject(value) { resume(\"throw\", value); }\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\n}\n\nexport function __asyncDelegator(o) {\n var i, p;\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\n}\n\nexport function __asyncValues(o) {\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\n var m = o[Symbol.asyncIterator], i;\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\n}\n\nexport function __makeTemplateObject(cooked, raw) {\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\n return cooked;\n};\n\nvar __setModuleDefault = Object.create ? (function(o, v) {\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\n}) : function(o, v) {\n o[\"default\"] = v;\n};\n\nexport function __importStar(mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\n __setModuleDefault(result, mod);\n return result;\n}\n\nexport function __importDefault(mod) {\n return (mod && mod.__esModule) ? mod : { default: mod };\n}\n\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\n}\n\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\n}\n\nexport function __classPrivateFieldIn(state, receiver) {\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\n}\n\nexport function __addDisposableResource(env, value, async) {\n if (value !== null && value !== void 0) {\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\n var dispose, inner;\n if (async) {\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\n dispose = value[Symbol.asyncDispose];\n }\n if (dispose === void 0) {\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\n dispose = value[Symbol.dispose];\n if (async) inner = dispose;\n }\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\n if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };\n env.stack.push({ value: value, dispose: dispose, async: async });\n }\n else if (async) {\n env.stack.push({ async: true });\n }\n return value;\n}\n\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\n var e = new Error(message);\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\n};\n\nexport function __disposeResources(env) {\n function fail(e) {\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\n env.hasError = true;\n }\n var r, s = 0;\n function next() {\n while (r = env.stack.pop()) {\n try {\n if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);\n if (r.dispose) {\n var result = r.dispose.call(r.value);\n if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\n }\n else s |= 1;\n }\n catch (e) {\n fail(e);\n }\n }\n if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();\n if (env.hasError) throw env.error;\n }\n return next();\n}\n\nexport default {\n __extends,\n __assign,\n __rest,\n __decorate,\n __param,\n __metadata,\n __awaiter,\n __generator,\n __createBinding,\n __exportStar,\n __values,\n __read,\n __spread,\n __spreadArrays,\n __spreadArray,\n __await,\n __asyncGenerator,\n __asyncDelegator,\n __asyncValues,\n __makeTemplateObject,\n __importStar,\n __importDefault,\n __classPrivateFieldGet,\n __classPrivateFieldSet,\n __classPrivateFieldIn,\n __addDisposableResource,\n __disposeResources,\n};\n", "/**\n * Returns true if the object is a function.\n * @param value The value to check\n */\nexport function isFunction(value: any): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n", "/**\n * Used to create Error subclasses until the community moves away from ES5.\n *\n * This is because compiling from TypeScript down to ES5 has issues with subclassing Errors\n * as well as other built-in types: https://github.com/Microsoft/TypeScript/issues/12123\n *\n * @param createImpl A factory function to create the actual constructor implementation. The returned\n * function should be a named function that calls `_super` internally.\n */\nexport function createErrorClass(createImpl: (_super: any) => any): T {\n const _super = (instance: any) => {\n Error.call(instance);\n instance.stack = new Error().stack;\n };\n\n const ctorFunc = createImpl(_super);\n ctorFunc.prototype = Object.create(Error.prototype);\n ctorFunc.prototype.constructor = ctorFunc;\n return ctorFunc;\n}\n", "import { createErrorClass } from './createErrorClass';\n\nexport interface UnsubscriptionError extends Error {\n readonly errors: any[];\n}\n\nexport interface UnsubscriptionErrorCtor {\n /**\n * @deprecated Internal implementation detail. Do not construct error instances.\n * Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269\n */\n new (errors: any[]): UnsubscriptionError;\n}\n\n/**\n * An error thrown when one or more errors have occurred during the\n * `unsubscribe` of a {@link Subscription}.\n */\nexport const UnsubscriptionError: UnsubscriptionErrorCtor = createErrorClass(\n (_super) =>\n function UnsubscriptionErrorImpl(this: any, errors: (Error | string)[]) {\n _super(this);\n this.message = errors\n ? `${errors.length} errors occurred during unsubscription:\n${errors.map((err, i) => `${i + 1}) ${err.toString()}`).join('\\n ')}`\n : '';\n this.name = 'UnsubscriptionError';\n this.errors = errors;\n }\n);\n", "/**\n * Removes an item from an array, mutating it.\n * @param arr The array to remove the item from\n * @param item The item to remove\n */\nexport function arrRemove(arr: T[] | undefined | null, item: T) {\n if (arr) {\n const index = arr.indexOf(item);\n 0 <= index && arr.splice(index, 1);\n }\n}\n", "import { isFunction } from './util/isFunction';\nimport { UnsubscriptionError } from './util/UnsubscriptionError';\nimport { SubscriptionLike, TeardownLogic, Unsubscribable } from './types';\nimport { arrRemove } from './util/arrRemove';\n\n/**\n * Represents a disposable resource, such as the execution of an Observable. A\n * Subscription has one important method, `unsubscribe`, that takes no argument\n * and just disposes the resource held by the subscription.\n *\n * Additionally, subscriptions may be grouped together through the `add()`\n * method, which will attach a child Subscription to the current Subscription.\n * When a Subscription is unsubscribed, all its children (and its grandchildren)\n * will be unsubscribed as well.\n *\n * @class Subscription\n */\nexport class Subscription implements SubscriptionLike {\n /** @nocollapse */\n public static EMPTY = (() => {\n const empty = new Subscription();\n empty.closed = true;\n return empty;\n })();\n\n /**\n * A flag to indicate whether this Subscription has already been unsubscribed.\n */\n public closed = false;\n\n private _parentage: Subscription[] | Subscription | null = null;\n\n /**\n * The list of registered finalizers to execute upon unsubscription. Adding and removing from this\n * list occurs in the {@link #add} and {@link #remove} methods.\n */\n private _finalizers: Exclude[] | null = null;\n\n /**\n * @param initialTeardown A function executed first as part of the finalization\n * process that is kicked off when {@link #unsubscribe} is called.\n */\n constructor(private initialTeardown?: () => void) {}\n\n /**\n * Disposes the resources held by the subscription. May, for instance, cancel\n * an ongoing Observable execution or cancel any other type of work that\n * started when the Subscription was created.\n * @return {void}\n */\n unsubscribe(): void {\n let errors: any[] | undefined;\n\n if (!this.closed) {\n this.closed = true;\n\n // Remove this from it's parents.\n const { _parentage } = this;\n if (_parentage) {\n this._parentage = null;\n if (Array.isArray(_parentage)) {\n for (const parent of _parentage) {\n parent.remove(this);\n }\n } else {\n _parentage.remove(this);\n }\n }\n\n const { initialTeardown: initialFinalizer } = this;\n if (isFunction(initialFinalizer)) {\n try {\n initialFinalizer();\n } catch (e) {\n errors = e instanceof UnsubscriptionError ? e.errors : [e];\n }\n }\n\n const { _finalizers } = this;\n if (_finalizers) {\n this._finalizers = null;\n for (const finalizer of _finalizers) {\n try {\n execFinalizer(finalizer);\n } catch (err) {\n errors = errors ?? [];\n if (err instanceof UnsubscriptionError) {\n errors = [...errors, ...err.errors];\n } else {\n errors.push(err);\n }\n }\n }\n }\n\n if (errors) {\n throw new UnsubscriptionError(errors);\n }\n }\n }\n\n /**\n * Adds a finalizer to this subscription, so that finalization will be unsubscribed/called\n * when this subscription is unsubscribed. If this subscription is already {@link #closed},\n * because it has already been unsubscribed, then whatever finalizer is passed to it\n * will automatically be executed (unless the finalizer itself is also a closed subscription).\n *\n * Closed Subscriptions cannot be added as finalizers to any subscription. Adding a closed\n * subscription to a any subscription will result in no operation. (A noop).\n *\n * Adding a subscription to itself, or adding `null` or `undefined` will not perform any\n * operation at all. (A noop).\n *\n * `Subscription` instances that are added to this instance will automatically remove themselves\n * if they are unsubscribed. Functions and {@link Unsubscribable} objects that you wish to remove\n * will need to be removed manually with {@link #remove}\n *\n * @param teardown The finalization logic to add to this subscription.\n */\n add(teardown: TeardownLogic): void {\n // Only add the finalizer if it's not undefined\n // and don't add a subscription to itself.\n if (teardown && teardown !== this) {\n if (this.closed) {\n // If this subscription is already closed,\n // execute whatever finalizer is handed to it automatically.\n execFinalizer(teardown);\n } else {\n if (teardown instanceof Subscription) {\n // We don't add closed subscriptions, and we don't add the same subscription\n // twice. Subscription unsubscribe is idempotent.\n if (teardown.closed || teardown._hasParent(this)) {\n return;\n }\n teardown._addParent(this);\n }\n (this._finalizers = this._finalizers ?? []).push(teardown);\n }\n }\n }\n\n /**\n * Checks to see if a this subscription already has a particular parent.\n * This will signal that this subscription has already been added to the parent in question.\n * @param parent the parent to check for\n */\n private _hasParent(parent: Subscription) {\n const { _parentage } = this;\n return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent));\n }\n\n /**\n * Adds a parent to this subscription so it can be removed from the parent if it\n * unsubscribes on it's own.\n *\n * NOTE: THIS ASSUMES THAT {@link _hasParent} HAS ALREADY BEEN CHECKED.\n * @param parent The parent subscription to add\n */\n private _addParent(parent: Subscription) {\n const { _parentage } = this;\n this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent;\n }\n\n /**\n * Called on a child when it is removed via {@link #remove}.\n * @param parent The parent to remove\n */\n private _removeParent(parent: Subscription) {\n const { _parentage } = this;\n if (_parentage === parent) {\n this._parentage = null;\n } else if (Array.isArray(_parentage)) {\n arrRemove(_parentage, parent);\n }\n }\n\n /**\n * Removes a finalizer from this subscription that was previously added with the {@link #add} method.\n *\n * Note that `Subscription` instances, when unsubscribed, will automatically remove themselves\n * from every other `Subscription` they have been added to. This means that using the `remove` method\n * is not a common thing and should be used thoughtfully.\n *\n * If you add the same finalizer instance of a function or an unsubscribable object to a `Subscription` instance\n * more than once, you will need to call `remove` the same number of times to remove all instances.\n *\n * All finalizer instances are removed to free up memory upon unsubscription.\n *\n * @param teardown The finalizer to remove from this subscription\n */\n remove(teardown: Exclude): void {\n const { _finalizers } = this;\n _finalizers && arrRemove(_finalizers, teardown);\n\n if (teardown instanceof Subscription) {\n teardown._removeParent(this);\n }\n }\n}\n\nexport const EMPTY_SUBSCRIPTION = Subscription.EMPTY;\n\nexport function isSubscription(value: any): value is Subscription {\n return (\n value instanceof Subscription ||\n (value && 'closed' in value && isFunction(value.remove) && isFunction(value.add) && isFunction(value.unsubscribe))\n );\n}\n\nfunction execFinalizer(finalizer: Unsubscribable | (() => void)) {\n if (isFunction(finalizer)) {\n finalizer();\n } else {\n finalizer.unsubscribe();\n }\n}\n", "import { Subscriber } from './Subscriber';\nimport { ObservableNotification } from './types';\n\n/**\n * The {@link GlobalConfig} object for RxJS. It is used to configure things\n * like how to react on unhandled errors.\n */\nexport const config: GlobalConfig = {\n onUnhandledError: null,\n onStoppedNotification: null,\n Promise: undefined,\n useDeprecatedSynchronousErrorHandling: false,\n useDeprecatedNextContext: false,\n};\n\n/**\n * The global configuration object for RxJS, used to configure things\n * like how to react on unhandled errors. Accessible via {@link config}\n * object.\n */\nexport interface GlobalConfig {\n /**\n * A registration point for unhandled errors from RxJS. These are errors that\n * cannot were not handled by consuming code in the usual subscription path. For\n * example, if you have this configured, and you subscribe to an observable without\n * providing an error handler, errors from that subscription will end up here. This\n * will _always_ be called asynchronously on another job in the runtime. This is because\n * we do not want errors thrown in this user-configured handler to interfere with the\n * behavior of the library.\n */\n onUnhandledError: ((err: any) => void) | null;\n\n /**\n * A registration point for notifications that cannot be sent to subscribers because they\n * have completed, errored or have been explicitly unsubscribed. By default, next, complete\n * and error notifications sent to stopped subscribers are noops. However, sometimes callers\n * might want a different behavior. For example, with sources that attempt to report errors\n * to stopped subscribers, a caller can configure RxJS to throw an unhandled error instead.\n * This will _always_ be called asynchronously on another job in the runtime. This is because\n * we do not want errors thrown in this user-configured handler to interfere with the\n * behavior of the library.\n */\n onStoppedNotification: ((notification: ObservableNotification, subscriber: Subscriber) => void) | null;\n\n /**\n * The promise constructor used by default for {@link Observable#toPromise toPromise} and {@link Observable#forEach forEach}\n * methods.\n *\n * @deprecated As of version 8, RxJS will no longer support this sort of injection of a\n * Promise constructor. If you need a Promise implementation other than native promises,\n * please polyfill/patch Promise as you see appropriate. Will be removed in v8.\n */\n Promise?: PromiseConstructorLike;\n\n /**\n * If true, turns on synchronous error rethrowing, which is a deprecated behavior\n * in v6 and higher. This behavior enables bad patterns like wrapping a subscribe\n * call in a try/catch block. It also enables producer interference, a nasty bug\n * where a multicast can be broken for all observers by a downstream consumer with\n * an unhandled error. DO NOT USE THIS FLAG UNLESS IT'S NEEDED TO BUY TIME\n * FOR MIGRATION REASONS.\n *\n * @deprecated As of version 8, RxJS will no longer support synchronous throwing\n * of unhandled errors. All errors will be thrown on a separate call stack to prevent bad\n * behaviors described above. Will be removed in v8.\n */\n useDeprecatedSynchronousErrorHandling: boolean;\n\n /**\n * If true, enables an as-of-yet undocumented feature from v5: The ability to access\n * `unsubscribe()` via `this` context in `next` functions created in observers passed\n * to `subscribe`.\n *\n * This is being removed because the performance was severely problematic, and it could also cause\n * issues when types other than POJOs are passed to subscribe as subscribers, as they will likely have\n * their `this` context overwritten.\n *\n * @deprecated As of version 8, RxJS will no longer support altering the\n * context of next functions provided as part of an observer to Subscribe. Instead,\n * you will have access to a subscription or a signal or token that will allow you to do things like\n * unsubscribe and test closed status. Will be removed in v8.\n */\n useDeprecatedNextContext: boolean;\n}\n", "import type { TimerHandle } from './timerHandle';\ntype SetTimeoutFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle;\ntype ClearTimeoutFunction = (handle: TimerHandle) => void;\n\ninterface TimeoutProvider {\n setTimeout: SetTimeoutFunction;\n clearTimeout: ClearTimeoutFunction;\n delegate:\n | {\n setTimeout: SetTimeoutFunction;\n clearTimeout: ClearTimeoutFunction;\n }\n | undefined;\n}\n\nexport const timeoutProvider: TimeoutProvider = {\n // When accessing the delegate, use the variable rather than `this` so that\n // the functions can be called without being bound to the provider.\n setTimeout(handler: () => void, timeout?: number, ...args) {\n const { delegate } = timeoutProvider;\n if (delegate?.setTimeout) {\n return delegate.setTimeout(handler, timeout, ...args);\n }\n return setTimeout(handler, timeout, ...args);\n },\n clearTimeout(handle) {\n const { delegate } = timeoutProvider;\n return (delegate?.clearTimeout || clearTimeout)(handle as any);\n },\n delegate: undefined,\n};\n", "import { config } from '../config';\nimport { timeoutProvider } from '../scheduler/timeoutProvider';\n\n/**\n * Handles an error on another job either with the user-configured {@link onUnhandledError},\n * or by throwing it on that new job so it can be picked up by `window.onerror`, `process.on('error')`, etc.\n *\n * This should be called whenever there is an error that is out-of-band with the subscription\n * or when an error hits a terminal boundary of the subscription and no error handler was provided.\n *\n * @param err the error to report\n */\nexport function reportUnhandledError(err: any) {\n timeoutProvider.setTimeout(() => {\n const { onUnhandledError } = config;\n if (onUnhandledError) {\n // Execute the user-configured error handler.\n onUnhandledError(err);\n } else {\n // Throw so it is picked up by the runtime's uncaught error mechanism.\n throw err;\n }\n });\n}\n", "/* tslint:disable:no-empty */\nexport function noop() { }\n", "import { CompleteNotification, NextNotification, ErrorNotification } from './types';\n\n/**\n * A completion object optimized for memory use and created to be the\n * same \"shape\" as other notifications in v8.\n * @internal\n */\nexport const COMPLETE_NOTIFICATION = (() => createNotification('C', undefined, undefined) as CompleteNotification)();\n\n/**\n * Internal use only. Creates an optimized error notification that is the same \"shape\"\n * as other notifications.\n * @internal\n */\nexport function errorNotification(error: any): ErrorNotification {\n return createNotification('E', undefined, error) as any;\n}\n\n/**\n * Internal use only. Creates an optimized next notification that is the same \"shape\"\n * as other notifications.\n * @internal\n */\nexport function nextNotification(value: T) {\n return createNotification('N', value, undefined) as NextNotification;\n}\n\n/**\n * Ensures that all notifications created internally have the same \"shape\" in v8.\n *\n * TODO: This is only exported to support a crazy legacy test in `groupBy`.\n * @internal\n */\nexport function createNotification(kind: 'N' | 'E' | 'C', value: any, error: any) {\n return {\n kind,\n value,\n error,\n };\n}\n", "import { config } from '../config';\n\nlet context: { errorThrown: boolean; error: any } | null = null;\n\n/**\n * Handles dealing with errors for super-gross mode. Creates a context, in which\n * any synchronously thrown errors will be passed to {@link captureError}. Which\n * will record the error such that it will be rethrown after the call back is complete.\n * TODO: Remove in v8\n * @param cb An immediately executed function.\n */\nexport function errorContext(cb: () => void) {\n if (config.useDeprecatedSynchronousErrorHandling) {\n const isRoot = !context;\n if (isRoot) {\n context = { errorThrown: false, error: null };\n }\n cb();\n if (isRoot) {\n const { errorThrown, error } = context!;\n context = null;\n if (errorThrown) {\n throw error;\n }\n }\n } else {\n // This is the general non-deprecated path for everyone that\n // isn't crazy enough to use super-gross mode (useDeprecatedSynchronousErrorHandling)\n cb();\n }\n}\n\n/**\n * Captures errors only in super-gross mode.\n * @param err the error to capture\n */\nexport function captureError(err: any) {\n if (config.useDeprecatedSynchronousErrorHandling && context) {\n context.errorThrown = true;\n context.error = err;\n }\n}\n", "import { isFunction } from './util/isFunction';\nimport { Observer, ObservableNotification } from './types';\nimport { isSubscription, Subscription } from './Subscription';\nimport { config } from './config';\nimport { reportUnhandledError } from './util/reportUnhandledError';\nimport { noop } from './util/noop';\nimport { nextNotification, errorNotification, COMPLETE_NOTIFICATION } from './NotificationFactories';\nimport { timeoutProvider } from './scheduler/timeoutProvider';\nimport { captureError } from './util/errorContext';\n\n/**\n * Implements the {@link Observer} interface and extends the\n * {@link Subscription} class. While the {@link Observer} is the public API for\n * consuming the values of an {@link Observable}, all Observers get converted to\n * a Subscriber, in order to provide Subscription-like capabilities such as\n * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for\n * implementing operators, but it is rarely used as a public API.\n *\n * @class Subscriber\n */\nexport class Subscriber extends Subscription implements Observer {\n /**\n * A static factory for a Subscriber, given a (potentially partial) definition\n * of an Observer.\n * @param next The `next` callback of an Observer.\n * @param error The `error` callback of an\n * Observer.\n * @param complete The `complete` callback of an\n * Observer.\n * @return A Subscriber wrapping the (partially defined)\n * Observer represented by the given arguments.\n * @nocollapse\n * @deprecated Do not use. Will be removed in v8. There is no replacement for this\n * method, and there is no reason to be creating instances of `Subscriber` directly.\n * If you have a specific use case, please file an issue.\n */\n static create(next?: (x?: T) => void, error?: (e?: any) => void, complete?: () => void): Subscriber {\n return new SafeSubscriber(next, error, complete);\n }\n\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n protected isStopped: boolean = false;\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n protected destination: Subscriber | Observer; // this `any` is the escape hatch to erase extra type param (e.g. R)\n\n /**\n * @deprecated Internal implementation detail, do not use directly. Will be made internal in v8.\n * There is no reason to directly create an instance of Subscriber. This type is exported for typings reasons.\n */\n constructor(destination?: Subscriber | Observer) {\n super();\n if (destination) {\n this.destination = destination;\n // Automatically chain subscriptions together here.\n // if destination is a Subscription, then it is a Subscriber.\n if (isSubscription(destination)) {\n destination.add(this);\n }\n } else {\n this.destination = EMPTY_OBSERVER;\n }\n }\n\n /**\n * The {@link Observer} callback to receive notifications of type `next` from\n * the Observable, with a value. The Observable may call this method 0 or more\n * times.\n * @param {T} [value] The `next` value.\n * @return {void}\n */\n next(value?: T): void {\n if (this.isStopped) {\n handleStoppedNotification(nextNotification(value), this);\n } else {\n this._next(value!);\n }\n }\n\n /**\n * The {@link Observer} callback to receive notifications of type `error` from\n * the Observable, with an attached `Error`. Notifies the Observer that\n * the Observable has experienced an error condition.\n * @param {any} [err] The `error` exception.\n * @return {void}\n */\n error(err?: any): void {\n if (this.isStopped) {\n handleStoppedNotification(errorNotification(err), this);\n } else {\n this.isStopped = true;\n this._error(err);\n }\n }\n\n /**\n * The {@link Observer} callback to receive a valueless notification of type\n * `complete` from the Observable. Notifies the Observer that the Observable\n * has finished sending push-based notifications.\n * @return {void}\n */\n complete(): void {\n if (this.isStopped) {\n handleStoppedNotification(COMPLETE_NOTIFICATION, this);\n } else {\n this.isStopped = true;\n this._complete();\n }\n }\n\n unsubscribe(): void {\n if (!this.closed) {\n this.isStopped = true;\n super.unsubscribe();\n this.destination = null!;\n }\n }\n\n protected _next(value: T): void {\n this.destination.next(value);\n }\n\n protected _error(err: any): void {\n try {\n this.destination.error(err);\n } finally {\n this.unsubscribe();\n }\n }\n\n protected _complete(): void {\n try {\n this.destination.complete();\n } finally {\n this.unsubscribe();\n }\n }\n}\n\n/**\n * This bind is captured here because we want to be able to have\n * compatibility with monoid libraries that tend to use a method named\n * `bind`. In particular, a library called Monio requires this.\n */\nconst _bind = Function.prototype.bind;\n\nfunction bind any>(fn: Fn, thisArg: any): Fn {\n return _bind.call(fn, thisArg);\n}\n\n/**\n * Internal optimization only, DO NOT EXPOSE.\n * @internal\n */\nclass ConsumerObserver implements Observer {\n constructor(private partialObserver: Partial>) {}\n\n next(value: T): void {\n const { partialObserver } = this;\n if (partialObserver.next) {\n try {\n partialObserver.next(value);\n } catch (error) {\n handleUnhandledError(error);\n }\n }\n }\n\n error(err: any): void {\n const { partialObserver } = this;\n if (partialObserver.error) {\n try {\n partialObserver.error(err);\n } catch (error) {\n handleUnhandledError(error);\n }\n } else {\n handleUnhandledError(err);\n }\n }\n\n complete(): void {\n const { partialObserver } = this;\n if (partialObserver.complete) {\n try {\n partialObserver.complete();\n } catch (error) {\n handleUnhandledError(error);\n }\n }\n }\n}\n\nexport class SafeSubscriber extends Subscriber {\n constructor(\n observerOrNext?: Partial> | ((value: T) => void) | null,\n error?: ((e?: any) => void) | null,\n complete?: (() => void) | null\n ) {\n super();\n\n let partialObserver: Partial>;\n if (isFunction(observerOrNext) || !observerOrNext) {\n // The first argument is a function, not an observer. The next\n // two arguments *could* be observers, or they could be empty.\n partialObserver = {\n next: (observerOrNext ?? undefined) as (((value: T) => void) | undefined),\n error: error ?? undefined,\n complete: complete ?? undefined,\n };\n } else {\n // The first argument is a partial observer.\n let context: any;\n if (this && config.useDeprecatedNextContext) {\n // This is a deprecated path that made `this.unsubscribe()` available in\n // next handler functions passed to subscribe. This only exists behind a flag\n // now, as it is *very* slow.\n context = Object.create(observerOrNext);\n context.unsubscribe = () => this.unsubscribe();\n partialObserver = {\n next: observerOrNext.next && bind(observerOrNext.next, context),\n error: observerOrNext.error && bind(observerOrNext.error, context),\n complete: observerOrNext.complete && bind(observerOrNext.complete, context),\n };\n } else {\n // The \"normal\" path. Just use the partial observer directly.\n partialObserver = observerOrNext;\n }\n }\n\n // Wrap the partial observer to ensure it's a full observer, and\n // make sure proper error handling is accounted for.\n this.destination = new ConsumerObserver(partialObserver);\n }\n}\n\nfunction handleUnhandledError(error: any) {\n if (config.useDeprecatedSynchronousErrorHandling) {\n captureError(error);\n } else {\n // Ideal path, we report this as an unhandled error,\n // which is thrown on a new call stack.\n reportUnhandledError(error);\n }\n}\n\n/**\n * An error handler used when no error handler was supplied\n * to the SafeSubscriber -- meaning no error handler was supplied\n * do the `subscribe` call on our observable.\n * @param err The error to handle\n */\nfunction defaultErrorHandler(err: any) {\n throw err;\n}\n\n/**\n * A handler for notifications that cannot be sent to a stopped subscriber.\n * @param notification The notification being sent\n * @param subscriber The stopped subscriber\n */\nfunction handleStoppedNotification(notification: ObservableNotification, subscriber: Subscriber) {\n const { onStoppedNotification } = config;\n onStoppedNotification && timeoutProvider.setTimeout(() => onStoppedNotification(notification, subscriber));\n}\n\n/**\n * The observer used as a stub for subscriptions where the user did not\n * pass any arguments to `subscribe`. Comes with the default error handling\n * behavior.\n */\nexport const EMPTY_OBSERVER: Readonly> & { closed: true } = {\n closed: true,\n next: noop,\n error: defaultErrorHandler,\n complete: noop,\n};\n", "/**\n * Symbol.observable or a string \"@@observable\". Used for interop\n *\n * @deprecated We will no longer be exporting this symbol in upcoming versions of RxJS.\n * Instead polyfill and use Symbol.observable directly *or* use https://www.npmjs.com/package/symbol-observable\n */\nexport const observable: string | symbol = (() => (typeof Symbol === 'function' && Symbol.observable) || '@@observable')();\n", "/**\n * This function takes one parameter and just returns it. Simply put,\n * this is like `(x: T): T => x`.\n *\n * ## Examples\n *\n * This is useful in some cases when using things like `mergeMap`\n *\n * ```ts\n * import { interval, take, map, range, mergeMap, identity } from 'rxjs';\n *\n * const source$ = interval(1000).pipe(take(5));\n *\n * const result$ = source$.pipe(\n * map(i => range(i)),\n * mergeMap(identity) // same as mergeMap(x => x)\n * );\n *\n * result$.subscribe({\n * next: console.log\n * });\n * ```\n *\n * Or when you want to selectively apply an operator\n *\n * ```ts\n * import { interval, take, identity } from 'rxjs';\n *\n * const shouldLimit = () => Math.random() < 0.5;\n *\n * const source$ = interval(1000);\n *\n * const result$ = source$.pipe(shouldLimit() ? take(5) : identity);\n *\n * result$.subscribe({\n * next: console.log\n * });\n * ```\n *\n * @param x Any value that is returned by this function\n * @returns The value passed as the first parameter to this function\n */\nexport function identity(x: T): T {\n return x;\n}\n", "import { identity } from './identity';\nimport { UnaryFunction } from '../types';\n\nexport function pipe(): typeof identity;\nexport function pipe(fn1: UnaryFunction): UnaryFunction;\nexport function pipe(fn1: UnaryFunction, fn2: UnaryFunction): UnaryFunction;\nexport function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction\n): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction,\n fn5: UnaryFunction\n): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction,\n fn5: UnaryFunction,\n fn6: UnaryFunction\n): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction,\n fn5: UnaryFunction,\n fn6: UnaryFunction,\n fn7: UnaryFunction\n): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction,\n fn5: UnaryFunction,\n fn6: UnaryFunction,\n fn7: UnaryFunction,\n fn8: UnaryFunction\n): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction,\n fn5: UnaryFunction,\n fn6: UnaryFunction,\n fn7: UnaryFunction,\n fn8: UnaryFunction,\n fn9: UnaryFunction\n): UnaryFunction;\nexport function pipe(\n fn1: UnaryFunction,\n fn2: UnaryFunction,\n fn3: UnaryFunction,\n fn4: UnaryFunction,\n fn5: UnaryFunction,\n fn6: UnaryFunction,\n fn7: UnaryFunction,\n fn8: UnaryFunction,\n fn9: UnaryFunction,\n ...fns: UnaryFunction[]\n): UnaryFunction;\n\n/**\n * pipe() can be called on one or more functions, each of which can take one argument (\"UnaryFunction\")\n * and uses it to return a value.\n * It returns a function that takes one argument, passes it to the first UnaryFunction, and then\n * passes the result to the next one, passes that result to the next one, and so on. \n */\nexport function pipe(...fns: Array>): UnaryFunction {\n return pipeFromArray(fns);\n}\n\n/** @internal */\nexport function pipeFromArray(fns: Array>): UnaryFunction {\n if (fns.length === 0) {\n return identity as UnaryFunction;\n }\n\n if (fns.length === 1) {\n return fns[0];\n }\n\n return function piped(input: T): R {\n return fns.reduce((prev: any, fn: UnaryFunction) => fn(prev), input as any);\n };\n}\n", "import { Operator } from './Operator';\nimport { SafeSubscriber, Subscriber } from './Subscriber';\nimport { isSubscription, Subscription } from './Subscription';\nimport { TeardownLogic, OperatorFunction, Subscribable, Observer } from './types';\nimport { observable as Symbol_observable } from './symbol/observable';\nimport { pipeFromArray } from './util/pipe';\nimport { config } from './config';\nimport { isFunction } from './util/isFunction';\nimport { errorContext } from './util/errorContext';\n\n/**\n * A representation of any set of values over any amount of time. This is the most basic building block\n * of RxJS.\n *\n * @class Observable\n */\nexport class Observable implements Subscribable {\n /**\n * @deprecated Internal implementation detail, do not use directly. Will be made internal in v8.\n */\n source: Observable | undefined;\n\n /**\n * @deprecated Internal implementation detail, do not use directly. Will be made internal in v8.\n */\n operator: Operator | undefined;\n\n /**\n * @constructor\n * @param {Function} subscribe the function that is called when the Observable is\n * initially subscribed to. This function is given a Subscriber, to which new values\n * can be `next`ed, or an `error` method can be called to raise an error, or\n * `complete` can be called to notify of a successful completion.\n */\n constructor(subscribe?: (this: Observable, subscriber: Subscriber) => TeardownLogic) {\n if (subscribe) {\n this._subscribe = subscribe;\n }\n }\n\n // HACK: Since TypeScript inherits static properties too, we have to\n // fight against TypeScript here so Subject can have a different static create signature\n /**\n * Creates a new Observable by calling the Observable constructor\n * @owner Observable\n * @method create\n * @param {Function} subscribe? the subscriber function to be passed to the Observable constructor\n * @return {Observable} a new observable\n * @nocollapse\n * @deprecated Use `new Observable()` instead. Will be removed in v8.\n */\n static create: (...args: any[]) => any = (subscribe?: (subscriber: Subscriber) => TeardownLogic) => {\n return new Observable(subscribe);\n };\n\n /**\n * Creates a new Observable, with this Observable instance as the source, and the passed\n * operator defined as the new observable's operator.\n * @method lift\n * @param operator the operator defining the operation to take on the observable\n * @return a new observable with the Operator applied\n * @deprecated Internal implementation detail, do not use directly. Will be made internal in v8.\n * If you have implemented an operator using `lift`, it is recommended that you create an\n * operator by simply returning `new Observable()` directly. See \"Creating new operators from\n * scratch\" section here: https://rxjs.dev/guide/operators\n */\n lift(operator?: Operator): Observable {\n const observable = new Observable();\n observable.source = this;\n observable.operator = operator;\n return observable;\n }\n\n subscribe(observerOrNext?: Partial> | ((value: T) => void)): Subscription;\n /** @deprecated Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in v8. Details: https://rxjs.dev/deprecations/subscribe-arguments */\n subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Subscription;\n /**\n * Invokes an execution of an Observable and registers Observer handlers for notifications it will emit.\n *\n * Use it when you have all these Observables, but still nothing is happening.\n *\n * `subscribe` is not a regular operator, but a method that calls Observable's internal `subscribe` function. It\n * might be for example a function that you passed to Observable's constructor, but most of the time it is\n * a library implementation, which defines what will be emitted by an Observable, and when it be will emitted. This means\n * that calling `subscribe` is actually the moment when Observable starts its work, not when it is created, as it is often\n * the thought.\n *\n * Apart from starting the execution of an Observable, this method allows you to listen for values\n * that an Observable emits, as well as for when it completes or errors. You can achieve this in two\n * of the following ways.\n *\n * The first way is creating an object that implements {@link Observer} interface. It should have methods\n * defined by that interface, but note that it should be just a regular JavaScript object, which you can create\n * yourself in any way you want (ES6 class, classic function constructor, object literal etc.). In particular, do\n * not attempt to use any RxJS implementation details to create Observers - you don't need them. Remember also\n * that your object does not have to implement all methods. If you find yourself creating a method that doesn't\n * do anything, you can simply omit it. Note however, if the `error` method is not provided and an error happens,\n * it will be thrown asynchronously. Errors thrown asynchronously cannot be caught using `try`/`catch`. Instead,\n * use the {@link onUnhandledError} configuration option or use a runtime handler (like `window.onerror` or\n * `process.on('error)`) to be notified of unhandled errors. Because of this, it's recommended that you provide\n * an `error` method to avoid missing thrown errors.\n *\n * The second way is to give up on Observer object altogether and simply provide callback functions in place of its methods.\n * This means you can provide three functions as arguments to `subscribe`, where the first function is equivalent\n * of a `next` method, the second of an `error` method and the third of a `complete` method. Just as in case of an Observer,\n * if you do not need to listen for something, you can omit a function by passing `undefined` or `null`,\n * since `subscribe` recognizes these functions by where they were placed in function call. When it comes\n * to the `error` function, as with an Observer, if not provided, errors emitted by an Observable will be thrown asynchronously.\n *\n * You can, however, subscribe with no parameters at all. This may be the case where you're not interested in terminal events\n * and you also handled emissions internally by using operators (e.g. using `tap`).\n *\n * Whichever style of calling `subscribe` you use, in both cases it returns a Subscription object.\n * This object allows you to call `unsubscribe` on it, which in turn will stop the work that an Observable does and will clean\n * up all resources that an Observable used. Note that cancelling a subscription will not call `complete` callback\n * provided to `subscribe` function, which is reserved for a regular completion signal that comes from an Observable.\n *\n * Remember that callbacks provided to `subscribe` are not guaranteed to be called asynchronously.\n * It is an Observable itself that decides when these functions will be called. For example {@link of}\n * by default emits all its values synchronously. Always check documentation for how given Observable\n * will behave when subscribed and if its default behavior can be modified with a `scheduler`.\n *\n * #### Examples\n *\n * Subscribe with an {@link guide/observer Observer}\n *\n * ```ts\n * import { of } from 'rxjs';\n *\n * const sumObserver = {\n * sum: 0,\n * next(value) {\n * console.log('Adding: ' + value);\n * this.sum = this.sum + value;\n * },\n * error() {\n * // We actually could just remove this method,\n * // since we do not really care about errors right now.\n * },\n * complete() {\n * console.log('Sum equals: ' + this.sum);\n * }\n * };\n *\n * of(1, 2, 3) // Synchronously emits 1, 2, 3 and then completes.\n * .subscribe(sumObserver);\n *\n * // Logs:\n * // 'Adding: 1'\n * // 'Adding: 2'\n * // 'Adding: 3'\n * // 'Sum equals: 6'\n * ```\n *\n * Subscribe with functions ({@link deprecations/subscribe-arguments deprecated})\n *\n * ```ts\n * import { of } from 'rxjs'\n *\n * let sum = 0;\n *\n * of(1, 2, 3).subscribe(\n * value => {\n * console.log('Adding: ' + value);\n * sum = sum + value;\n * },\n * undefined,\n * () => console.log('Sum equals: ' + sum)\n * );\n *\n * // Logs:\n * // 'Adding: 1'\n * // 'Adding: 2'\n * // 'Adding: 3'\n * // 'Sum equals: 6'\n * ```\n *\n * Cancel a subscription\n *\n * ```ts\n * import { interval } from 'rxjs';\n *\n * const subscription = interval(1000).subscribe({\n * next(num) {\n * console.log(num)\n * },\n * complete() {\n * // Will not be called, even when cancelling subscription.\n * console.log('completed!');\n * }\n * });\n *\n * setTimeout(() => {\n * subscription.unsubscribe();\n * console.log('unsubscribed!');\n * }, 2500);\n *\n * // Logs:\n * // 0 after 1s\n * // 1 after 2s\n * // 'unsubscribed!' after 2.5s\n * ```\n *\n * @param {Observer|Function} observerOrNext (optional) Either an observer with methods to be called,\n * or the first of three possible handlers, which is the handler for each value emitted from the subscribed\n * Observable.\n * @param {Function} error (optional) A handler for a terminal event resulting from an error. If no error handler is provided,\n * the error will be thrown asynchronously as unhandled.\n * @param {Function} complete (optional) A handler for a terminal event resulting from successful completion.\n * @return {Subscription} a subscription reference to the registered handlers\n * @method subscribe\n */\n subscribe(\n observerOrNext?: Partial> | ((value: T) => void) | null,\n error?: ((error: any) => void) | null,\n complete?: (() => void) | null\n ): Subscription {\n const subscriber = isSubscriber(observerOrNext) ? observerOrNext : new SafeSubscriber(observerOrNext, error, complete);\n\n errorContext(() => {\n const { operator, source } = this;\n subscriber.add(\n operator\n ? // We're dealing with a subscription in the\n // operator chain to one of our lifted operators.\n operator.call(subscriber, source)\n : source\n ? // If `source` has a value, but `operator` does not, something that\n // had intimate knowledge of our API, like our `Subject`, must have\n // set it. We're going to just call `_subscribe` directly.\n this._subscribe(subscriber)\n : // In all other cases, we're likely wrapping a user-provided initializer\n // function, so we need to catch errors and handle them appropriately.\n this._trySubscribe(subscriber)\n );\n });\n\n return subscriber;\n }\n\n /** @internal */\n protected _trySubscribe(sink: Subscriber): TeardownLogic {\n try {\n return this._subscribe(sink);\n } catch (err) {\n // We don't need to return anything in this case,\n // because it's just going to try to `add()` to a subscription\n // above.\n sink.error(err);\n }\n }\n\n /**\n * Used as a NON-CANCELLABLE means of subscribing to an observable, for use with\n * APIs that expect promises, like `async/await`. You cannot unsubscribe from this.\n *\n * **WARNING**: Only use this with observables you *know* will complete. If the source\n * observable does not complete, you will end up with a promise that is hung up, and\n * potentially all of the state of an async function hanging out in memory. To avoid\n * this situation, look into adding something like {@link timeout}, {@link take},\n * {@link takeWhile}, or {@link takeUntil} amongst others.\n *\n * #### Example\n *\n * ```ts\n * import { interval, take } from 'rxjs';\n *\n * const source$ = interval(1000).pipe(take(4));\n *\n * async function getTotal() {\n * let total = 0;\n *\n * await source$.forEach(value => {\n * total += value;\n * console.log('observable -> ' + value);\n * });\n *\n * return total;\n * }\n *\n * getTotal().then(\n * total => console.log('Total: ' + total)\n * );\n *\n * // Expected:\n * // 'observable -> 0'\n * // 'observable -> 1'\n * // 'observable -> 2'\n * // 'observable -> 3'\n * // 'Total: 6'\n * ```\n *\n * @param next a handler for each value emitted by the observable\n * @return a promise that either resolves on observable completion or\n * rejects with the handled error\n */\n forEach(next: (value: T) => void): Promise;\n\n /**\n * @param next a handler for each value emitted by the observable\n * @param promiseCtor a constructor function used to instantiate the Promise\n * @return a promise that either resolves on observable completion or\n * rejects with the handled error\n * @deprecated Passing a Promise constructor will no longer be available\n * in upcoming versions of RxJS. This is because it adds weight to the library, for very\n * little benefit. If you need this functionality, it is recommended that you either\n * polyfill Promise, or you create an adapter to convert the returned native promise\n * to whatever promise implementation you wanted. Will be removed in v8.\n */\n forEach(next: (value: T) => void, promiseCtor: PromiseConstructorLike): Promise;\n\n forEach(next: (value: T) => void, promiseCtor?: PromiseConstructorLike): Promise {\n promiseCtor = getPromiseCtor(promiseCtor);\n\n return new promiseCtor((resolve, reject) => {\n const subscriber = new SafeSubscriber({\n next: (value) => {\n try {\n next(value);\n } catch (err) {\n reject(err);\n subscriber.unsubscribe();\n }\n },\n error: reject,\n complete: resolve,\n });\n this.subscribe(subscriber);\n }) as Promise;\n }\n\n /** @internal */\n protected _subscribe(subscriber: Subscriber): TeardownLogic {\n return this.source?.subscribe(subscriber);\n }\n\n /**\n * An interop point defined by the es7-observable spec https://github.com/zenparsing/es-observable\n * @method Symbol.observable\n * @return {Observable} this instance of the observable\n */\n [Symbol_observable]() {\n return this;\n }\n\n /* tslint:disable:max-line-length */\n pipe(): Observable;\n pipe(op1: OperatorFunction): Observable;\n pipe(op1: OperatorFunction, op2: OperatorFunction): Observable;\n pipe(op1: OperatorFunction, op2: OperatorFunction, op3: OperatorFunction): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction\n ): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction,\n op5: OperatorFunction\n ): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction,\n op5: OperatorFunction,\n op6: OperatorFunction\n ): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction,\n op5: OperatorFunction,\n op6: OperatorFunction,\n op7: OperatorFunction\n ): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction,\n op5: OperatorFunction,\n op6: OperatorFunction,\n op7: OperatorFunction,\n op8: OperatorFunction\n ): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction,\n op5: OperatorFunction,\n op6: OperatorFunction,\n op7: OperatorFunction,\n op8: OperatorFunction,\n op9: OperatorFunction\n ): Observable;\n pipe(\n op1: OperatorFunction,\n op2: OperatorFunction,\n op3: OperatorFunction,\n op4: OperatorFunction,\n op5: OperatorFunction,\n op6: OperatorFunction,\n op7: OperatorFunction,\n op8: OperatorFunction,\n op9: OperatorFunction,\n ...operations: OperatorFunction[]\n ): Observable;\n /* tslint:enable:max-line-length */\n\n /**\n * Used to stitch together functional operators into a chain.\n * @method pipe\n * @return {Observable} the Observable result of all of the operators having\n * been called in the order they were passed in.\n *\n * ## Example\n *\n * ```ts\n * import { interval, filter, map, scan } from 'rxjs';\n *\n * interval(1000)\n * .pipe(\n * filter(x => x % 2 === 0),\n * map(x => x + x),\n * scan((acc, x) => acc + x)\n * )\n * .subscribe(x => console.log(x));\n * ```\n */\n pipe(...operations: OperatorFunction[]): Observable {\n return pipeFromArray(operations)(this);\n }\n\n /* tslint:disable:max-line-length */\n /** @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise */\n toPromise(): Promise;\n /** @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise */\n toPromise(PromiseCtor: typeof Promise): Promise;\n /** @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise */\n toPromise(PromiseCtor: PromiseConstructorLike): Promise;\n /* tslint:enable:max-line-length */\n\n /**\n * Subscribe to this Observable and get a Promise resolving on\n * `complete` with the last emission (if any).\n *\n * **WARNING**: Only use this with observables you *know* will complete. If the source\n * observable does not complete, you will end up with a promise that is hung up, and\n * potentially all of the state of an async function hanging out in memory. To avoid\n * this situation, look into adding something like {@link timeout}, {@link take},\n * {@link takeWhile}, or {@link takeUntil} amongst others.\n *\n * @method toPromise\n * @param [promiseCtor] a constructor function used to instantiate\n * the Promise\n * @return A Promise that resolves with the last value emit, or\n * rejects on an error. If there were no emissions, Promise\n * resolves with undefined.\n * @deprecated Replaced with {@link firstValueFrom} and {@link lastValueFrom}. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise\n */\n toPromise(promiseCtor?: PromiseConstructorLike): Promise {\n promiseCtor = getPromiseCtor(promiseCtor);\n\n return new promiseCtor((resolve, reject) => {\n let value: T | undefined;\n this.subscribe(\n (x: T) => (value = x),\n (err: any) => reject(err),\n () => resolve(value)\n );\n }) as Promise;\n }\n}\n\n/**\n * Decides between a passed promise constructor from consuming code,\n * A default configured promise constructor, and the native promise\n * constructor and returns it. If nothing can be found, it will throw\n * an error.\n * @param promiseCtor The optional promise constructor to passed by consuming code\n */\nfunction getPromiseCtor(promiseCtor: PromiseConstructorLike | undefined) {\n return promiseCtor ?? config.Promise ?? Promise;\n}\n\nfunction isObserver(value: any): value is Observer {\n return value && isFunction(value.next) && isFunction(value.error) && isFunction(value.complete);\n}\n\nfunction isSubscriber(value: any): value is Subscriber {\n return (value && value instanceof Subscriber) || (isObserver(value) && isSubscription(value));\n}\n", "import { Observable } from '../Observable';\nimport { Subscriber } from '../Subscriber';\nimport { OperatorFunction } from '../types';\nimport { isFunction } from './isFunction';\n\n/**\n * Used to determine if an object is an Observable with a lift function.\n */\nexport function hasLift(source: any): source is { lift: InstanceType['lift'] } {\n return isFunction(source?.lift);\n}\n\n/**\n * Creates an `OperatorFunction`. Used to define operators throughout the library in a concise way.\n * @param init The logic to connect the liftedSource to the subscriber at the moment of subscription.\n */\nexport function operate(\n init: (liftedSource: Observable, subscriber: Subscriber) => (() => void) | void\n): OperatorFunction {\n return (source: Observable) => {\n if (hasLift(source)) {\n return source.lift(function (this: Subscriber, liftedSource: Observable) {\n try {\n return init(liftedSource, this);\n } catch (err) {\n this.error(err);\n }\n });\n }\n throw new TypeError('Unable to lift unknown Observable type');\n };\n}\n", "import { Subscriber } from '../Subscriber';\n\n/**\n * Creates an instance of an `OperatorSubscriber`.\n * @param destination The downstream subscriber.\n * @param onNext Handles next values, only called if this subscriber is not stopped or closed. Any\n * error that occurs in this function is caught and sent to the `error` method of this subscriber.\n * @param onError Handles errors from the subscription, any errors that occur in this handler are caught\n * and send to the `destination` error handler.\n * @param onComplete Handles completion notification from the subscription. Any errors that occur in\n * this handler are sent to the `destination` error handler.\n * @param onFinalize Additional teardown logic here. This will only be called on teardown if the\n * subscriber itself is not already closed. This is called after all other teardown logic is executed.\n */\nexport function createOperatorSubscriber(\n destination: Subscriber,\n onNext?: (value: T) => void,\n onComplete?: () => void,\n onError?: (err: any) => void,\n onFinalize?: () => void\n): Subscriber {\n return new OperatorSubscriber(destination, onNext, onComplete, onError, onFinalize);\n}\n\n/**\n * A generic helper for allowing operators to be created with a Subscriber and\n * use closures to capture necessary state from the operator function itself.\n */\nexport class OperatorSubscriber extends Subscriber {\n /**\n * Creates an instance of an `OperatorSubscriber`.\n * @param destination The downstream subscriber.\n * @param onNext Handles next values, only called if this subscriber is not stopped or closed. Any\n * error that occurs in this function is caught and sent to the `error` method of this subscriber.\n * @param onError Handles errors from the subscription, any errors that occur in this handler are caught\n * and send to the `destination` error handler.\n * @param onComplete Handles completion notification from the subscription. Any errors that occur in\n * this handler are sent to the `destination` error handler.\n * @param onFinalize Additional finalization logic here. This will only be called on finalization if the\n * subscriber itself is not already closed. This is called after all other finalization logic is executed.\n * @param shouldUnsubscribe An optional check to see if an unsubscribe call should truly unsubscribe.\n * NOTE: This currently **ONLY** exists to support the strange behavior of {@link groupBy}, where unsubscription\n * to the resulting observable does not actually disconnect from the source if there are active subscriptions\n * to any grouped observable. (DO NOT EXPOSE OR USE EXTERNALLY!!!)\n */\n constructor(\n destination: Subscriber,\n onNext?: (value: T) => void,\n onComplete?: () => void,\n onError?: (err: any) => void,\n private onFinalize?: () => void,\n private shouldUnsubscribe?: () => boolean\n ) {\n // It's important - for performance reasons - that all of this class's\n // members are initialized and that they are always initialized in the same\n // order. This will ensure that all OperatorSubscriber instances have the\n // same hidden class in V8. This, in turn, will help keep the number of\n // hidden classes involved in property accesses within the base class as\n // low as possible. If the number of hidden classes involved exceeds four,\n // the property accesses will become megamorphic and performance penalties\n // will be incurred - i.e. inline caches won't be used.\n //\n // The reasons for ensuring all instances have the same hidden class are\n // further discussed in this blog post from Benedikt Meurer:\n // https://benediktmeurer.de/2018/03/23/impact-of-polymorphism-on-component-based-frameworks-like-react/\n super(destination);\n this._next = onNext\n ? function (this: OperatorSubscriber, value: T) {\n try {\n onNext(value);\n } catch (err) {\n destination.error(err);\n }\n }\n : super._next;\n this._error = onError\n ? function (this: OperatorSubscriber, err: any) {\n try {\n onError(err);\n } catch (err) {\n // Send any errors that occur down stream.\n destination.error(err);\n } finally {\n // Ensure finalization.\n this.unsubscribe();\n }\n }\n : super._error;\n this._complete = onComplete\n ? function (this: OperatorSubscriber) {\n try {\n onComplete();\n } catch (err) {\n // Send any errors that occur down stream.\n destination.error(err);\n } finally {\n // Ensure finalization.\n this.unsubscribe();\n }\n }\n : super._complete;\n }\n\n unsubscribe() {\n if (!this.shouldUnsubscribe || this.shouldUnsubscribe()) {\n const { closed } = this;\n super.unsubscribe();\n // Execute additional teardown if we have any and we didn't already do so.\n !closed && this.onFinalize?.();\n }\n }\n}\n", "import { Subscription } from '../Subscription';\n\ninterface AnimationFrameProvider {\n schedule(callback: FrameRequestCallback): Subscription;\n requestAnimationFrame: typeof requestAnimationFrame;\n cancelAnimationFrame: typeof cancelAnimationFrame;\n delegate:\n | {\n requestAnimationFrame: typeof requestAnimationFrame;\n cancelAnimationFrame: typeof cancelAnimationFrame;\n }\n | undefined;\n}\n\nexport const animationFrameProvider: AnimationFrameProvider = {\n // When accessing the delegate, use the variable rather than `this` so that\n // the functions can be called without being bound to the provider.\n schedule(callback) {\n let request = requestAnimationFrame;\n let cancel: typeof cancelAnimationFrame | undefined = cancelAnimationFrame;\n const { delegate } = animationFrameProvider;\n if (delegate) {\n request = delegate.requestAnimationFrame;\n cancel = delegate.cancelAnimationFrame;\n }\n const handle = request((timestamp) => {\n // Clear the cancel function. The request has been fulfilled, so\n // attempting to cancel the request upon unsubscription would be\n // pointless.\n cancel = undefined;\n callback(timestamp);\n });\n return new Subscription(() => cancel?.(handle));\n },\n requestAnimationFrame(...args) {\n const { delegate } = animationFrameProvider;\n return (delegate?.requestAnimationFrame || requestAnimationFrame)(...args);\n },\n cancelAnimationFrame(...args) {\n const { delegate } = animationFrameProvider;\n return (delegate?.cancelAnimationFrame || cancelAnimationFrame)(...args);\n },\n delegate: undefined,\n};\n", "import { createErrorClass } from './createErrorClass';\n\nexport interface ObjectUnsubscribedError extends Error {}\n\nexport interface ObjectUnsubscribedErrorCtor {\n /**\n * @deprecated Internal implementation detail. Do not construct error instances.\n * Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269\n */\n new (): ObjectUnsubscribedError;\n}\n\n/**\n * An error thrown when an action is invalid because the object has been\n * unsubscribed.\n *\n * @see {@link Subject}\n * @see {@link BehaviorSubject}\n *\n * @class ObjectUnsubscribedError\n */\nexport const ObjectUnsubscribedError: ObjectUnsubscribedErrorCtor = createErrorClass(\n (_super) =>\n function ObjectUnsubscribedErrorImpl(this: any) {\n _super(this);\n this.name = 'ObjectUnsubscribedError';\n this.message = 'object unsubscribed';\n }\n);\n", "import { Operator } from './Operator';\nimport { Observable } from './Observable';\nimport { Subscriber } from './Subscriber';\nimport { Subscription, EMPTY_SUBSCRIPTION } from './Subscription';\nimport { Observer, SubscriptionLike, TeardownLogic } from './types';\nimport { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';\nimport { arrRemove } from './util/arrRemove';\nimport { errorContext } from './util/errorContext';\n\n/**\n * A Subject is a special type of Observable that allows values to be\n * multicasted to many Observers. Subjects are like EventEmitters.\n *\n * Every Subject is an Observable and an Observer. You can subscribe to a\n * Subject, and you can call next to feed values as well as error and complete.\n */\nexport class Subject extends Observable implements SubscriptionLike {\n closed = false;\n\n private currentObservers: Observer[] | null = null;\n\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n observers: Observer[] = [];\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n isStopped = false;\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n hasError = false;\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n thrownError: any = null;\n\n /**\n * Creates a \"subject\" by basically gluing an observer to an observable.\n *\n * @nocollapse\n * @deprecated Recommended you do not use. Will be removed at some point in the future. Plans for replacement still under discussion.\n */\n static create: (...args: any[]) => any = (destination: Observer, source: Observable): AnonymousSubject => {\n return new AnonymousSubject(destination, source);\n };\n\n constructor() {\n // NOTE: This must be here to obscure Observable's constructor.\n super();\n }\n\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n lift(operator: Operator): Observable {\n const subject = new AnonymousSubject(this, this);\n subject.operator = operator as any;\n return subject as any;\n }\n\n /** @internal */\n protected _throwIfClosed() {\n if (this.closed) {\n throw new ObjectUnsubscribedError();\n }\n }\n\n next(value: T) {\n errorContext(() => {\n this._throwIfClosed();\n if (!this.isStopped) {\n if (!this.currentObservers) {\n this.currentObservers = Array.from(this.observers);\n }\n for (const observer of this.currentObservers) {\n observer.next(value);\n }\n }\n });\n }\n\n error(err: any) {\n errorContext(() => {\n this._throwIfClosed();\n if (!this.isStopped) {\n this.hasError = this.isStopped = true;\n this.thrownError = err;\n const { observers } = this;\n while (observers.length) {\n observers.shift()!.error(err);\n }\n }\n });\n }\n\n complete() {\n errorContext(() => {\n this._throwIfClosed();\n if (!this.isStopped) {\n this.isStopped = true;\n const { observers } = this;\n while (observers.length) {\n observers.shift()!.complete();\n }\n }\n });\n }\n\n unsubscribe() {\n this.isStopped = this.closed = true;\n this.observers = this.currentObservers = null!;\n }\n\n get observed() {\n return this.observers?.length > 0;\n }\n\n /** @internal */\n protected _trySubscribe(subscriber: Subscriber): TeardownLogic {\n this._throwIfClosed();\n return super._trySubscribe(subscriber);\n }\n\n /** @internal */\n protected _subscribe(subscriber: Subscriber): Subscription {\n this._throwIfClosed();\n this._checkFinalizedStatuses(subscriber);\n return this._innerSubscribe(subscriber);\n }\n\n /** @internal */\n protected _innerSubscribe(subscriber: Subscriber) {\n const { hasError, isStopped, observers } = this;\n if (hasError || isStopped) {\n return EMPTY_SUBSCRIPTION;\n }\n this.currentObservers = null;\n observers.push(subscriber);\n return new Subscription(() => {\n this.currentObservers = null;\n arrRemove(observers, subscriber);\n });\n }\n\n /** @internal */\n protected _checkFinalizedStatuses(subscriber: Subscriber) {\n const { hasError, thrownError, isStopped } = this;\n if (hasError) {\n subscriber.error(thrownError);\n } else if (isStopped) {\n subscriber.complete();\n }\n }\n\n /**\n * Creates a new Observable with this Subject as the source. You can do this\n * to create custom Observer-side logic of the Subject and conceal it from\n * code that uses the Observable.\n * @return {Observable} Observable that the Subject casts to\n */\n asObservable(): Observable {\n const observable: any = new Observable();\n observable.source = this;\n return observable;\n }\n}\n\n/**\n * @class AnonymousSubject\n */\nexport class AnonymousSubject extends Subject {\n constructor(\n /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */\n public destination?: Observer,\n source?: Observable\n ) {\n super();\n this.source = source;\n }\n\n next(value: T) {\n this.destination?.next?.(value);\n }\n\n error(err: any) {\n this.destination?.error?.(err);\n }\n\n complete() {\n this.destination?.complete?.();\n }\n\n /** @internal */\n protected _subscribe(subscriber: Subscriber): Subscription {\n return this.source?.subscribe(subscriber) ?? EMPTY_SUBSCRIPTION;\n }\n}\n", "import { Subject } from './Subject';\nimport { Subscriber } from './Subscriber';\nimport { Subscription } from './Subscription';\n\n/**\n * A variant of Subject that requires an initial value and emits its current\n * value whenever it is subscribed to.\n *\n * @class BehaviorSubject\n */\nexport class BehaviorSubject extends Subject {\n constructor(private _value: T) {\n super();\n }\n\n get value(): T {\n return this.getValue();\n }\n\n /** @internal */\n protected _subscribe(subscriber: Subscriber): Subscription {\n const subscription = super._subscribe(subscriber);\n !subscription.closed && subscriber.next(this._value);\n return subscription;\n }\n\n getValue(): T {\n const { hasError, thrownError, _value } = this;\n if (hasError) {\n throw thrownError;\n }\n this._throwIfClosed();\n return _value;\n }\n\n next(value: T): void {\n super.next((this._value = value));\n }\n}\n", "import { TimestampProvider } from '../types';\n\ninterface DateTimestampProvider extends TimestampProvider {\n delegate: TimestampProvider | undefined;\n}\n\nexport const dateTimestampProvider: DateTimestampProvider = {\n now() {\n // Use the variable rather than `this` so that the function can be called\n // without being bound to the provider.\n return (dateTimestampProvider.delegate || Date).now();\n },\n delegate: undefined,\n};\n", "import { Subject } from './Subject';\nimport { TimestampProvider } from './types';\nimport { Subscriber } from './Subscriber';\nimport { Subscription } from './Subscription';\nimport { dateTimestampProvider } from './scheduler/dateTimestampProvider';\n\n/**\n * A variant of {@link Subject} that \"replays\" old values to new subscribers by emitting them when they first subscribe.\n *\n * `ReplaySubject` has an internal buffer that will store a specified number of values that it has observed. Like `Subject`,\n * `ReplaySubject` \"observes\" values by having them passed to its `next` method. When it observes a value, it will store that\n * value for a time determined by the configuration of the `ReplaySubject`, as passed to its constructor.\n *\n * When a new subscriber subscribes to the `ReplaySubject` instance, it will synchronously emit all values in its buffer in\n * a First-In-First-Out (FIFO) manner. The `ReplaySubject` will also complete, if it has observed completion; and it will\n * error if it has observed an error.\n *\n * There are two main configuration items to be concerned with:\n *\n * 1. `bufferSize` - This will determine how many items are stored in the buffer, defaults to infinite.\n * 2. `windowTime` - The amount of time to hold a value in the buffer before removing it from the buffer.\n *\n * Both configurations may exist simultaneously. So if you would like to buffer a maximum of 3 values, as long as the values\n * are less than 2 seconds old, you could do so with a `new ReplaySubject(3, 2000)`.\n *\n * ### Differences with BehaviorSubject\n *\n * `BehaviorSubject` is similar to `new ReplaySubject(1)`, with a couple of exceptions:\n *\n * 1. `BehaviorSubject` comes \"primed\" with a single value upon construction.\n * 2. `ReplaySubject` will replay values, even after observing an error, where `BehaviorSubject` will not.\n *\n * @see {@link Subject}\n * @see {@link BehaviorSubject}\n * @see {@link shareReplay}\n */\nexport class ReplaySubject extends Subject {\n private _buffer: (T | number)[] = [];\n private _infiniteTimeWindow = true;\n\n /**\n * @param bufferSize The size of the buffer to replay on subscription\n * @param windowTime The amount of time the buffered items will stay buffered\n * @param timestampProvider An object with a `now()` method that provides the current timestamp. This is used to\n * calculate the amount of time something has been buffered.\n */\n constructor(\n private _bufferSize = Infinity,\n private _windowTime = Infinity,\n private _timestampProvider: TimestampProvider = dateTimestampProvider\n ) {\n super();\n this._infiniteTimeWindow = _windowTime === Infinity;\n this._bufferSize = Math.max(1, _bufferSize);\n this._windowTime = Math.max(1, _windowTime);\n }\n\n next(value: T): void {\n const { isStopped, _buffer, _infiniteTimeWindow, _timestampProvider, _windowTime } = this;\n if (!isStopped) {\n _buffer.push(value);\n !_infiniteTimeWindow && _buffer.push(_timestampProvider.now() + _windowTime);\n }\n this._trimBuffer();\n super.next(value);\n }\n\n /** @internal */\n protected _subscribe(subscriber: Subscriber): Subscription {\n this._throwIfClosed();\n this._trimBuffer();\n\n const subscription = this._innerSubscribe(subscriber);\n\n const { _infiniteTimeWindow, _buffer } = this;\n // We use a copy here, so reentrant code does not mutate our array while we're\n // emitting it to a new subscriber.\n const copy = _buffer.slice();\n for (let i = 0; i < copy.length && !subscriber.closed; i += _infiniteTimeWindow ? 1 : 2) {\n subscriber.next(copy[i] as T);\n }\n\n this._checkFinalizedStatuses(subscriber);\n\n return subscription;\n }\n\n private _trimBuffer() {\n const { _bufferSize, _timestampProvider, _buffer, _infiniteTimeWindow } = this;\n // If we don't have an infinite buffer size, and we're over the length,\n // use splice to truncate the old buffer values off. Note that we have to\n // double the size for instances where we're not using an infinite time window\n // because we're storing the values and the timestamps in the same array.\n const adjustedBufferSize = (_infiniteTimeWindow ? 1 : 2) * _bufferSize;\n _bufferSize < Infinity && adjustedBufferSize < _buffer.length && _buffer.splice(0, _buffer.length - adjustedBufferSize);\n\n // Now, if we're not in an infinite time window, remove all values where the time is\n // older than what is allowed.\n if (!_infiniteTimeWindow) {\n const now = _timestampProvider.now();\n let last = 0;\n // Search the array for the first timestamp that isn't expired and\n // truncate the buffer up to that point.\n for (let i = 1; i < _buffer.length && (_buffer[i] as number) <= now; i += 2) {\n last = i;\n }\n last && _buffer.splice(0, last + 1);\n }\n }\n}\n", "import { Scheduler } from '../Scheduler';\nimport { Subscription } from '../Subscription';\nimport { SchedulerAction } from '../types';\n\n/**\n * A unit of work to be executed in a `scheduler`. An action is typically\n * created from within a {@link SchedulerLike} and an RxJS user does not need to concern\n * themselves about creating and manipulating an Action.\n *\n * ```ts\n * class Action extends Subscription {\n * new (scheduler: Scheduler, work: (state?: T) => void);\n * schedule(state?: T, delay: number = 0): Subscription;\n * }\n * ```\n *\n * @class Action\n */\nexport class Action extends Subscription {\n constructor(scheduler: Scheduler, work: (this: SchedulerAction, state?: T) => void) {\n super();\n }\n /**\n * Schedules this action on its parent {@link SchedulerLike} for execution. May be passed\n * some context object, `state`. May happen at some point in the future,\n * according to the `delay` parameter, if specified.\n * @param {T} [state] Some contextual data that the `work` function uses when\n * called by the Scheduler.\n * @param {number} [delay] Time to wait before executing the work, where the\n * time unit is implicit and defined by the Scheduler.\n * @return {void}\n */\n public schedule(state?: T, delay: number = 0): Subscription {\n return this;\n }\n}\n", "import type { TimerHandle } from './timerHandle';\ntype SetIntervalFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle;\ntype ClearIntervalFunction = (handle: TimerHandle) => void;\n\ninterface IntervalProvider {\n setInterval: SetIntervalFunction;\n clearInterval: ClearIntervalFunction;\n delegate:\n | {\n setInterval: SetIntervalFunction;\n clearInterval: ClearIntervalFunction;\n }\n | undefined;\n}\n\nexport const intervalProvider: IntervalProvider = {\n // When accessing the delegate, use the variable rather than `this` so that\n // the functions can be called without being bound to the provider.\n setInterval(handler: () => void, timeout?: number, ...args) {\n const { delegate } = intervalProvider;\n if (delegate?.setInterval) {\n return delegate.setInterval(handler, timeout, ...args);\n }\n return setInterval(handler, timeout, ...args);\n },\n clearInterval(handle) {\n const { delegate } = intervalProvider;\n return (delegate?.clearInterval || clearInterval)(handle as any);\n },\n delegate: undefined,\n};\n", "import { Action } from './Action';\nimport { SchedulerAction } from '../types';\nimport { Subscription } from '../Subscription';\nimport { AsyncScheduler } from './AsyncScheduler';\nimport { intervalProvider } from './intervalProvider';\nimport { arrRemove } from '../util/arrRemove';\nimport { TimerHandle } from './timerHandle';\n\nexport class AsyncAction extends Action {\n public id: TimerHandle | undefined;\n public state?: T;\n // @ts-ignore: Property has no initializer and is not definitely assigned\n public delay: number;\n protected pending: boolean = false;\n\n constructor(protected scheduler: AsyncScheduler, protected work: (this: SchedulerAction, state?: T) => void) {\n super(scheduler, work);\n }\n\n public schedule(state?: T, delay: number = 0): Subscription {\n if (this.closed) {\n return this;\n }\n\n // Always replace the current state with the new state.\n this.state = state;\n\n const id = this.id;\n const scheduler = this.scheduler;\n\n //\n // Important implementation note:\n //\n // Actions only execute once by default, unless rescheduled from within the\n // scheduled callback. This allows us to implement single and repeat\n // actions via the same code path, without adding API surface area, as well\n // as mimic traditional recursion but across asynchronous boundaries.\n //\n // However, JS runtimes and timers distinguish between intervals achieved by\n // serial `setTimeout` calls vs. a single `setInterval` call. An interval of\n // serial `setTimeout` calls can be individually delayed, which delays\n // scheduling the next `setTimeout`, and so on. `setInterval` attempts to\n // guarantee the interval callback will be invoked more precisely to the\n // interval period, regardless of load.\n //\n // Therefore, we use `setInterval` to schedule single and repeat actions.\n // If the action reschedules itself with the same delay, the interval is not\n // canceled. If the action doesn't reschedule, or reschedules with a\n // different delay, the interval will be canceled after scheduled callback\n // execution.\n //\n if (id != null) {\n this.id = this.recycleAsyncId(scheduler, id, delay);\n }\n\n // Set the pending flag indicating that this action has been scheduled, or\n // has recursively rescheduled itself.\n this.pending = true;\n\n this.delay = delay;\n // If this action has already an async Id, don't request a new one.\n this.id = this.id ?? this.requestAsyncId(scheduler, this.id, delay);\n\n return this;\n }\n\n protected requestAsyncId(scheduler: AsyncScheduler, _id?: TimerHandle, delay: number = 0): TimerHandle {\n return intervalProvider.setInterval(scheduler.flush.bind(scheduler, this), delay);\n }\n\n protected recycleAsyncId(_scheduler: AsyncScheduler, id?: TimerHandle, delay: number | null = 0): TimerHandle | undefined {\n // If this action is rescheduled with the same delay time, don't clear the interval id.\n if (delay != null && this.delay === delay && this.pending === false) {\n return id;\n }\n // Otherwise, if the action's delay time is different from the current delay,\n // or the action has been rescheduled before it's executed, clear the interval id\n if (id != null) {\n intervalProvider.clearInterval(id);\n }\n\n return undefined;\n }\n\n /**\n * Immediately executes this action and the `work` it contains.\n * @return {any}\n */\n public execute(state: T, delay: number): any {\n if (this.closed) {\n return new Error('executing a cancelled action');\n }\n\n this.pending = false;\n const error = this._execute(state, delay);\n if (error) {\n return error;\n } else if (this.pending === false && this.id != null) {\n // Dequeue if the action didn't reschedule itself. Don't call\n // unsubscribe(), because the action could reschedule later.\n // For example:\n // ```\n // scheduler.schedule(function doWork(counter) {\n // /* ... I'm a busy worker bee ... */\n // var originalAction = this;\n // /* wait 100ms before rescheduling the action */\n // setTimeout(function () {\n // originalAction.schedule(counter + 1);\n // }, 100);\n // }, 1000);\n // ```\n this.id = this.recycleAsyncId(this.scheduler, this.id, null);\n }\n }\n\n protected _execute(state: T, _delay: number): any {\n let errored: boolean = false;\n let errorValue: any;\n try {\n this.work(state);\n } catch (e) {\n errored = true;\n // HACK: Since code elsewhere is relying on the \"truthiness\" of the\n // return here, we can't have it return \"\" or 0 or false.\n // TODO: Clean this up when we refactor schedulers mid-version-8 or so.\n errorValue = e ? e : new Error('Scheduled action threw falsy error');\n }\n if (errored) {\n this.unsubscribe();\n return errorValue;\n }\n }\n\n unsubscribe() {\n if (!this.closed) {\n const { id, scheduler } = this;\n const { actions } = scheduler;\n\n this.work = this.state = this.scheduler = null!;\n this.pending = false;\n\n arrRemove(actions, this);\n if (id != null) {\n this.id = this.recycleAsyncId(scheduler, id, null);\n }\n\n this.delay = null!;\n super.unsubscribe();\n }\n }\n}\n", "import { Action } from './scheduler/Action';\nimport { Subscription } from './Subscription';\nimport { SchedulerLike, SchedulerAction } from './types';\nimport { dateTimestampProvider } from './scheduler/dateTimestampProvider';\n\n/**\n * An execution context and a data structure to order tasks and schedule their\n * execution. Provides a notion of (potentially virtual) time, through the\n * `now()` getter method.\n *\n * Each unit of work in a Scheduler is called an `Action`.\n *\n * ```ts\n * class Scheduler {\n * now(): number;\n * schedule(work, delay?, state?): Subscription;\n * }\n * ```\n *\n * @class Scheduler\n * @deprecated Scheduler is an internal implementation detail of RxJS, and\n * should not be used directly. Rather, create your own class and implement\n * {@link SchedulerLike}. Will be made internal in v8.\n */\nexport class Scheduler implements SchedulerLike {\n public static now: () => number = dateTimestampProvider.now;\n\n constructor(private schedulerActionCtor: typeof Action, now: () => number = Scheduler.now) {\n this.now = now;\n }\n\n /**\n * A getter method that returns a number representing the current time\n * (at the time this function was called) according to the scheduler's own\n * internal clock.\n * @return {number} A number that represents the current time. May or may not\n * have a relation to wall-clock time. May or may not refer to a time unit\n * (e.g. milliseconds).\n */\n public now: () => number;\n\n /**\n * Schedules a function, `work`, for execution. May happen at some point in\n * the future, according to the `delay` parameter, if specified. May be passed\n * some context object, `state`, which will be passed to the `work` function.\n *\n * The given arguments will be processed an stored as an Action object in a\n * queue of actions.\n *\n * @param {function(state: ?T): ?Subscription} work A function representing a\n * task, or some unit of work to be executed by the Scheduler.\n * @param {number} [delay] Time to wait before executing the work, where the\n * time unit is implicit and defined by the Scheduler itself.\n * @param {T} [state] Some contextual data that the `work` function uses when\n * called by the Scheduler.\n * @return {Subscription} A subscription in order to be able to unsubscribe\n * the scheduled work.\n */\n public schedule(work: (this: SchedulerAction, state?: T) => void, delay: number = 0, state?: T): Subscription {\n return new this.schedulerActionCtor(this, work).schedule(state, delay);\n }\n}\n", "import { Scheduler } from '../Scheduler';\nimport { Action } from './Action';\nimport { AsyncAction } from './AsyncAction';\nimport { TimerHandle } from './timerHandle';\n\nexport class AsyncScheduler extends Scheduler {\n public actions: Array> = [];\n /**\n * A flag to indicate whether the Scheduler is currently executing a batch of\n * queued actions.\n * @type {boolean}\n * @internal\n */\n public _active: boolean = false;\n /**\n * An internal ID used to track the latest asynchronous task such as those\n * coming from `setTimeout`, `setInterval`, `requestAnimationFrame`, and\n * others.\n * @type {any}\n * @internal\n */\n public _scheduled: TimerHandle | undefined;\n\n constructor(SchedulerAction: typeof Action, now: () => number = Scheduler.now) {\n super(SchedulerAction, now);\n }\n\n public flush(action: AsyncAction): void {\n const { actions } = this;\n\n if (this._active) {\n actions.push(action);\n return;\n }\n\n let error: any;\n this._active = true;\n\n do {\n if ((error = action.execute(action.state, action.delay))) {\n break;\n }\n } while ((action = actions.shift()!)); // exhaust the scheduler queue\n\n this._active = false;\n\n if (error) {\n while ((action = actions.shift()!)) {\n action.unsubscribe();\n }\n throw error;\n }\n }\n}\n", "import { AsyncAction } from './AsyncAction';\nimport { AsyncScheduler } from './AsyncScheduler';\n\n/**\n *\n * Async Scheduler\n *\n * Schedule task as if you used setTimeout(task, duration)\n *\n * `async` scheduler schedules tasks asynchronously, by putting them on the JavaScript\n * event loop queue. It is best used to delay tasks in time or to schedule tasks repeating\n * in intervals.\n *\n * If you just want to \"defer\" task, that is to perform it right after currently\n * executing synchronous code ends (commonly achieved by `setTimeout(deferredTask, 0)`),\n * better choice will be the {@link asapScheduler} scheduler.\n *\n * ## Examples\n * Use async scheduler to delay task\n * ```ts\n * import { asyncScheduler } from 'rxjs';\n *\n * const task = () => console.log('it works!');\n *\n * asyncScheduler.schedule(task, 2000);\n *\n * // After 2 seconds logs:\n * // \"it works!\"\n * ```\n *\n * Use async scheduler to repeat task in intervals\n * ```ts\n * import { asyncScheduler } from 'rxjs';\n *\n * function task(state) {\n * console.log(state);\n * this.schedule(state + 1, 1000); // `this` references currently executing Action,\n * // which we reschedule with new state and delay\n * }\n *\n * asyncScheduler.schedule(task, 3000, 0);\n *\n * // Logs:\n * // 0 after 3s\n * // 1 after 4s\n * // 2 after 5s\n * // 3 after 6s\n * ```\n */\n\nexport const asyncScheduler = new AsyncScheduler(AsyncAction);\n\n/**\n * @deprecated Renamed to {@link asyncScheduler}. Will be removed in v8.\n */\nexport const async = asyncScheduler;\n", "import { AsyncAction } from './AsyncAction';\nimport { Subscription } from '../Subscription';\nimport { QueueScheduler } from './QueueScheduler';\nimport { SchedulerAction } from '../types';\nimport { TimerHandle } from './timerHandle';\n\nexport class QueueAction extends AsyncAction {\n constructor(protected scheduler: QueueScheduler, protected work: (this: SchedulerAction, state?: T) => void) {\n super(scheduler, work);\n }\n\n public schedule(state?: T, delay: number = 0): Subscription {\n if (delay > 0) {\n return super.schedule(state, delay);\n }\n this.delay = delay;\n this.state = state;\n this.scheduler.flush(this);\n return this;\n }\n\n public execute(state: T, delay: number): any {\n return delay > 0 || this.closed ? super.execute(state, delay) : this._execute(state, delay);\n }\n\n protected requestAsyncId(scheduler: QueueScheduler, id?: TimerHandle, delay: number = 0): TimerHandle {\n // If delay exists and is greater than 0, or if the delay is null (the\n // action wasn't rescheduled) but was originally scheduled as an async\n // action, then recycle as an async action.\n\n if ((delay != null && delay > 0) || (delay == null && this.delay > 0)) {\n return super.requestAsyncId(scheduler, id, delay);\n }\n\n // Otherwise flush the scheduler starting with this action.\n scheduler.flush(this);\n\n // HACK: In the past, this was returning `void`. However, `void` isn't a valid\n // `TimerHandle`, and generally the return value here isn't really used. So the\n // compromise is to return `0` which is both \"falsy\" and a valid `TimerHandle`,\n // as opposed to refactoring every other instanceo of `requestAsyncId`.\n return 0;\n }\n}\n", "import { AsyncScheduler } from './AsyncScheduler';\n\nexport class QueueScheduler extends AsyncScheduler {\n}\n", "import { QueueAction } from './QueueAction';\nimport { QueueScheduler } from './QueueScheduler';\n\n/**\n *\n * Queue Scheduler\n *\n * Put every next task on a queue, instead of executing it immediately\n *\n * `queue` scheduler, when used with delay, behaves the same as {@link asyncScheduler} scheduler.\n *\n * When used without delay, it schedules given task synchronously - executes it right when\n * it is scheduled. However when called recursively, that is when inside the scheduled task,\n * another task is scheduled with queue scheduler, instead of executing immediately as well,\n * that task will be put on a queue and wait for current one to finish.\n *\n * This means that when you execute task with `queue` scheduler, you are sure it will end\n * before any other task scheduled with that scheduler will start.\n *\n * ## Examples\n * Schedule recursively first, then do something\n * ```ts\n * import { queueScheduler } from 'rxjs';\n *\n * queueScheduler.schedule(() => {\n * queueScheduler.schedule(() => console.log('second')); // will not happen now, but will be put on a queue\n *\n * console.log('first');\n * });\n *\n * // Logs:\n * // \"first\"\n * // \"second\"\n * ```\n *\n * Reschedule itself recursively\n * ```ts\n * import { queueScheduler } from 'rxjs';\n *\n * queueScheduler.schedule(function(state) {\n * if (state !== 0) {\n * console.log('before', state);\n * this.schedule(state - 1); // `this` references currently executing Action,\n * // which we reschedule with new state\n * console.log('after', state);\n * }\n * }, 0, 3);\n *\n * // In scheduler that runs recursively, you would expect:\n * // \"before\", 3\n * // \"before\", 2\n * // \"before\", 1\n * // \"after\", 1\n * // \"after\", 2\n * // \"after\", 3\n *\n * // But with queue it logs:\n * // \"before\", 3\n * // \"after\", 3\n * // \"before\", 2\n * // \"after\", 2\n * // \"before\", 1\n * // \"after\", 1\n * ```\n */\n\nexport const queueScheduler = new QueueScheduler(QueueAction);\n\n/**\n * @deprecated Renamed to {@link queueScheduler}. Will be removed in v8.\n */\nexport const queue = queueScheduler;\n", "import { AsyncAction } from './AsyncAction';\nimport { AnimationFrameScheduler } from './AnimationFrameScheduler';\nimport { SchedulerAction } from '../types';\nimport { animationFrameProvider } from './animationFrameProvider';\nimport { TimerHandle } from './timerHandle';\n\nexport class AnimationFrameAction extends AsyncAction {\n constructor(protected scheduler: AnimationFrameScheduler, protected work: (this: SchedulerAction, state?: T) => void) {\n super(scheduler, work);\n }\n\n protected requestAsyncId(scheduler: AnimationFrameScheduler, id?: TimerHandle, delay: number = 0): TimerHandle {\n // If delay is greater than 0, request as an async action.\n if (delay !== null && delay > 0) {\n return super.requestAsyncId(scheduler, id, delay);\n }\n // Push the action to the end of the scheduler queue.\n scheduler.actions.push(this);\n // If an animation frame has already been requested, don't request another\n // one. If an animation frame hasn't been requested yet, request one. Return\n // the current animation frame request id.\n return scheduler._scheduled || (scheduler._scheduled = animationFrameProvider.requestAnimationFrame(() => scheduler.flush(undefined)));\n }\n\n protected recycleAsyncId(scheduler: AnimationFrameScheduler, id?: TimerHandle, delay: number = 0): TimerHandle | undefined {\n // If delay exists and is greater than 0, or if the delay is null (the\n // action wasn't rescheduled) but was originally scheduled as an async\n // action, then recycle as an async action.\n if (delay != null ? delay > 0 : this.delay > 0) {\n return super.recycleAsyncId(scheduler, id, delay);\n }\n // If the scheduler queue has no remaining actions with the same async id,\n // cancel the requested animation frame and set the scheduled flag to\n // undefined so the next AnimationFrameAction will request its own.\n const { actions } = scheduler;\n if (id != null && actions[actions.length - 1]?.id !== id) {\n animationFrameProvider.cancelAnimationFrame(id as number);\n scheduler._scheduled = undefined;\n }\n // Return undefined so the action knows to request a new async id if it's rescheduled.\n return undefined;\n }\n}\n", "import { AsyncAction } from './AsyncAction';\nimport { AsyncScheduler } from './AsyncScheduler';\n\nexport class AnimationFrameScheduler extends AsyncScheduler {\n public flush(action?: AsyncAction): void {\n this._active = true;\n // The async id that effects a call to flush is stored in _scheduled.\n // Before executing an action, it's necessary to check the action's async\n // id to determine whether it's supposed to be executed in the current\n // flush.\n // Previous implementations of this method used a count to determine this,\n // but that was unsound, as actions that are unsubscribed - i.e. cancelled -\n // are removed from the actions array and that can shift actions that are\n // scheduled to be executed in a subsequent flush into positions at which\n // they are executed within the current flush.\n const flushId = this._scheduled;\n this._scheduled = undefined;\n\n const { actions } = this;\n let error: any;\n action = action || actions.shift()!;\n\n do {\n if ((error = action.execute(action.state, action.delay))) {\n break;\n }\n } while ((action = actions[0]) && action.id === flushId && actions.shift());\n\n this._active = false;\n\n if (error) {\n while ((action = actions[0]) && action.id === flushId && actions.shift()) {\n action.unsubscribe();\n }\n throw error;\n }\n }\n}\n", "import { AnimationFrameAction } from './AnimationFrameAction';\nimport { AnimationFrameScheduler } from './AnimationFrameScheduler';\n\n/**\n *\n * Animation Frame Scheduler\n *\n * Perform task when `window.requestAnimationFrame` would fire\n *\n * When `animationFrame` scheduler is used with delay, it will fall back to {@link asyncScheduler} scheduler\n * behaviour.\n *\n * Without delay, `animationFrame` scheduler can be used to create smooth browser animations.\n * It makes sure scheduled task will happen just before next browser content repaint,\n * thus performing animations as efficiently as possible.\n *\n * ## Example\n * Schedule div height animation\n * ```ts\n * // html:
\n * import { animationFrameScheduler } from 'rxjs';\n *\n * const div = document.querySelector('div');\n *\n * animationFrameScheduler.schedule(function(height) {\n * div.style.height = height + \"px\";\n *\n * this.schedule(height + 1); // `this` references currently executing Action,\n * // which we reschedule with new state\n * }, 0, 0);\n *\n * // You will see a div element growing in height\n * ```\n */\n\nexport const animationFrameScheduler = new AnimationFrameScheduler(AnimationFrameAction);\n\n/**\n * @deprecated Renamed to {@link animationFrameScheduler}. Will be removed in v8.\n */\nexport const animationFrame = animationFrameScheduler;\n", "import { Observable } from '../Observable';\nimport { SchedulerLike } from '../types';\n\n/**\n * A simple Observable that emits no items to the Observer and immediately\n * emits a complete notification.\n *\n * Just emits 'complete', and nothing else.\n *\n * ![](empty.png)\n *\n * A simple Observable that only emits the complete notification. It can be used\n * for composing with other Observables, such as in a {@link mergeMap}.\n *\n * ## Examples\n *\n * Log complete notification\n *\n * ```ts\n * import { EMPTY } from 'rxjs';\n *\n * EMPTY.subscribe({\n * next: () => console.log('Next'),\n * complete: () => console.log('Complete!')\n * });\n *\n * // Outputs\n * // Complete!\n * ```\n *\n * Emit the number 7, then complete\n *\n * ```ts\n * import { EMPTY, startWith } from 'rxjs';\n *\n * const result = EMPTY.pipe(startWith(7));\n * result.subscribe(x => console.log(x));\n *\n * // Outputs\n * // 7\n * ```\n *\n * Map and flatten only odd numbers to the sequence `'a'`, `'b'`, `'c'`\n *\n * ```ts\n * import { interval, mergeMap, of, EMPTY } from 'rxjs';\n *\n * const interval$ = interval(1000);\n * const result = interval$.pipe(\n * mergeMap(x => x % 2 === 1 ? of('a', 'b', 'c') : EMPTY),\n * );\n * result.subscribe(x => console.log(x));\n *\n * // Results in the following to the console:\n * // x is equal to the count on the interval, e.g. (0, 1, 2, 3, ...)\n * // x will occur every 1000ms\n * // if x % 2 is equal to 1, print a, b, c (each on its own)\n * // if x % 2 is not equal to 1, nothing will be output\n * ```\n *\n * @see {@link Observable}\n * @see {@link NEVER}\n * @see {@link of}\n * @see {@link throwError}\n */\nexport const EMPTY = new Observable((subscriber) => subscriber.complete());\n\n/**\n * @param scheduler A {@link SchedulerLike} to use for scheduling\n * the emission of the complete notification.\n * @deprecated Replaced with the {@link EMPTY} constant or {@link scheduled} (e.g. `scheduled([], scheduler)`). Will be removed in v8.\n */\nexport function empty(scheduler?: SchedulerLike) {\n return scheduler ? emptyScheduled(scheduler) : EMPTY;\n}\n\nfunction emptyScheduled(scheduler: SchedulerLike) {\n return new Observable((subscriber) => scheduler.schedule(() => subscriber.complete()));\n}\n", "import { SchedulerLike } from '../types';\nimport { isFunction } from './isFunction';\n\nexport function isScheduler(value: any): value is SchedulerLike {\n return value && isFunction(value.schedule);\n}\n", "import { SchedulerLike } from '../types';\nimport { isFunction } from './isFunction';\nimport { isScheduler } from './isScheduler';\n\nfunction last(arr: T[]): T | undefined {\n return arr[arr.length - 1];\n}\n\nexport function popResultSelector(args: any[]): ((...args: unknown[]) => unknown) | undefined {\n return isFunction(last(args)) ? args.pop() : undefined;\n}\n\nexport function popScheduler(args: any[]): SchedulerLike | undefined {\n return isScheduler(last(args)) ? args.pop() : undefined;\n}\n\nexport function popNumber(args: any[], defaultValue: number): number {\n return typeof last(args) === 'number' ? args.pop()! : defaultValue;\n}\n", "export const isArrayLike = ((x: any): x is ArrayLike => x && typeof x.length === 'number' && typeof x !== 'function');", "import { isFunction } from \"./isFunction\";\n\n/**\n * Tests to see if the object is \"thennable\".\n * @param value the object to test\n */\nexport function isPromise(value: any): value is PromiseLike {\n return isFunction(value?.then);\n}\n", "import { InteropObservable } from '../types';\nimport { observable as Symbol_observable } from '../symbol/observable';\nimport { isFunction } from './isFunction';\n\n/** Identifies an input as being Observable (but not necessary an Rx Observable) */\nexport function isInteropObservable(input: any): input is InteropObservable {\n return isFunction(input[Symbol_observable]);\n}\n", "import { isFunction } from './isFunction';\n\nexport function isAsyncIterable(obj: any): obj is AsyncIterable {\n return Symbol.asyncIterator && isFunction(obj?.[Symbol.asyncIterator]);\n}\n", "/**\n * Creates the TypeError to throw if an invalid object is passed to `from` or `scheduled`.\n * @param input The object that was passed.\n */\nexport function createInvalidObservableTypeError(input: any) {\n // TODO: We should create error codes that can be looked up, so this can be less verbose.\n return new TypeError(\n `You provided ${\n input !== null && typeof input === 'object' ? 'an invalid object' : `'${input}'`\n } where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.`\n );\n}\n", "export function getSymbolIterator(): symbol {\n if (typeof Symbol !== 'function' || !Symbol.iterator) {\n return '@@iterator' as any;\n }\n\n return Symbol.iterator;\n}\n\nexport const iterator = getSymbolIterator();\n", "import { iterator as Symbol_iterator } from '../symbol/iterator';\nimport { isFunction } from './isFunction';\n\n/** Identifies an input as being an Iterable */\nexport function isIterable(input: any): input is Iterable {\n return isFunction(input?.[Symbol_iterator]);\n}\n", "import { ReadableStreamLike } from '../types';\nimport { isFunction } from './isFunction';\n\nexport async function* readableStreamLikeToAsyncGenerator(readableStream: ReadableStreamLike): AsyncGenerator {\n const reader = readableStream.getReader();\n try {\n while (true) {\n const { value, done } = await reader.read();\n if (done) {\n return;\n }\n yield value!;\n }\n } finally {\n reader.releaseLock();\n }\n}\n\nexport function isReadableStreamLike(obj: any): obj is ReadableStreamLike {\n // We don't want to use instanceof checks because they would return\n // false for instances from another Realm, like an

+ +

⚙️ TECH STACK

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CategoryTechnologies
LanguagesPython, JavaScript
Libraries/FrameworksTensorFlow, Keras, Flask
DatabasesMongoDB, PostgreSQL
ToolsDocker, Git, Jupyter, VS Code
DeploymentAWS, Heroku
+
+

📝 DESCRIPTION

+ + + + + + +
+

What is the requirement of the project?

+
    +
  • Write the answer here in simple bullet points.
  • +
+
+ +
+How is it beneficial and used? +
    +
  • Write the answer here in simple bullet points.
  • +
+
+ +
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Write the answer here in simple bullet points.
  • +
+
+ +
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • Write the answer here in simple bullet points.
  • +
+
+
+

🔍 EXPLANATION

+

🧩 DETAILS OF THE DIFFERENT FEATURES

+ + + + +

📂 dataset.csv

+ + + + + + + + + + + + + +
Feature NameDescription
feature 1explain 1
+ +

🛠 Developed Features

+ + + + + + + + + + + + + + + +
Feature NameDescriptionReason
feature 1explain 1reason 1
+
+

🛤 PROJECT WORKFLOW

+ + +
+

Project workflow

+
  graph LR
+    A[Start] --> B{Error?};
+    B -->|Yes| C[Hmm...];
+    C --> D[Debug];
+    D --> B;
+    B ---->|No| E[Yay!];
+
+ +
+
+
+
    +
  • Explanation
  • +
+
+
+
    +
  • Explanation
  • +
+
+
+
    +
  • Explanation
  • +
+
+
+
    +
  • Explanation
  • +
+
+
+
    +
  • Explanation
  • +
+
+
+
    +
  • Explanation
  • +
+
+
+
+
+

🖥 CODE EXPLANATION

+ + + +
+
+
+
    +
  • Explanation
  • +
+
+
+
+
+

⚖️ PROJECT TRADE-OFFS AND SOLUTIONS

+ + +
+
+
+
    +
  • Describe the trade-off encountered (e.g., accuracy vs. computational efficiency).
  • +
  • Explain how you addressed this trade-off (e.g., by optimizing hyperparameters, using a more efficient algorithm, etc.).
  • +
+
+
+
    +
  • Describe another trade-off (e.g., model complexity vs. interpretability).
  • +
  • Explain the solution (e.g., by selecting a model that balances both aspects effectively).
  • +
+
+
+
+
+

🖼 SCREENSHOTS

+ + +
+Visualizations and EDA of different features +
+
+
+

img

+
+
+
+
+
+Model performance graphs +
+
+
+

img

+
+
+
+
+
+

📉 MODELS USED AND THEIR EVALUATION METRICS

+ + + + + + + + + + + + + + + + + + + + + + + + + +
ModelAccuracyMSER2 Score
Model Name95%0.0220.90
Model Name93%0.0330.88
+
+

✅ CONCLUSION

+

🔑 KEY LEARNINGS

+ + +
+

Insights gained from the data

+
    +
  • Write from here in bullet points
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Write from here in bullet points
  • +
+
+
+

🌍 USE CASES

+ + +
+
+
+
    +
  • Explain your application
  • +
+
+
+
    +
  • Explain your application
  • +
+
+
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/artificial-intelligence/index.html b/projects/artificial-intelligence/index.html new file mode 100644 index 00000000..d02facb9 --- /dev/null +++ b/projects/artificial-intelligence/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Artificial Intelligence 💡 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Artificial Intelligence 💡

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/computer-vision/black_and_white_image_colorizer/index.html b/projects/computer-vision/black_and_white_image_colorizer/index.html new file mode 100644 index 00000000..44c93279 --- /dev/null +++ b/projects/computer-vision/black_and_white_image_colorizer/index.html @@ -0,0 +1,801 @@ + + + + + + + + + + + + + + + + + + + + + Black and White Image Colorizer - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Black and White Image Colorizer

+

AIM

+

Colorization of Black and White Images using OpenCV and pre-trained caffe models.

+

PRE-TRAINED MODELS

+

colorization_deploy_v2.prototxt - +colorization_release_v2.caffemodel - +pts_in_hull.npy

+ +

Colab Notebook

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • numpy
  • +
  • cv2
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • The project aims to perform colorization of black and white images.
  • +
  • It involves in showcase the capabilities of OpenCV's DNN module and caffe models.
  • +
  • It is done by processing given image using openCV and use Lab Color space model to hallucinate an approximation of how colorized version of the image "might look like".
  • +
+
+
+Why is it necessary? +
    +
  • It helps preserving historical black-and-white photos.
  • +
  • It can be used adding color to grayscale images for creative industries.
  • +
  • It acts an advancing computer vision applications in artistic and research fields.
  • +
+
+
+How is it beneficial and used? +
    +
  • Personal use : It helps in restoring old family photographs.
  • +
  • Cultural and Political : it also enhances grayscale photographs of important historic events for modern displays.
  • +
  • Creativity and Art : it improves AI-based creative tools for artists and designers.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Initial approach : reading various research papers and analyze different approaches on how to deal with this project.
  • +
  • Identified Richzhang research paper on the title : Colorful Image colorization.
  • +
  • Did some research on pre-trained models for image colorization.
  • +
  • Understood OpenCV's DNN module and its implementation.
  • +
  • Experimented with sample images to test model outputs.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

EXPLANATION

+

WHAT I HAVE DONE

+
+
+
+

Initial data exploration and understanding:

+
    +
  • Load the grayscale input image.
  • +
  • Load pre-trained caffe models using openCV dnn module.
  • +
+
+
+

Data cleaning and preprocessing:

+
    +
  • Preprocess image to normalize and convert to LAB color space.
  • +
  • Resize image for the network.
  • +
  • Split L channel and perform mean subtraction.
  • +
  • Predict ab channel from the input of L channel.
  • +
+
+
+

Feature engineering and selection:

+
    +
  • Resize predicted ab channel's volume to same dimension as our image.
  • +
  • Join L and predicted ab channel.
  • +
  • Convert image from Lab back to RGB.
  • +
+
+
+

Result :

+
    +
  • Resize and Show the Original and Colorized image.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Computational efficiency vs. color accuracy.
  • +
  • Solution : Used optimized preprocessing pipelines to reduce runtime.
  • +
+
+
+
    +
  • Pre-trained model generalization vs. custom training.
  • +
  • Solution : Choose the pre-trained model for faster implementation and reliable results.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project structure or tree diagram

+
  graph LR  
+    A[Load Grayscale Image] --> B[Preprocess Image];  
+    B --> C[Load Pre-trained Model];  
+    C --> D[Predict A and B Channels];  
+    D --> E[Combine with L Channel];  
+    E --> F[Convert to RGB];  
+    F --> G[Display/Save Colorized Image];
+
+
+Visualizations of results +
+
+
+

Original Image

+
+
+

Colorized Image

+
+
+

result

+
+
+
+
+
+

CONCLUSION

+

KEY LEARNINGS

+
+

Insights gained from the data

+
    +
  • Color Space : LAB color space facilitates colorization tasks.
  • +
  • Pre-trained Models : Pre-trained models can generalize across various grayscale images.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • OpenCV : Enhanced knowledge of OpenCV's DNN module.
  • +
  • Caffe Models : Usage of pre-trained models.
  • +
  • Image Dimensionality : Understanding how Image can be manipulated.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Color Space Conversion : Initial difficulties with LAB to RGB conversion; resolved using OpenCV documentation.
  • +
+
+
+

USE CASES

+
+
+
+

Image Restoration

+
    +
  • Restoring old family photographs to vivid colors.
  • +
+
+
+

Creative Industries

+
    +
  • Colorizing artistic grayscale sketches for concept designs.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/computer-vision/brightness-control/index.html b/projects/computer-vision/brightness-control/index.html new file mode 100644 index 00000000..c055385a --- /dev/null +++ b/projects/computer-vision/brightness-control/index.html @@ -0,0 +1,919 @@ + + + + + + + + + + + + + + + + + + + + + 📜 Brightness control - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

📜 Brightness control

+
+ +
+ +

🎯 AIM

+

To develop a real-time brightness control system using hand gestures, leveraging OpenCV and MediaPipe for hand detection and brightness adjustment.

+ +

No dataset used

+ + +

https://drive.google.com/file/d/1q7kraajGykfc2Kb6-84dCOjkrDGhIQcy/view?usp=sharing

+

⚙️ TECH STACK

+ + + + + + + + + + + + + + + + + + + + + +
CategoryTechnologies
LanguagesPython
Libraries/FrameworksOpenCV, NumPy, MediaPipe, cvzone
ToolsJupyter Notebook, Local Python IDE
+
+

📝 DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • The project requires a webcam to capture real-time video and detect hand gestures for brightness control.
  • +
+
+
+How is it beneficial and used? +
    +
  • Allows users to control screen brightness without physical touch, making it useful for touchless interfaces.
  • +
  • Ideal for applications in smart home systems and assistive technologies.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Identified the need for a touchless brightness control system.
  • +
  • Selected OpenCV for video processing and MediaPipe for efficient hand tracking.
  • +
  • Developed a prototype to calculate brightness based on hand distance.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • OpenCV documentation for video processing.
  • +
  • MediaPipe's official guide for hand tracking.
  • +
+
+
+

🔍 EXPLANATION

+

🧩 DETAILS OF THE DIFFERENT FEATURES

+

🛠 Developed Features

+ + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescriptionReason
Hand DetectionDetects hand gestures in real-timeTo control brightness with gestures
Distance CalculationCalculates distance between fingersTo adjust brightness dynamically
Brightness MappingMaps hand distance to brightness levelsEnsures smooth adjustment of brightness
+
+

🛤 PROJECT WORKFLOW

+
+

Project workflow

+
  graph LR
+A[Start] --> B[Initialize Webcam];
+B --> C[Detect Hand Gestures];
+C --> D[Calculate Distance];
+D --> E[Adjust Brightness];
+E --> F[Display Output];
+
+
+
+
+
+
+
    +
  • Initialize the webcam using OpenCV.
  • +
+
+
+
+
+
+
    +
  • Use MediaPipe to detect hands in the video feed.
  • +
+
+
+
+
+
+
    +
  • Calculate the distance between two fingers (e.g., thumb and index).
  • +
+
+
+
+
+
+
    +
  • Map the distance to a brightness range.
  • +
+
+
+
+
+
+
    +
  • Display the adjusted brightness on the video feed.
  • +
+
+

🖥 CODE EXPLANATION

+
+
+
+
+
+
    +
  • +

    The program begins by setting up the webcam to capture frames with a resolution of 640x480 pixels. This ensures consistent processing and visualization of the video stream.

    +
    cap = cv2.VideoCapture(0)  
    +cap.set(3, 640)  # Set width  
    +cap.set(4, 480)  # Set height 
    +
    +
  • +
+
+
+
+
+
+
    +
  • +

    Using the HandDetector from cvzone, the program tracks one hand (maxHands=1). The brightness of the video frame is dynamically adjusted based on the distance between the thumb and index finger.

    +
    detector = HandDetector(detectionCon=0.8, maxHands=1)  
    +brightness = 1.0  # Default brightness level  
    +
    +
  • +
  • +

    The HandDetector detects hand landmarks in each frame with a confidence threshold of 0.8. The initial brightness is set to 1.0 (normal).

    +
    hands, img = detector.findHands(frame, flipType=False)  
    +
    +if hands:  
    +    hand = hands[0]  
    +    lm_list = hand['lmList']  
    +    if len(lm_list) > 8:  
    +        thumb_tip = lm_list[4]  
    +        index_tip = lm_list[8]  
    +        distance = int(((thumb_tip[0] - index_tip[0]) ** 2 + (thumb_tip[1] - index_tip[1]) ** 2) ** 0.5)  
    +        brightness = np.interp(distance, [20, 200], [0, 1])  
    +
    +
  • +
  • +

    The program calculates the distance between the thumb tip (lmList[4]) and index finger tip (lmList[8]). This distance is mapped to a brightness range of 0 to 1 using np.interp.

    +
  • +
+
+
+
+
+
+
    +
  • +

    The captured frame's brightness is modified by scaling the value (V) channel in the HSV color space according to the calculated brightness level.

    +
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  
    +hsv[..., 2] = np.clip(hsv[..., 2] * brightness, 0, 255).astype(np.uint8)  
    +frame_bright = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)  
    +cv2.imshow("Brightness Controller", frame_bright)  
    +
    +
  • +
  • +

    This technique ensures smooth, real-time brightness adjustments based on the user's hand gestures. The output frame is displayed with the adjusted brightness level.

    +
  • +
+
+

⚖️ PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Real-time processing vs. computational efficiency: Optimized hand detection by limiting the maximum number of detectable hands to 1.
  • +
+
+
+
    +
  • Precision in brightness control vs. usability: Adjusted mapping function to ensure smooth transitions.
  • +
+
+
+
+
+

🖼 SCREENSHOTS

+
+Working of the model +
+
+
+
+
+

image

+
+
+

✅ CONCLUSION

+

🔑 KEY LEARNINGS

+
+

Insights gained from the data

+
+
    +
  • Improved understanding of real-time video processing.
  • +
  • Learned to integrate gesture detection with hardware functionalities.
  • +
+
+Improvements in understanding machine learning concepts +
+
    +
  • Gained insights into MediaPipe's efficient hand detection algorithms.
  • +
+
+

🌍 USE CASES

+
+
+
+
+
+
    +
  • Touchless brightness control for smart home displays.
  • +
+
+
+
+
+
+
    +
  • Brightness adjustment for users with limited mobility.
  • +
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/computer-vision/counting-bicep-reps/index.html b/projects/computer-vision/counting-bicep-reps/index.html new file mode 100644 index 00000000..46feac6f --- /dev/null +++ b/projects/computer-vision/counting-bicep-reps/index.html @@ -0,0 +1,832 @@ + + + + + + + + + + + + + + + + + + + + + Counting Bicep Reps - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Counting Bicep Reps

+

AIM

+

To track and count bicep curls in real time using computer vision techniques with OpenCV and Mediapipe's Pose module.

+ +

This project does not use a specific dataset as it works with real-time video from a webcam.

+ +

https://drive.google.com/file/d/13Omm8Zy0lmtjmdHgfQbraBu3NJf3wknw/view?usp=sharing

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • OpenCV
  • +
  • Mediapipe
  • +
  • NumPy
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • The project aims to provide a computer vision-based solution for tracking fitness exercises like bicep curls without the need for wearable devices or sensors.
  • +
+
+
+Why is it necessary? +
    +
  • Helps fitness enthusiasts monitor their workouts in real time.
  • +
  • Provides an affordable and accessible alternative to wearable fitness trackers.
  • +
+
+
+How is it beneficial and used? +
    +
  • Real-time feedback on workout form and repetition count.
  • +
  • Can be extended to other fitness exercises and integrated into fitness apps
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Explored Mediapipe's Pose module for pose landmark detection.
  • +
  • Integrated OpenCV for video frame processing and real-time feedback.
  • +
  • Planned the logic for detecting curls based on elbow angle thresholds.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • Mediapipe official documentation.
  • +
  • OpenCV tutorials on video processing.
  • +
+
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+
- Pose Estimation: Utilized Mediapipe's Pose module to detect key landmarks on the human body.
+- Angle Calculation: Calculated angles at the elbow joints to determine curl movement.
+- Rep Tracking: Incremented rep count when alternating between full curl and relaxed positions.
+- Real-Time Feedback: Displayed the remaining curl count on the video feed.
+
+
+

PROJECT WORKFLOW

+
+
+
+

Initial setup:

+
+
+
+
    +
  • Installed OpenCV and Mediapipe.
  • +
  • Set up a webcam feed for video capture.
  • +
+
+
+
+

Pose detection:

+
+
+
+
    +
  • Used Mediapipe's Pose module to identify body landmarks.
  • +
+
+
+
+

Angle calculation:

+
+
+
+
    +
  • Implemented a function to calculate the angle between shoulder, elbow, and wrist.
  • +
+
+
+
+

Rep detection:

+
+
+
+
    +
  • Monitored elbow angles to track upward and downward movements.
  • +
+
+
+
+

Real-time feedback:

+
+
+
+
    +
  • Displayed the remaining number of curls on the video feed using OpenCV.
  • +
+
+
+
+

Completion:

+
+
+
+
    +
  • Stopped the program when the target reps were completed or on manual exit.
  • +
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Accuracy vs. Simplicity:
      +
    • Using elbow angles alone may not handle all body postures but ensures simplicity.
    • +
    • Solution: Fine-tuned angle thresholds and added tracking for alternating arms.
    • +
    +
  • +
+
+
+
    +
  • Real-Time Performance vs. Model Complexity:
      +
    • Mediapipe's lightweight solution ensured smooth processing over heavier models.
    • +
    +
  • +
+
+
+
+
+

SCREENSHOTS

+
    +
  1. +

    Entering no of reps you want to perform + Screenshot 2025-01-19 184454

    +
  2. +
  3. +

    Performing reps + Screenshot 2025-01-19 184607

    +
  4. +
+
+

Project workflow

+
graph LR  
+A[Webcam Feed] --> F[Enter No of Biceps Reps]
+F --> B[Mediapipe Pose Detection]  
+B --> C[Elbow Angle Calculation]  
+C --> D[Rep Count Decrement]  
+D --> E[Real-Time Update on Frsame]  
+
+
+

CONCLUSION

+

KEY LEARNINGS

+
+

Insights gained from the data

+
    +
  • Real-time video processing using OpenCV.
  • +
  • Pose detection and landmark analysis with Mediapipe.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Understanding geometric computations in pose analysis.
  • +
  • Effective use of pre-trained models like Mediapipe Pose.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Challenge: Handling incorrect postures.
      +
    • Solution: Fine-tuning angle thresholds.
    • +
    +
  • +
+
+
+

USE CASES

+
+
+
+

Personal Fitness Tracker
+- Helps users track their workouts without additional equipment.

+
+
+

Fitness App Integration
+- Can be integrated into fitness apps for real-time exercise tracking.

+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/computer-vision/face-detection/index.html b/projects/computer-vision/face-detection/index.html new file mode 100644 index 00000000..e774a81a --- /dev/null +++ b/projects/computer-vision/face-detection/index.html @@ -0,0 +1,816 @@ + + + + + + + + + + + + + + + + + + + + + Face Detection - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Face Detection

+

AIM

+

The goal of this project is to build a face detection system using OpenCV, which identifies faces in static images using Haar Cascades.

+ +

For this project we are going to use the pretrained Haar Cascade XML file for face detection from OpenCV's Github repository.

+

https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml

+ +

https://colab.research.google.com/drive/1upcl9sa5cL5fUuVLBG5IVuU0xPYs3Nwf#scrollTo=94ggAdg5AnUk

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • OpenCV
  • +
  • Random
  • +
  • Matplotlib
  • +
+
+
+

DESCRIPTION

+

This project involves building a face detection model using OpenCV's pre-trained Haar Cascade Classifiers to detect faces in images.

+
+

What is the requirement of the project?

+
    +
  • A face detection system is needed for various applications such as security, attendance tracking, and facial recognition systems.
  • +
  • This project demonstrates a basic use of computer vision techniques for detecting faces in static images.
  • +
+
+
+Why is it necessary? +
    +
  • Face detection is the first crucial step in many computer vision applications such as face recognition and emotion analysis.
  • +
  • It is an essential component in systems that require human identification or verification.
  • +
+
+
+How is it beneficial and used? +
    +
  • Face detection can be used in automation systems, for example, in attendance tracking, photo tagging, and security surveillance.
  • +
  • It enables various applications in user experience enhancement and biometric systems.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • I began by exploring OpenCV documentation, focusing on how to implement Haar Cascade for face detection.
  • +
  • Initially, I focused on static image detection, planning to extend the project to video-based detection in the future.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • OpenCV documentation
  • +
  • Book: "Learning OpenCV 3" by Adrian Kaehler and Gary Bradski
  • +
+
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+
- Haar Cascade Classifier: A machine learning-based approach for detecting objects in images or video. It works by training on a large set of positive and negative images of faces.
+- Cascade Classifier*: The classifier works through a series of stages, each aimed at increasing detection accuracy.
+- Face Detection: The primary feature of this project is detecting human faces in static images, which is the first step in many facial recognition systems.
+
+
+

PROJECT WORKFLOW

+
+
+
+

Initial data exploration and understanding:

+
+
+
+
    +
  • Research the Haar Cascade method for face detection in OpenCV.
  • +
  • Collect sample images for testing the model's performance.
  • +
+
+
+
+

Data cleaning and preprocessing:

+
+
+
+
    +
  • Ensure all input images are properly formatted (e.g., grayscale images for face detection).
  • +
  • Resize or crop images to ensure optimal processing speed.
  • +
+
+
+
+

Feature engineering and selection: +- Use pre-trained Haar Cascade classifiers for detecting faces. +- Select the appropriate classifier based on face orientation and conditions (e.g., frontal face, profile).

+
+
+

Model training and evaluation: +- Use OpenCV's pre-trained Haar Cascade models. +- Test the detection accuracy on various sample images.

+
+
+

Model optimization and fine-tuning:

+
+
+
+
    +
  • Adjust parameters such as scale factor and minNeighbors to enhance accuracy.
  • +
  • Experiment with different input image sizes to balance speed and accuracy.
  • +
+
+
+
+

Validation and testing:

+
+
+
+
    +
  • Validate the model's effectiveness on different test images, ensuring robust detection.
  • +
  • Evaluate the face detection accuracy based on diverse lighting and image conditions.
  • +
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Accuracy vs. computational efficiency.
      +
    • Solution: Fine-tuned classifier parameters to ensure a balance between accuracy and speed.
    • +
    +
  • +
+
+
+
    +
  • Detection performance vs. image resolution.
      +
    • Solution: Optimized input image resolution and processing flow to ensure both fast processing and accurate detection.
    • +
    +
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+A[Start] --> B{Face Detected?}
+B -->|Yes| C[Mark Face]
+C --> D[Display Result]
+B -->|No| F[Idle/Do Nothing]
+
+
+

CONCLUSION

+

KEY LEARNINGS

+
+

Insights gained from the data

+
    +
  • Gained an understanding of face detection using Haar Cascades.
  • +
  • Improved ability to optimize computer vision models for accuracy and speed.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Learned how to handle trade-offs between accuracy and speed in real-time applications.
  • +
  • Gained hands-on experience with the implementation of object detection algorithms.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Challenge: Low detection accuracy in poor lighting conditions.
  • +
  • Solution: Adjusted classifier parameters and added preprocessing steps to improve accuracy.
  • +
+
+
+

USE CASES

+
+
+
+

Security Surveillance Systems

+
    +
  • Used for identifying individuals or monitoring for intruders in secure areas.
  • +
+
+
+

Attendance Systems

+
    +
  • Used to automate attendance tracking by detecting the faces of students or employees.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/computer-vision/index.html b/projects/computer-vision/index.html new file mode 100644 index 00000000..cee4d1a7 --- /dev/null +++ b/projects/computer-vision/index.html @@ -0,0 +1,520 @@ + + + + + + + + + + + + + + + + + + + + + Computer Vision 🎥 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/computer-vision/music_genre_classification_model/index.html b/projects/computer-vision/music_genre_classification_model/index.html new file mode 100644 index 00000000..a70b4522 --- /dev/null +++ b/projects/computer-vision/music_genre_classification_model/index.html @@ -0,0 +1,1014 @@ + + + + + + + + + + + + + + + + + + + + + Music Genre Classification Model - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Music Genre Classification Model

+

AIM

+

To develop a precise and effective music genre classification model using Convolutional Neural Networks (CNN), Support Vector Machines (SVM), Random Forest and XGBoost Classifier algorithms for the Kaggle GTZAN Dataset Music Genre Classification.

+ +

https://www.kaggle.com/datasets/andradaolteanu/gtzan-dataset-music-genre-classification/data

+ +

https://colab.research.google.com/drive/1j8RZccP2ee5XlWEFSkTyJ98lFyNrezHS?usp=sharing

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • librosa
  • +
  • matplotlib
  • +
  • pandas
  • +
  • sklearn
  • +
  • seaborn
  • +
  • numpy
  • +
  • scipy
  • +
  • xgboost
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • The objective of this research is to develop a precise and effective music genre classification model using Convolutional Neural Networks (CNN), Support Vector Machines (SVM), Random Forest and XGBoost algorithms for the Kaggle GTZAN Dataset Music Genre Classification.
  • +
+
+
+Why is it necessary? +
    +
  • Music genre classification has several real-world applications, including music recommendation, content-based music retrieval, and personalized music services. However, the task of music genre classification is challenging due to the subjective nature of music and the complexity of audio signals.
  • +
+
+
+How is it beneficial and used? +
    +
  • For User: Provides more personalised music
  • +
  • For Developers: A recommendation system for songs that are of interest to the user
  • +
  • For Business: Able to charge premium for the more personalised and recommendation services provided
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Initially how the different sounds are structured.
  • +
  • Learned how to represent sound signal in 2D format on graphs using the librosa library.
  • +
  • Came to know about the various features of sound like
      +
    • Mel-frequency cepstral coefficients (MFCC)
    • +
    • Chromagram
    • +
    • Spectral Centroid
    • +
    • Zero-crossing rate
    • +
    • BPM - Beats Per Minute
    • +
    +
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+
There are 3 different types of the datasets.
+
+- genres_original
+- images_original
+- features_3_sec.csv
+- feature_30_sec.csv
+
+
    +
  • +

    The features in genres_original

    +

    ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock'] +Each and every genre has 100 WAV files

    +
  • +
  • +

    The features in genres_original

    +

    ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock'] +Each and every genre has 100 PNG files

    +
  • +
  • +

    There are 60 features in features_3_sec.csv

    +
  • +
  • +

    There are 60 features in features_30_sec.csv

    +
  • +
+
+

WHAT I HAVE DONE

+
+
+
+
    +
  • Created data visual reprsentation of the data to help understand the data
  • +
+
+
+
    +
  • Found strong relationships between independent features and dependent feature using correlation.
  • +
+
+
+
    +
  • Performed Exploratory Data Analysis on data.
  • +
+
+
+
    +
  • Used different Classification techniques like SVM, Random Forest,
  • +
+
+
+
    +
  • Compared various models and used best performance model to make predictions.
  • +
+
+
+
    +
  • Used Mean Squared Error and R2 Score for evaluating model's performance.
  • +
+
+
+
    +
  • Visualized best model's performance using matplotlib and seaborn library.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

How do you visualize audio signal

+
    +
  • +

    Solution:

    +
  • +
  • +

    librosa: It is the mother of all audio file libraries

    +
  • +
  • Plotting Graphs: As I have the necessary libraries to visualize the data. I started plotting the audio signals
  • +
  • Spectogram:A spectrogram is a visual representation of the spectrum of frequencies of a signal as it varies with time. When applied to an audio signal, spectrograms are sometimes called sonographs, voiceprints, or voicegrams. Here we convert the frequency axis to a logarithmic one.
  • +
+
+
+

Features that help classify the data

+
    +
  • +

    Solution:

    +
  • +
  • +

    Feature Engineering: What are the features present in audio signals

    +
  • +
  • Spectral Centroid: Indicates where the ”centre of mass” for a sound is located and is calculated as the weighted mean of the frequencies present in the sound.
  • +
  • Mel-Frequency Cepstral Coefficients: The Mel frequency cepstral coefficients (MFCCs) of a signal are a small set of features (usually about 10–20) which concisely describe the overall shape of a spectral envelope. It models the characteristics of the human voice.
  • +
  • Chroma Frequencies: Chroma features are an interesting and powerful representation for music audio in which the entire spectrum is projected onto 12 bins representing the 12 distinct semitones (or chroma) of the musical octave.
  • +
+
+
+

Performing EDA on the CSV files

+
    +
  • +

    Solution:

    +
  • +
  • +

    Tool Selection: Used the correlation matrix on the features_30_sec.csv dataset to extract most related datasets

    +
  • +
  • Visualization Best Practices: Followed best practices such as using appropriate chart types (e.g., box plots for BPM data, PCA plots for correlations), adding labels and titles, and ensuring readability.
  • +
  • Iterative Refinement: Iteratively refined visualizations based on feedback and self-review to enhance clarity and informativeness.
  • +
+
+
+

Implementing Machine Learning Models

+
    +
  • +

    Solution:

    +
  • +
  • +

    Cross-validation: Used cross-validation techniques to ensure the reliability and accuracy of the analysis results.

    +
  • +
  • Collaboration with Experts: Engaged with Music experts and enthusiasts to validate the findings and gain additional perspectives.
  • +
  • Contextual Understanding: Interpreted results within the context of the music, considering factors such as mood of the users, surrounding, and specific events to provide meaningful and actionable insights.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Error?};
+    B -->|Yes| C[Hmm...];
+    C --> D[Debug];
+    D --> B;
+    B ---->|No| E[Yay!];
+
+
+Visualizations and EDA of different features +
+
+
+

harm _perc

+
+
+

sound _wave

+
+
+

stft

+
+
+

pop _mel-_spec

+
+
+

blues _mel-_spec

+
+
+

spec _cent

+
+
+

spec _rolloff

+
+
+

m_f_c_c

+
+
+

chromogram

+
+
+

corr _heatmap

+
+
+

b_p_m _boxplot

+
+
+

p_c_a _scattert

+
+
+

conf matrix

+
+
+
+
+
+

MODELS USED AND THEIR ACCURACIES

+ + + + + + + + + + + + + + + + + + + + + + + + + +
ModelAccuracy
KNN0.80581
Random Forest0.81415
Cross Gradient Booster0.90123
SVM0.75409
+
+

MODELS COMPARISON GRAPHS

+
+

Models Comparison Graphs

+
+
+
+

accplot

+
+
+
+
+
+

CONCLUSION

+
We can see that Accuracy plots of the different models.
+XGB Classifier can predict most accurate results for predicting the Genre of the music.
+
+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  • Discovered a new library that help visualize audio signal
  • +
  • Discovered new features related to audio like STFT, MFCC, Spectral Centroid, Spectral Rolloff
  • +
  • Gained a deeper understanding of the features of different genres of music
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Enhanced knowledge of data cleaning and preprocessing techniques to handle real-world datasets.
  • +
  • Improved skills in exploratory data analysis (EDA) to extract meaningful insights from raw data.
  • +
  • Learned how to use visualization tools to effectively communicate data-driven findings.
  • +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+

User Personalisation

+
    +
  • It can be used to provide more personalised music recommendation for users based on their taste in music or the various genres they listen to. This personalisation experience can be used to develop 'Premium' based business models.
  • +
+
+
+

Compatability Between Users

+
    +
  • Based on the musical taste and the genres they listen we can identify the user behaviour and pattern come with similar users who can be friends with. This increases social interaction within the app.
  • +
+
+
+
+
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+
+
+
+
    +
  • +

    Real-time Compatability Tracking

    +
  • +
  • +

    Implementing a real-time tracking system to view compatability between users.

    +
  • +
+
+
+
    +
  • +

    Predictive Analytics

    +
  • +
  • +

    Using advanced machine learning algorithms to predict the next song the users is likely to listen to.

    +
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/deep-learning/brain-tumor-detection-model/index.html b/projects/deep-learning/brain-tumor-detection-model/index.html new file mode 100644 index 00000000..d9913ab7 --- /dev/null +++ b/projects/deep-learning/brain-tumor-detection-model/index.html @@ -0,0 +1,784 @@ + + + + + + + + + + + + + + + + + + + + + Brain Tumor Detectioon - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Brain Tumor Detectioon

+

AIM

+

To predict the Brain Tumor using Convolutional Neural Network

+ +

https://www.kaggle.com/datasets/primus11/brain-tumor-mri

+ +

https://colab.research.google.com/github/11PRIMUS/ALOK/blob/main/Tumor3.ipynb

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn (>=1.5.0 for TunedThresholdClassifierCV)
  • +
  • matplotlib
  • +
  • seaborn
  • +
  • streamlit
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • This project aims to predict early stage brain tumor it uses Convolutional Neural Network to classify wheter tumor is present or not.
  • +
+
+
+Why is it necessary? +
    +
  • Brain Tumor is leading case of deaths on world and most of the cases can be solved by detecting the cancer in its initial stages so one can take medication according to that without having further risks.
  • +
+
+
+How is it beneficial and used? +
    +
  • Doctors can use it to detect cancer and the region affected by that using MRI scans an help patient to overcom ethat with right and proper guidance. It also acts as a fallback mechanism in rare cases where the diagnosis is not obvious.
  • +
  • People (patients in particular) can check simply by using MRI scans to detect Tumor and take necessary medication and precautions
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Going through previous research and articles related to the problem.
  • +
  • Data exploration to understand the features.
  • +
  • Identifying key metrics for the problem based on ratio of target classes.
  • +
  • Feature engineering and selection based on EDA.
  • +
  • Setting up a framework for easier testing of multiple models even for peoples.
  • +
  • Analysing results of models simply using MRI scans
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

Model Architecture

+
- The CNN architecture is designed to perform binary classification. The key layers used in the architecture are:
+- Convolutional Layers: For feature extraction from images. MaxPooling Layers: To downsample the image features. Dense Layers: To perform the classification. Dropout: For regularization to prevent overfitting.
+
+

Model Structure

+
- Input Layer 224*224 pixels.
+- Convolutionla layer followed by MaxPooling layers.
+- Flattern layer to convert feature into 1D vector
+- Fully connected layer for Classification.
+- Output Layer: Sigmoid activation for binary classification (tumor/no tumor)
+
+
+

WHAT I HAVE DONE

+
+
+
+

Exploratory Data Analysis

+
    +
  • Summary statistics
  • +
  • Data visualization for numerical feature distributions
  • +
  • Splitting of data (70% for training, 15% for validation and 15% for testing)
  • +
+
+
+

Data cleaning and Preprocessing

+
    +
  • Data preperation using Image Generator
  • +
  • Categorical feature encoding
  • +
  • Image resized to 224*224 pixels
  • +
+
+
+

Feature engineering and selection

+
    +
  • Combining original features based on domain knowledge
  • +
  • Using MobileNet to process input
  • +
+
+
+

Modeling

+
    +
  • Convolutional layer followed by MaxPooling layer
  • +
  • Flattering the layer to convert features into 1D vector
  • +
  • Sigmoid function to actiavte binary classification
  • +
  • Holdout dataset created or model testing
  • +
  • Using VGG16 and ResNet for future improvement
  • +
+
+
+

Result analysis

+
    +
  • Hosted on Streamlit to ensure one can easily upload MRI scans and detect wether canncer is present or not.
  • +
  • Early stopping to ensure better accuracy is achieved.
  • +
  • Experiment with differnt agumentation techniques to improve model's robustness.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Accuracy vs Validation_accuracy: +The training accuracy is much higher than the validation accuracy after epoch 2, suggesting that the model may be overfitting the training data.

+
    +
  • Solution: It might be better to stop training around epoch 2 or 3 to avoid overfitting and ensure better generalization.
  • +
+
+
+
+
+

CONCLUSION

+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  • Early detection of cancer can lead to easily reduce the leading death of person and take proper medication on that basis.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Learned and implemented the concept of predicting probability and tuning the prediction threshold for more accurate results, compared to directly predicting with the default thresold for models.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Resigning the RGB image to grayscale and reducing the pixel by 225 * 225 was big challenge.
  • +
  • Lesser dataset so we reached out to some hospitals which helped us collecting the MRI scans.
  • +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+
    +
  • Doctors can identify the cancer stage and type accurately, allowing for tailored treatment approaches.
  • +
+
+
+
    +
  • Treatments at early stages are often less invasive and have fewer side effects compared to late-stage therapies
  • +
+
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/deep-learning/index.html b/projects/deep-learning/index.html new file mode 100644 index 00000000..05456588 --- /dev/null +++ b/projects/deep-learning/index.html @@ -0,0 +1,502 @@ + + + + + + + + + + + + + + + + + + + + + Deep Learning ✨ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/generative-adversarial-networks/index.html b/projects/generative-adversarial-networks/index.html new file mode 100644 index 00000000..d896e978 --- /dev/null +++ b/projects/generative-adversarial-networks/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Generative Adversarial Networks 💱 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Generative Adversarial Networks 💱

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/index.html b/projects/index.html new file mode 100644 index 00000000..bfde6eb1 --- /dev/null +++ b/projects/index.html @@ -0,0 +1,631 @@ + + + + + + + + + + + + + + + + + + + + + + + + + 🎉 Projects - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/large-language-models/index.html b/projects/large-language-models/index.html new file mode 100644 index 00000000..8ad43bc3 --- /dev/null +++ b/projects/large-language-models/index.html @@ -0,0 +1,497 @@ + + + + + + + + + + + + + + + + + + + + + Large Language Models 🤪 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Large Language Models 🤪

+
+
+ +
+

No Items Found

+

+ There are no items available at this time. Check back again later. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/air-quality-prediction/index.html b/projects/machine-learning/air-quality-prediction/index.html new file mode 100644 index 00000000..a2e5a78c --- /dev/null +++ b/projects/machine-learning/air-quality-prediction/index.html @@ -0,0 +1,962 @@ + + + + + + + + + + + + + + + + + + + + + Air Quality Prediction Model - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Air Quality Prediction Model

+
+ +
+ +

🎯 AIM

+

To predict air quality levels based on various features such as CO (Carbon Monoxide), NO (Nitrogen Oxides), NO2 (Nitrogen Dioxide), O3 (Ozone), and other environmental factors. By applying machine learning models, this project explores how different algorithms perform in predicting air quality and understanding the key factors that influence it.

+ +

https://www.kaggle.com/datasets/fedesoriano/air-quality-data-set

+

📓 NOTEBOOK

+

https://www.kaggle.com/code/disha520/air-quality-predictor

+
+Kaggle Notebook +

+
+

⚙️ TECH STACK

+ + + + + + + + + + + + + + + + + + + + + +
CategoryTechnologies
LanguagesPython
Libraries/FrameworksPandas, Numpy, Matplotlib, Seaborn, Scikit-learn
ToolsGit, Jupyter, VS Code
+
+

📝 DESCRIPTION

+

The project focuses on predicting air quality levels based on the features of air pollutants and environmental parameters. +The objective is to test various regression models to see which one gives the best predictions for CO (Carbon Monoxide) levels.

+
+

What is the requirement of the project?

+
    +
  • Air quality is a critical issue for human health, and accurate forecasting models can provide insights to policymakers and the public.
  • +
  • To accurately predict the CO levels based on environmental data.
  • +
+
+
+How is it beneficial and used? +
    +
  • Predicting air quality can help in early detection of air pollution and assist in controlling environmental factors effectively.
  • +
  • This model can be used by environmental agencies, city planners, and policymakers to predict and manage air pollution in urban areas, contributing to better public health outcomes.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Began by cleaning the dataset, handling missing data, and converting categorical features into numerical data.
  • +
  • After preparing the data, various machine learning models were trained and evaluated to identify the best-performing model.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • Kaggle kernels and documentation for additional dataset understanding.
  • +
  • Tutorials on machine learning regression techniques, particularly for Random Forest, SVR, and Decision Trees.
  • +
+
+
+

🔍 EXPLANATION

+

🧩 DETAILS OF THE DIFFERENT FEATURES

+

📂 AirQuality.csv

+ + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescription
CO(GT)Carbon monoxide in the air
Date & TimeRecord of data collection time
PT08.S1(CO), PT08.S2(NMHC), PT08.S3(NOX), PT08.S4(NO2), PT08.S5(O3)These are sensor readings for different gas pollutants
T, RH, AHTemperature, Humidity, and Absolute Humidity respectively, recorded as environmental factors
+
+

🛤 PROJECT WORKFLOW

+
  graph LR
+    A[Start] --> B{Is data clean?};
+    B -->|Yes| C[Explore Data];
+    C --> D[Data Preprocessing];
+    D --> E[Feature Selection & Engineering];
+    E --> F[Split Data into Training & Test Sets];
+    F --> G[Define Models];
+    G --> H[Train and Evaluate Models];
+    H --> I[Visualize Evaluation Metrics];
+    I --> J[Model Testing];
+    J --> K[Conclusion and Observations];
+    B ---->|No| L[Clean Data];
+
+
+
+
    +
  • First, we import all the essential libraries needed for handling, analyzing, and modeling the dataset.
  • +
  • This includes libraries like Pandas for data manipulation, Numpy for numerical computations, Matplotlib and Seaborn for data visualization, and Scikit-learn for machine learning models, evaluation, and data preprocessing.
  • +
  • These libraries will enable us to perform all required tasks efficiently.
  • +
+
+
+
    +
  • We load the dataset using Pandas read_csv() function. The dataset contains air quality data, which is loaded with a semicolon delimiter.
  • +
  • After loading, we inspect the first few rows to understand the structure of the data and ensure that the dataset is correctly loaded.
  • +
+
+
+

Data cleaning is a crucial step in any project. In this step:

+
    +
  • We remove unnamed columns that aren't useful for analysis (such as 'Unnamed: 15', 'Unnamed: 16').
  • +
  • We correct data consistency issues, specifically replacing commas with periods in numeric columns to ensure the correct parsing of values.
  • +
  • Missing values in numeric columns are replaced with the mean of that respective column.
  • +
  • We eliminate rows that consist entirely of missing values (NaN).
  • +
  • A new datetime feature is created by combining the 'Date' and 'Time' columns.
  • +
  • Additional temporal features such as month, day, weekday, and hour are derived from the new datetime feature.
  • +
  • The original Date and Time columns are dropped as they are no longer needed.
  • +
+
+
+
    +
  • To understand relationships among the features, a heatmap is used to visualize correlations between all numeric columns.
  • +
  • The heatmap highlights how features are correlated with each other, helping to identify possible redundancies or important predictors for the target variable.
  • +
+
+
+

After cleaning the data, we separate the dataset into features (X) and the target variable (y):

+
    +
  • Features (X): These are the columns used to predict the target value. We exclude the target variable column ‘CO(GT)’ and include all other columns as features.
  • +
  • Target (y): This is the variable we want to predict. We extract the 'CO(GT)' column and ensure all values are numeric.
  • +
+

To prepare the data for machine learning, any non-numeric columns in the features (X) are encoded using LabelEncoder.

+
+
+
    +
  • We split the dataset into training and testing sets, allocating 80% of the data for training and the remaining 20% for testing.
  • +
  • This split allows us to evaluate model performance on unseen data and validate the effectiveness of the model.
  • +
+
+
+

We define multiple regression models to train and evaluate on the dataset:

+
    +
  • RandomForestRegressor: A robust ensemble method that performs well on non-linear datasets.
  • +
  • LinearRegression: A fundamental regression model, useful for establishing linear relationships.
  • +
  • SVR (Support Vector Regression): A regression model based on Support Vector Machines, useful for complex, non-linear relationships.
  • +
  • DecisionTreeRegressor: A decision tree-based model, capturing non-linear patterns and interactions.
  • +
+
+
+

Each model is trained on the training data and used to make predictions on the testing set. The performance is evaluated using two metrics:

+
    +
  • Mean Absolute Error (MAE): Measures the average error between predicted and actual values.
  • +
  • R2 Score: Represents the proportion of the variance in the target variable that is predictable from the features.
  • +
+

The evaluation metrics for each model are stored for comparison.

+
+
+

We visualize the evaluation results for all models to get a comparative view of their performances. Two plots are generated:

+
    +
  • Mean Absolute Error (MAE) for each model, showing how much deviation there is between predicted and actual values.
  • +
  • R2 Score, depicting the models' ability to explain the variability in the target variable. Higher R2 values indicate a better fit.
  • +
+

These visualizations make it easy to compare model performances and understand which model is performing the best.

+
+
+
    +
  • In this final step, we summarize the results and draw conclusions based on the evaluation metrics. We discuss which model achieved the best performance in terms of both MAE and R2 Score, along with insights from the data cleaning and feature engineering steps.
  • +
  • Key observations include the importance of feature selection, the efficacy of different models for regression tasks, and which model has the most accurate predictions based on the dataset at hand.
  • +
+
+
+
+
+

🖥 CODE EXPLANATION

+
+

⚖️ PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Trade-off: Choosing between model accuracy and training time.
  • +
  • Solution: Random Forest was chosen due to its balance between accuracy and efficiency, with SVR considered for its powerful predictive power despite longer training time.
  • +
+
+
+
    +
  • Trade-off: Model interpretability vs complexity.
  • +
  • Solution: Decision trees were avoided in favor of Random Forest, which tends to be more robust in dealing with complex data and prevents overfitting.
  • +
+
+
+
+
+

🖼 SCREENSHOTS

+
+

Visualizations and EDA of different features

+
+
+
+

img

+
+
+

model-comparison

+
+
+
+
+
+

📉 MODELS USED AND THEIR EVALUATION METRICS

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ModelMean Absolute Error (MAE)R2 Score
Random Forest Regressor1.23910.885
Linear Regression1.45920.82
SVR1.32100.843
Decision Tree Regressor1.51380.755
+
+

✅ CONCLUSION

+

🔑 KEY LEARNINGS

+
+

Insights gained from the data

+
    +
  • Learned how different machine learning models perform on real-world data and gained insights into their strengths and weaknesses.
  • +
  • Understood the significance of feature engineering and preprocessing to achieve better model performance.
  • +
  • Data had missing values that required filling.
  • +
  • Feature creation from datetime led to better prediction accuracy.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Learned how to effectively implement and optimize machine learning models using libraries like scikit-learn.
  • +
+
+
+

🌍 USE CASES

+
+
+
+

Predicting Air Quality in Urban Areas

+
    +
  • Local governments can use this model to predict air pollution levels and take early actions to reduce pollution in cities.
  • +
+
+
+

Predicting Seasonal Air Pollution Levels

+
    +
  • The model can help forecast air quality during different times of the year, assisting in long-term policy planning.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/cardiovascular-disease-prediction/index.html b/projects/machine-learning/cardiovascular-disease-prediction/index.html new file mode 100644 index 00000000..8b49f040 --- /dev/null +++ b/projects/machine-learning/cardiovascular-disease-prediction/index.html @@ -0,0 +1,1201 @@ + + + + + + + + + + + + + + + + + + + + + Cardiovascular Disease Prediction - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Cardiovascular Disease Prediction

+

AIM

+

To predict the risk of cardiovascular disease based on lifestyle factors.

+ +

https://www.kaggle.com/datasets/alphiree/cardiovascular-diseases-risk-prediction-dataset

+ +

https://www.kaggle.com/code/sid4ds/cardiovascular-disease-risk-prediction

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn (>=1.5.0 for TunedThresholdClassifierCV)
  • +
  • matplotlib
  • +
  • seaborn
  • +
  • joblib
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • This project aims to predict the risk of cardivascular diseases (CVD) based on data provided by people about their lifestyle factors. Predicting the risk in advance can minimize cases which reach a terminal stage.
  • +
+
+
+Why is it necessary? +
    +
  • CVD is one of the leading causes of death globally. Using machine learning models to predict risk of CVD can be an important tool in helping the people affected by it.
  • +
+
+
+How is it beneficial and used? +
    +
  • Doctors can use it as a second opinion to support their diagnosis. It also acts as a fallback mechanism in rare cases where the diagnosis is not obvious.
  • +
  • People (patients in particular) can track their risk of CVD based on their own lifestyle and schedule an appointment with a doctor in advance to mitigate the risk.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Going through previous research and articles related to the problem.
  • +
  • Data exploration to understand the features. Using data visualization to check their distributions.
  • +
  • Identifying key metrics for the problem based on ratio of target classes.
  • +
  • Feature engineering and selection based on EDA.
  • +
  • Setting up a framework for easier testing of multiple models.
  • +
  • Analysing results of models using confusion matrix.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescriptionTypeValues/Range
General_Health"Would you say that in general your health is—"Categorical[Poor, Fair, Good, Very Good, Excellent]
Checkup"About how long has it been since you last visited a doctor for a routine checkup?"Categorical[Never, 5 or more years ago, Within last 5 years, Within last 2 years, Within the last year]
Exercise"Did you participate in any physical activities like running, walking, or gardening?"Categorical[Yes, No]
Skin_CancerRespondents that reported having skin cancerCategorical[Yes, No]
Other_CancerRespondents that reported having any other types of cancerCategorical[Yes, No]
DepressionRespondents that reported having a depressive disorderCategorical[Yes, No]
DiabetesRespondents that reported having diabetes. If yes, specify the type.Categorical[Yes, No, No pre-diabetes or borderline diabetes, Yes but female told only during pregnancy]
ArthritisRespondents that reported having arthritisCategorical[Yes, No]
SexRespondent's genderCategorical[Yes, No]
Age_CategoryRespondent's age rangeCategorical['18-24', '25-34', '35-44', '45-54', '55-64', '65-74', '75-80', '80+']
Height_(cm)Respondent's height in cmNumericalMeasured in cm
Weight_(kg)Respondent's weight in kgNumericalMeasured in kg
BMIRespondent's Body Mass Index in kg/cm²NumericalMeasured in kg/cm²
Smoking_HistoryRespondent's smoking historyCategorical[Yes, No]
Alcohol_ConsumptionNumber of days of alcohol consumption in a monthNumericalInteger values
Fruit_ConsumptionNumber of servings of fruit consumed in a monthNumericalInteger values
Green_Vegetables_ConsumptionNumber of servings of green vegetables consumed in a monthNumericalInteger values
FriedPotato_ConsumptionNumber of servings of fried potato consumed in a monthNumericalInteger values
+
+

WHAT I HAVE DONE

+
+
+
+

Exploratory Data Analysis

+
    +
  • Summary statistics
  • +
  • Data visualization for numerical feature distributions
  • +
  • Target splits for categorical features
  • +
+
+
+

Data cleaning and Preprocessing

+
    +
  • Regrouping rare categories
  • +
  • Categorical feature encoding
  • +
  • Outlier clipping for numerical features
  • +
+
+
+

Feature engineering and selection

+
    +
  • Combining original features based on domain knowledge
  • +
  • Discretizing numerical features
  • +
+
+
+

Modeling

+
    +
  • Holdout dataset created or model testing
  • +
  • Models trained: Logistic Regression, Decision Tree, Random Forest, AdaBoost, HistGradient Boosting, Multi-Layer Perceptron
  • +
  • Class imbalance handled through:
  • +
  • Class weights, when supported by model architecture
  • +
  • Threshold tuning using TunedThresholdClassifierCV
  • +
  • Metric for model-tuning: F2-score (harmonic weighted mean of precision and recall, with twice the weightage for recall)
  • +
+
+
+

Result analysis

+
    +
  • Confusion matrix using predictions made on holdout test set
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Accuracy vs Recall: +Data is extremely imbalanced, with only ~8% representing the positive class. This makes accuracy unsuitable as a metric for our problem. It is critical to correctly predict all the positive samples, due to which we must focus on recall. However, this lowers the overall accuracy since some negative samples may be predicted as positive.

+
    +
  • Solution: Prediction threshold for models is tuned using F2-score to create a balance between precision and recall, with more importance given to recall. This maintains overall accuracy at an acceptable level while boosting recall.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Error?};
+    B -->|Yes| C[Hmm...];
+    C --> D[Debug];
+    D --> B;
+    B ---->|No| E[Yay!];
+
+
+Numerical feature distributions +
+
+
+

dist_height

+
+
+

dist_weight

+
+
+

dist_bmi

+
+
+

dist_alcohol

+
+
+

dist_fruit

+
+
+

dist_vegetable

+
+
+

dist_friedpotato

+
+
+
+
+
+Correlations +
+
+
+

pearson_correlation

+
+
+

spearman_correlation

+
+
+

kendall_correlation

+
+
+
+
+
+

MODELS USED AND THEIR ACCURACIES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Model + Feature setAccuracy (%)Recall (%)
Logistic Regression + Original76.2974.21
Logistic Regression + Extended76.2774.41
Logistic Regression + Selected72.6678.09
Decision Tree + Original72.7678.61
Decision Tree + Extended74.0976.69
Decision Tree + Selected75.5273.61
Random Forest + Original73.9777.33
Random Forest + Extended74.1076.61
Random Forest + Selected74.8074.05
AdaBoost + Original76.0374.49
AdaBoost + Extended74.9976.25
AdaBoost + Selected74.7675.33
Multi-Layer Perceptron + Original76.9172.81
Multi-Layer Perceptron + Extended73.2679.01
Multi-Layer Perceptron + Selected74.8675.05
Hist-Gradient Boosting + Original75.9873.49
Hist-Gradient Boosting + Extended75.6374.73
Hist-Gradient Boosting + Selected74.4075.85
+

MODELS COMPARISON GRAPHS

+
+

Logistic Regression

+
+
+
+

cm_logistic_original

+
+
+

cm_logistic_extended

+
+
+

cm_logistic_selected

+
+
+
+
+
+Decision Tree +
+
+
+

cm_decisiontree_original

+
+
+

cm_decisiontree_extended

+
+
+

cm_decisiontree_selected

+
+
+
+
+
+Random Forest +
+
+
+

cm_randomforest_original

+
+
+

cm_randomforest_extended

+
+
+

cm_randomforest_selected

+
+
+
+
+
+Ada Boost +
+
+
+

cm_adaboost_original

+
+
+

cm_adaboost_extended

+
+
+

cm_adaboost_selected

+
+
+
+
+
+Multi-Layer Perceptron +
+
+
+

cm_mlpnn_original

+
+
+

cm_mlpnn_extended

+
+
+

cm_mlpnn_selected

+
+
+
+
+
+Hist-Gradient Boosting +
+
+
+

cm_histgradient_original

+
+
+

cm_histgradient_extended

+
+
+

cm_histgradient_selected

+
+
+
+
+
+

CONCLUSION

+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  • General Health, Age and Co-morbities (such as Diabetes & Arthritis) are the most indicative features for CVD risk.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Learned and implemented the concept of predicting probability and tuning the prediction threshold for more accurate results, compared to directly predicting with the default thresold for models.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Deciding the correct metric for evaluation of models due to imbalanced nature of the dataset. Since positive class is more important, Recall was used as the final metric for ranking models.
  • +
  • F2-score was used to tune the threshold for models to maintain a balance between precision and recall, thereby maintaining overall accuracy.
  • +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+
    +
  • Doctors can use it as a second opinion when assessing a new patient. Model trained on cases from previous patients can be used to predict the risk.
  • +
+
+
+
    +
  • People (patients in particular) can use this tool to track the risk of CVD based on their own lifestyle factors and take preventive measures when the risk is high.
  • +
+
+
+
+
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+
+
+
+
    +
  • Different implementations of gradient-boosting models such as XGBoost, CatBoost, LightGBM, etc. were not implemented since none of the tree ensemble models such as Random Forest, AdaBoost or Hist-Gradient Boosting were among the best performers. Hence, avoid additional dependencies based on such models.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/health-insurance-cross-sell-prediction/index.html b/projects/machine-learning/health-insurance-cross-sell-prediction/index.html new file mode 100644 index 00000000..52ead9e3 --- /dev/null +++ b/projects/machine-learning/health-insurance-cross-sell-prediction/index.html @@ -0,0 +1,1308 @@ + + + + + + + + + + + + + + + + + + + + + Health Insurance Cross-Sell Prediction - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Health Insurance Cross-Sell Prediction

+

AIM

+

To predict whether a Health Insurance customer would be interested in buying Vehicle Insurance.

+ +

https://www.kaggle.com/datasets/anmolkumar/health-insurance-cross-sell-prediction

+ +

https://www.kaggle.com/code/sid4ds/insurance-cross-sell-prediction-eda-modeling

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn (>=1.5.0 for TunedThresholdClassifierCV)
  • +
  • xgboost
  • +
  • catboost
  • +
  • lightgbm
  • +
  • matplotlib
  • +
  • seaborn
  • +
  • joblib
  • +
+
+
+

DESCRIPTION

+
+

Why is it necessary?

+
    +
  • This project aims to predict the chances of cross-selling Vehicle insurance to existing Health insurance customers. This would be extremely helpful for companies because they can then accordingly plan communication strategy to reach out to those customers and optimise their business model and revenue.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Going through previous research and articles related to the problem.
  • +
  • Data exploration to understand the features. Using data visualization to check their distributions.
  • +
  • Identifying key metrics for the problem based on ratio of target classes - ROC-AUC & Matthew's Correlation Coefficient (MCC) instead of Accuracy.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescriptionTypeValues/Range
idUnique ID for the customerNumericalUnique numerical values
GenderBinary gender of the customerBinary[0: Male, 1: Female] (or other binary representations as applicable)
AgeNumerical age of the customerNumericalMeasured in years
Driving_LicenseIndicates if the customer has a Driving LicenseBinary[0: No, 1: Yes]
Region_CodeUnique code for the customer's regionNumericalUnique numerical values
Previously_InsuredIndicates if the customer already has Vehicle InsuranceBinary[0: No, 1: Yes]
Vehicle_AgeAge of the vehicle categorized as ordinal valuesCategorical[< 1 year, 1-2 years, > 2 years]
Vehicle_DamageIndicates if the vehicle was damaged in the pastBinary[0: No, 1: Yes]
Annual_PremiumAmount to be paid as premium over the yearNumericalMeasured in currency
Policy_Sales_ChannelAnonymized code for the channel of customer outreachNumericalUnique numerical codes representing various channels
VintageNumber of days the customer has been associated with the companyNumericalMeasured in days
+
+

WHAT I HAVE DONE

+
+
+
+

Exploratory Data Analysis

+
    +
  • Summary statistics
  • +
  • Data visualization for numerical feature distributions
  • +
  • Target splits for categorical features
  • +
+
+
+

Data cleaning and Preprocessing

+
    +
  • Removing duplicates
  • +
  • Categorical feature encoding
  • +
+
+
+

Feature engineering and selection

+
    +
  • Discretizing numerical features
  • +
  • Feature selection based on model-based feature importances and statistical tests.
  • +
+
+
+

Modeling

+
    +
  • Holdout dataset created or model testing
  • +
  • Setting up a framework for easier testing of multiple models.
  • +
  • Models trained: Logistic Regression, Linear Discriminant Analysis, Quadratic Discriminant Analysis, Gaussian Naive-Bayes, Decision Tree, Random Forest, AdaBoost, Multi-Layer Perceptron, XGBoost, CatBoost, LightGBM
  • +
  • Class imbalance handled through:
  • +
  • Class weights, when supported by model architecture
  • +
  • Threshold tuning using TunedThresholdClassifierCV
  • +
  • Metric for model-tuning: F1-score (harmonic weighted mean of precision and recall)
  • +
+
+
+

Result analysis

+
    +
  • Predictions made on holdout test set
  • +
  • Models compared based on classification report and chosen metrics: ROC-AUC and MCC.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Accuracy vs Recall & Precision

+

Data is heavily imbalanced, with only ~12% representing the positive class. This makes accuracy unsuitable as a metric for our problem. Our goal is to correctly predict all the positive samples, due to which we must focus on recall. However, this lowers the overall accuracy since some negative samples may be predicted as positive.

+
    +
  • Solution: Prediction threshold for models is tuned using F1-score to create a balance between precision and recall. This maintains overall accuracy at an acceptable level while boosting recall.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Error?};
+    B -->|Yes| C[Hmm...];
+    C --> D[Debug];
+    D --> B;
+    B ---->|No| E[Yay!];
+
+
+Feature distributions (Univariate Analysis) +
+
+
+

01_featdist_age

+
+
+

02_featdist_license

+
+
+

03_featdist_regioncode

+
+
+

04_featdist_previnsured

+
+
+

05_featdist_vehicleage

+
+
+

06_featdist_vehicledamage

+
+
+

07_featdist_premium

+
+
+

08_featdist_policychannel

+
+
+

09_featdist_vintage

+
+
+
+
+
+Engineered Features +
+
+
+

10_featengg_agegroup

+
+
+

11_featengg_policygroup

+
+
+
+
+
+Feature Distributions (Bivariate Analysis) +
+
+
+

12_bivariate_pairplots

+
+
+

13_bivariate_spearmancorr

+
+
+

14_bivariate_pointbiserial

+
+
+

15_bivariate_tetrachoric

+
+
+
+
+
+Feature Selection +
+
+
+

16_featselect_pointbiserial

+
+
+

17_featselect_anova

+
+
+

18_featselect_tetrachoric

+
+
+

19_featselect_chisquared

+
+
+

20_featselect_mutualinfo

+
+
+

21_featselect_xgbfimp

+
+
+

22_featselect_xtreesfimp

+
+
+
+
+
+

MODELS USED AND THEIR PERFORMANCE

+
Best threshold after threshold tuning is also mentioned.
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Model + Feature setROC-AUCMCCBest threshold
Logistic Regression + Original0.83360.36710.65
Logistic Regression + Extended0.84560.38210.66
Logistic Regression + Reduced0.84550.37920.67
Logistic Regression + Minimal0.81770.35070.60
Linear DA + Original0.83260.35840.19
Linear DA + Extended0.84230.37850.18
Linear DA + Reduced0.84210.37680.18
Linear DA + Minimal0.81850.34730.15
Quadratic DA + Original0.83530.37790.45
Quadratic DA + Extended0.84180.37930.54
Quadratic DA + Reduced0.84220.38070.44
Quadratic DA + Minimal0.82120.35870.28
Gaussian Naive Bayes + Original0.82300.38790.78
Gaussian Naive Bayes + Extended0.82420.39140.13
Gaussian Naive Bayes + Reduced0.82400.39080.08
Gaussian Naive Bayes + Minimal0.80550.36050.15
K-Nearest Neighbors + Original0.78190.34610.09
K-Nearest Neighbors + Extended0.78250.34690.09
K-Nearest Neighbors + Reduced0.77100.34050.01
K-Nearest Neighbors + Minimal0.75610.32010.01
Decision Tree + Original0.84200.39250.67
Decision Tree + Extended0.84200.39250.67
Decision Tree + Reduced0.84190.39250.67
Decision Tree + Minimal0.82620.36830.63
Random Forest + Original0.85050.38240.70
Random Forest + Extended0.85080.38320.70
Random Forest + Reduced0.85080.38200.71
Random Forest + Minimal0.83750.37210.66
Extra-Trees + Original0.84590.37700.70
Extra-Trees + Extended0.85040.38470.71
Extra-Trees + Reduced0.85150.38360.72
Extra-Trees + Minimal0.83370.36820.67
AdaBoost + Original0.83940.38940.83
AdaBoost + Extended0.83940.38940.83
AdaBoost + Reduced0.84040.38390.84
AdaBoost + Minimal0.82690.36430.86
Multi-Layer Perceptron + Original0.85120.38990.22
Multi-Layer Perceptron + Extended0.85280.38650.23
Multi-Layer Perceptron + Reduced0.85170.38920.23
Multi-Layer Perceptron + Minimal0.83650.36630.21
XGBoost + Original0.85850.39800.68
XGBoost + Extended0.85850.39800.68
XGBoost + Reduced0.85840.39670.68
XGBoost + Minimal0.84590.37650.66
CatBoost + Original0.85790.39510.46
CatBoost + Extended0.85780.39810.45
CatBoost + Reduced0.85770.39750.45
CatBoost + Minimal0.84490.37810.42
LightGBM + Original0.85870.39780.67
LightGBM + Extended0.85870.39760.67
LightGBM + Reduced0.85870.39830.67
LightGBM + Minimal0.84620.37530.66
+
+

CONCLUSION

+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  1. Previously_Insured, Vehicle_Damage, Policy_Sales_Channel and Age are the most informative features for predicting cross-sell probability.
  2. +
  3. Vintage and Driving_License have no predictive power. They are not included in the best model.
  4. +
+
+
+Improvements in understanding machine learning concepts +
    +
  1. Implemented threshold-tuning for more accurate results.
  2. +
  3. Researched and utilized statistical tests for feature selection.
  4. +
+
+
+Challenges faced and how they were overcome +
    +
  1. Shortlisting the apropriate statistical test for bivariate analysis and feature selection.
  2. +
  3. Deciding the correct metric for evaluation of models due to imbalanced nature of the dataset.
  4. +
  5. F1-score was used for threshold-tuning. ROC-AUC score and MCC were used for model comparison.
  6. +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+
    +
  • Companies can use customer data to predict which customers to target for cross-sell marketing. This saves cost and effort for the company, and protects uninterested customers from unnecessary marketing calls.
  • +
+
+
+
+
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+
+
+
+
    +
  • Complex model-ensembling through stacking or hill-climbing was not implemented due to significantly longer training time.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/heart-disease-detection-model/index.html b/projects/machine-learning/heart-disease-detection-model/index.html new file mode 100644 index 00000000..b9831569 --- /dev/null +++ b/projects/machine-learning/heart-disease-detection-model/index.html @@ -0,0 +1,921 @@ + + + + + + + + + + + + + + + + + + + + + Heart Disease Detection Model - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Heart Disease Detection Model

+

AIM

+

The aim of this project is to develop a reliable and efficient machine learning-based system for the early detection and diagnosis of heart disease. By leveraging advanced algorithms, the system seeks to analyze patient data, identify significant patterns, and predict the likelihood of heart disease, thereby assisting healthcare professionals in making informed decisions.

+ +

This project uses a publicly available heart disease dataset from UCI Machine Learning Repository

+ +

This is notebook of the following project Kaggle

+

LIBRARIES NEEDED

+
- pandas
+- numpy
+- scikit-learn
+- matplotlib
+- seaborn
+
+
+

DESCRIPTION

+

what is the requirement of the project?, +The project requires a dataset containing patient health records, including attributes like age, cholesterol levels, blood pressure, and medical history. Additionally, it needs machine learning tools and frameworks (e.g., Python, scikit-learn) for building and evaluating predictive models.

+

why is it necessary?, +Early detection of heart disease is crucial to prevent severe complications and reduce mortality rates. A machine learning-based system provides accurate, fast, and cost-effective predictions, aiding timely medical intervention and improved patient outcomes.

+

how is it beneficial and used?, +This system benefits healthcare by improving diagnostic accuracy and reducing reliance on invasive procedures. It can be used by doctors for decision support, by patients for risk assessment, and in hospitals for proactive healthcare management.

+

how did you start approaching this project?, +The project begins by collecting and preprocessing a heart disease dataset, ensuring it is clean and ready for analysis. Next, machine learning models are selected, trained, and evaluated to identify the most accurate algorithm for predicting heart disease.

+

Any additional resources used like blogs reading, books reading (mention the name of book along with the pages you have read)? +Kaggle kernels and documentation for additional dataset understanding. +Tutorials on machine learning regression techniques, particularly for Random Forest, SVR, and Decision Trees.

+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+ + +

Age: Patient's age in years.

+

Sex: Gender of the patient (1 = male; 0 = female).

+

Chest Pain Type (cp): Categorized as:

+

0: Typical angina +1: Atypical angina +2: Non-anginal pain +3: Asymptomatic +Resting Blood Pressure (trestbps): Measured in mm Hg upon hospital admission.

+

Serum Cholesterol (chol): Measured in mg/dL.

+

Fasting Blood Sugar (fbs): Indicates if fasting blood sugar > 120 mg/dL (1 = true; 0 = false).

+

Resting Electrocardiographic Results (restecg):

+

0: Normal +1: Having ST-T wave abnormality (e.g., T wave inversions and/or ST elevation or depression > 0.05 mV) +2: Showing probable or definite left ventricular hypertrophy by Estes' criteria +Maximum Heart Rate Achieved (thalach): Peak heart rate during exercise.

+

Exercise-Induced Angina (exang): Presence of angina induced by exercise (1 = yes; 0 = no).

+

Oldpeak: ST depression induced by exercise relative to rest.

+

Slope of the Peak Exercise ST Segment (slope):

+

0: Upsloping +1: Flat +2: Downsloping +Number of Major Vessels Colored by Fluoroscopy (ca): Ranges from 0 to 3.

+

Thalassemia (thal):

+

1: Normal +2: Fixed defect +3: Reversible defect +Target: Diagnosis of heart disease (0 = no disease; 1 = disease).

+
+

PROJECT WORKFLOW

+

1.Problem Definition

+

Identify the objective: To predict the presence or absence of heart disease based on patient data. +Define the outcome variable (target) and input features.

+

2.Data Collection

+

Gather a reliable dataset, such as the Cleveland Heart Disease dataset, which includes features relevant to heart disease prediction.

+

3.Data Preprocessing

+

Handle missing values: Fill or remove records with missing data. +Normalize/standardize data to ensure all features have comparable scales. +Encode categorical variables like sex, cp, and thal using techniques like one-hot encoding or label encoding.

+

4.Exploratory Data Analysis (EDA)

+

Visualize data distributions using histograms, boxplots, or density plots. +Identify relationships between features using correlation matrices and scatterplots. +Detect and handle outliers to improve model performance.

+

5.Feature Selection

+

Use statistical methods or feature importance metrics to identify the most relevant features for prediction. +Remove redundant or less significant features.

+

6.Data Splitting

+

Divide the dataset into training, validation, and testing sets (e.g., 70%-15%-15%). +Ensure a balanced distribution of the target variable in all splits.

+

7.Model Selection

+

Experiment with multiple machine learning algorithms such as Logistic Regression, Random Forest, Decision Trees, Support Vector Machines (SVM), and Neural Networks. +Select models based on the complexity and nature of the dataset.

+

8.Model Training

+

Train the chosen models using the training dataset. +Tune hyperparameters using grid search or random search techniques.

+

9.Model Evaluation

+

Assess models on validation and testing datasets using metrics such as: +Accuracy +Precision, Recall, and F1-score +Receiver Operating Characteristic (ROC) curve and Area Under the Curve (AUC). +Compare models to identify the best-performing one.

+

10.##Deployment and Prediction

+

Save the trained model using frameworks like joblib or pickle. +Develop a user interface (UI) or API for end-users to input data and receive predictions.

+

11.Iterative Improvement

+

Continuously refine the model using new data or advanced algorithms. +Address feedback and optimize the system based on real-world performance.

+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Accuracy vs. Interpretability
  • +
  • Complex models like Random Forests or Neural Networks offer higher accuracy but are less interpretable compared to simpler models like Logistic Regression.
  • +
+
+
+
    +
  • Overfitting vs. Generalization
  • +
  • Models with high complexity may overfit the training data, leading to poor generalization on unseen data.
  • +
+
+
+
+
+

MODELS USED AND THEIR EVALUATION METRICS

+ + + + + + + + + + + + + + + + + + + + + + + +
ModelScore
Logistic regression88%
K-Nearest Classifier68%
Random Forest Classifier86%
+
+

CONCLUSION

+

KEY LEARNINGS

+
    +
  1. +

    Data Insights +Understanding Healthcare Data: Learned how medical attributes (e.g., age, cholesterol, chest pain type) influence heart disease risk. +Data Imbalance: Recognized the challenges posed by imbalanced datasets and explored techniques like SMOTE and class weighting to address them. +Importance of Preprocessing: Gained expertise in handling missing values, scaling data, and encoding categorical variables, which are crucial for model performance.

    +
  2. +
  3. +

    Techniques Mastered +Exploratory Data Analysis (EDA): Applied visualization tools (e.g., histograms, boxplots, heatmaps) to uncover patterns and correlations in data. +Feature Engineering: Identified and prioritized key features using statistical methods and feature importance metrics. +Modeling: Implemented various machine learning algorithms, including Logistic Regression, Random Forest, Gradient Boosting, and Support Vector Machines. +Evaluation Metrics: Learned to evaluate models using metrics like Precision, Recall, F1-score, and ROC-AUC to optimize for healthcare-specific goals. +Hyperparameter Tuning: Used grid search and random search to optimize model parameters and improve performance. +Interpretability Tools: Utilized SHAP and feature importance analysis to explain model predictions.

    +
  4. +
  5. +

    Skills Developed +Problem-Solving: Addressed trade-offs such as accuracy vs. interpretability, and overfitting vs. generalization. +Critical Thinking: Improved decision-making on model selection, preprocessing methods, and evaluation strategies. +Programming: Strengthened Python programming skills, including the use of libraries like scikit-learn, pandas, matplotlib, and TensorFlow. +Collaboration: Enhanced communication and teamwork when discussing medical insights and technical challenges with domain experts. +Time Management: Balanced experimentation with computational efficiency, focusing on techniques that maximized impact. +Ethical Considerations: Gained awareness of ethical issues like ensuring fairness in predictions and minimizing false negatives, which are critical in healthcare applications.

    +
  6. +
  7. +

    Broader Understanding +Interdisciplinary Knowledge: Combined expertise from data science, healthcare, and statistics to create a meaningful application. +Real-World Challenges: Understood the complexities of translating machine learning models into practical tools for healthcare. +Continuous Learning: Learned that model development is iterative, requiring continuous refinement based on feedback and new data.

    +
  8. +
+

USE CASES

+
+
+
+

Clinical Decision Support Systems (CDSS)

+
    +
  • ML models can be integrated into Electronic Health Record (EHR) systems to assist doctors in diagnosing heart disease. The model can provide predictions based on patient data, helping clinicians make faster and more accurate decisions.
  • +
+
+
+

Early Screening and Risk Assessment

+
    +
  • Patients can undergo routine screening using a heart disease detection system to assess their risk level. The system can predict whether a patient is at high, moderate, or low risk, prompting early interventions or lifestyle changes.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/index.html b/projects/machine-learning/index.html new file mode 100644 index 00000000..e6a931ff --- /dev/null +++ b/projects/machine-learning/index.html @@ -0,0 +1,503 @@ + + + + + + + + + + + + + + + + + + + + + Machine Learning 🤖 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/poker-hand-prediction/index.html b/projects/machine-learning/poker-hand-prediction/index.html new file mode 100644 index 00000000..7b1e01e9 --- /dev/null +++ b/projects/machine-learning/poker-hand-prediction/index.html @@ -0,0 +1,1089 @@ + + + + + + + + + + + + + + + + + + + + + Poker Hand Prediction - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Poker Hand Prediction

+
+ +
+ +

AIM

+

The aim of this project is to develop a machine learning model using a Multi-Layer Perceptron (MLP) classifier to accurately classify different types of poker hands based on the suit and rank of five cards.

+ +

https://www.kaggle.com/datasets/dysphoria/poker-hand-classification

+ +

https://www.kaggle.com/code/supratikbhowal/poker-hand-prediction-model

+

LIBRARIES NEEDED

+
    +
  • pandas
  • +
  • numpy
  • +
  • matplotlib
  • +
  • seaborn
  • +
  • scikit-learn
  • +
+
+

DESCRIPTION

+

This project involves building a classification model to predict poker hands using a Multi-Layer Perceptron (MLP) classifier. The dataset consists of features representing the suit and rank of five cards, with the target variable being the type of poker hand (e.g., one pair, two pair, royal flush). The model is trained on a standardized dataset, with class weights computed to address class imbalance. Performance is evaluated using metrics such as accuracy, classification report, confusion matrix, prediction error, ROC curve, and AUC, providing a comprehensive analysis of the model's effectiveness in classifying poker hands.

+

What is the requirement of the project?
+- To accurately predict the Poker Hand type.

+

Why is it necessary?
+- The project demonstrates how machine learning can solve structured data problems, bridging the gap between theoretical knowledge and practical implementation.

+

How is it beneficial and used?
+- The project automates the classification of poker hands, enabling players to quickly and accurately identify the type of hand they have, such as a straight, flush, or royal flush, without manual effort.
+- By understanding the probabilities and patterns of certain hands appearing, players can make informed decisions on whether to bet, raise, or fold, improving their gameplay strategy.

+

How did you start approaching this project?
+- Analyzed the poker hand classification problem, reviewed the dataset structure (suits, ranks, and hand types), and identified the multi-class nature of the target variable. Studied the class imbalance issue and planned data preprocessing steps, including scaling and class weight computation.
+- Chose the Multi-Layer Perceptron (MLP) Classifier for its capability to handle complex patterns in data. Defined model hyperparameters, trained the model using standardized features, and evaluated its performance using metrics like accuracy, ROC-AUC, and confusion matrix

+

Mention any additional resources used:
+- Kaggle kernels and documentation for additional dataset understanding.
+- Tutorials on machine learning regression techniques, particularly for MLP

+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescriptionTypeValues/Range
S1Suit of card #1Ordinal(1-4) representing
C1Rank of card #1Numerical(1-13) representing (Ace, 2, 3, … , Queen, King)
S2Suit of card #2Ordinal(1-4) representing
C2Rank of card #2Numerical(1-13) representing (Ace, 2, 3, … , Queen, King)
S3Suit of card #3Ordinal(1-4) representing
C3Rank of card #3Numerical(1-13) representing (Ace, 2, 3, … , Queen, King)
S4Suit of card #4Ordinal(1-4) representing
C4Rank of card #4Numerical(1-13) representing (Ace, 2, 3, … , Queen, King)
S5Suit of card #5Ordinal(1-4) representing
C5Rank of card #5Numerical(1-13) representing (Ace, 2, 3, … , Queen, King)
Poker HandType of Card in HandOrdinal(0-9) types*
+

Poker Hands
+0: Nothing in hand, not a recognized poker hand
+1: One pair, one pair of equal ranks within five cards
+2: Two pairs, two pairs of equal ranks within five cards
+3: Three of a kind, three equal ranks within five cards
+4: Straight, five cards, sequentially ranked with no gaps
+5: Flush, five cards with the same suit
+6: Full house, pair + different rank three of a kind
+7: Four of a kind, four equal ranks within five cards
+8: Straight flush, straight + flush
+9: Royal flush, {Ace, King, Queen, Jack, Ten} + flush

+
+

PROJECT WORKFLOW

+

1. Dataset Loading and Inspection

+

The dataset is loaded into the environment, and its structure, features, and target classes are analyzed. Column names are assigned to enhance clarity and understanding.

+

2. Data Preprocessing

+

Target variables are mapped to descriptive hand labels, features and target variables are separated, and data is standardized using 'StandardScaler' for uniform scaling.

+

3. Handling Class Imbalance

+

Class weights are computed using 'compute_class_weight' to address the imbalance in target classes, ensuring fair training for all hand types.

+

4. Model Selection and Training

+

The MLPClassifier is selected for its capability to handle multi-class problems, configured with suitable hyperparameters, and trained on the preprocessed training data.

+

5. Model Evaluation

+

The model's performance is assessed using metrics such as accuracy, classification reports, and confusion matrices. Predictions are generated for the test dataset.

+

6. Visualization and Error Analysis

+

Class prediction errors are visualized using bar charts, and ROC curves with AUC values are generated for each hand type to evaluate classification effectiveness.

+

7. Insights and Interpretation

+

Strengths and weaknesses of the model are identified through error analysis, and the findings are presented using visual tools like heatmaps for better understanding.

+

8. Conclusion

+

The project outcomes are summarized, practical applications are discussed, and suggestions for further research or improvements are proposed.

+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
+
+
    +
  • Trade-off: Accuracy vs. Complexity.
      +
    • Solution: Using a Multi-Layer Perceptron (MLP) introduces complexity due to its hidden layers and numerous parameters. While this improves accuracy, it requires more computational resources and training time compared to simpler models like decision trees or logistic regression.
    • +
    +
  • +
+
+
+
+
+
+
    +
  • Trade-off: Bias vs. Variance
      +
    • Solution: The project's hyperparameter tuning (e.g., learning rate, number of hidden layers) aims to reduce bias and variance. However, achieving a perfect balance is a trade-off, as overly complex models may increase variance (overfitting), while overly simplified models may increase bias (underfitting).
    • +
    +
  • +
+
+
+
+
+
+
    +
  • Trade-off: Generalization vs. Overfitting
      +
    • Solution:The model's flexibility with complex hyperparameters (e.g., hidden layers, activation functions) risks overfitting, especially on small or imbalanced datasets. Regularization techniques like adjusting 'alpha' help mitigate this but may compromise some accuracy.
    • +
    +
  • +
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Is data clean?};
+    B -->|Yes| C[Explore Data];
+    C --> D[Data Preprocessing];
+    D --> E[Define Models];
+    E --> F[Train the Model];
+    F --> G[Evaluate Performance];
+    G --> H[Visualize Evaluation Metrics];
+    H --> I[Model Testing];
+    I --> J[Conclusion and Observations];
+    B ---->|No| K[Clean Data];
+    K --> C;
+
+
+Model Evaluation Metrics +
+
+
+

model_vs_accuracy

+
+
+

Roc_Curve_&_Auc

+
+
+

Classification_Heatmap

+
+
+
+
+
+

MODELS EVALUATION METRICS

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Poker Handprecisionrecallf1-scoresupport
flush0.520.070.121996
four_of_a_kind0.000.000.00230
full_house0.770.340.471424
one_pair1.001.001.00422498
royal_flush0.000.000.003
straight0.960.440.603885
straight_flush0.000.000.0012
three_of_a_kind0.950.000.0021121
two_pair1.001.001.0047622
zilch0.991.001.00501209
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
ModelAccuracy
Random Forest Regression0.6190
Gradient Boosting Regression0.3204
MLP Classifier0.9924
StackingClassifier0.9382
+

---

+

CONCLUSION

+

KEY LEARNINGS

+
    +
  • Learned how different machine learning models perform on real-world data and gained insights into their strengths and weaknesses.
  • +
  • Realized how important feature engineering and data preprocessing are for enhancing model performance.
  • +
+

Insights gained from the data:

+
    +
  • Class imbalance can bias the model, requiring class weighting for fair performance.
  • +
  • Suits and ranks of cards are crucial features, necessitating preprocessing for better model input.
  • +
+

Improvements in understanding machine learning concepts:

+
    +
  • Learned how to effectively implement and optimize machine learning models using libraries like scikit-learn.
  • +
  • Learned how to effectively use visualizations (e.g., ROC curves, confusion matrices, and heatmaps) to interpret and communicate model performance.
  • +
+

Challenges faced and how they were overcome:

+
    +
  • Some features had different scales, which could affect model performance. This was overcome by applying standardization to the features, ensuring they were on a similar scale for better convergence during training.
  • +
  • Some rare hands, such as "royal_flush" and "straight_flush," had very low prediction accuracy. This was mitigated by analyzing misclassification patterns and considering potential improvements like generating synthetic samples or using other models.
  • +
+
+

USE CASES OF THIS MODEL

+
+
+
+
+
+

Poker Strategy Development and Simulation
+ -> Developers or researchers studying poker strategies can use this model to simulate various hand combinations and evaluate strategic decisions. The model's classification can help assess the strength of different hands and optimize strategies.

+
+
+
+
+
+

Real-time Poker Hand Evaluation for Mobile Apps
+ -> Mobile apps that allow users to practice or play poker could incorporate this model to provide real-time hand evaluation, helping users understand the strength of their hands during gameplay.

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/sleep-quality-prediction/index.html b/projects/machine-learning/sleep-quality-prediction/index.html new file mode 100644 index 00000000..99edc8ea --- /dev/null +++ b/projects/machine-learning/sleep-quality-prediction/index.html @@ -0,0 +1,743 @@ + + + + + + + + + + + + + + + + + + + + + Sleep quality prediction - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + +
+
+ + + + +

Sleep Quality Prediction

+ +

AIM

+

To predict sleep quality based on lifestyle and health factors.

+ +
+

DATASET LINK

+

+ + Sleep Health and Lifestyle Dataset + +

+ +
+

DESCRIPTION

+ +

What is the requirement of the project?

+
    +
  • This project aims to predict the quality of sleep using various health and lifestyle metrics. Predicting sleep quality helps individuals and healthcare professionals address potential sleep-related health issues early.
  • +
+ +

Why is it necessary?

+
    +
  • Sleep quality significantly impacts physical and mental health. Early predictions can prevent chronic conditions linked to poor sleep, such as obesity, heart disease, and cognitive impairment.
  • +
+ +

How is it beneficial and used?

+
    +
  • Individuals: Assess their sleep health and make lifestyle changes to improve sleep quality.
  • +
  • Healthcare Professionals: Use the model as an auxiliary diagnostic tool to recommend personalized interventions.
  • +
+ +

How did you start approaching this project? (Initial thoughts and planning)

+
    +
  • Researching sleep health factors and existing literature.
  • +
  • Exploring and analyzing the dataset to understand feature distributions.
  • +
  • Preprocessing data for effective feature representation.
  • +
  • Iterating over machine learning models to find the optimal balance between accuracy and interpretability.
  • +
+ +

Mention any additional resources used

+ + +
+

LIBRARIES USED

+
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn
  • +
  • matplotlib
  • +
  • seaborn
  • +
  • joblib
  • +
  • flask
  • +
+ +
+

EXPLANATION

+ +

DETAILS OF THE DIFFERENT FEATURES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescriptionTypeValues/Range
GenderRespondent's genderCategorical[Male, Female]
AgeRespondent's ageNumericalMeasured in years
Sleep Duration (hours)Hours of sleep per dayNumericalMeasured in hours
Physical Activity LevelDaily physical activity in minutesNumericalMeasured in minutes
Stress LevelStress level on a scaleNumerical1 to 5 (low to high)
BMI CategoryBody Mass Index categoryCategorical[Underweight, Normal, Overweight, Obese]
Systolic Blood PressureSystolic blood pressureNumericalMeasured in mmHg
Diastolic Blood PressureDiastolic blood pressureNumericalMeasured in mmHg
Heart Rate (bpm)Resting heart rateNumericalBeats per minute
Daily StepsAverage number of steps per dayNumericalMeasured in steps
Sleep DisorderReported sleep disorderCategorical[Yes, No]
+ +
+

WHAT I HAVE DONE

+ +

Step 1: Exploratory Data Analysis

+
    +
  • Summary statistics
  • +
  • Data visualization for numerical feature distributions
  • +
  • Target splits for categorical features
  • +
+ +

Step 2: Data Cleaning and Preprocessing

+
    +
  • Handling missing values
  • +
  • Label encoding categorical features
  • +
  • Standardizing numerical features
  • +
+ +

Step 3: Feature Engineering and Selection

+
    +
  • Merging features based on domain knowledge
  • +
  • Creating derived features such as "Activity-to-Sleep Ratio"
  • +
+ +

Step 4: Modeling

+
    +
  • Model trained: Decision Tree
  • +
  • Class imbalance handled using SMOTE
  • +
  • Metric for optimization: F1-score
  • +
+ +

Step 5: Result Analysis

+
    +
  • Visualized results using confusion matrices and classification reports
  • +
  • Interpreted feature importance for tree-based models
  • +
+ +
+

MODELS USED AND THEIR ACCURACIES

+ + + + + + + + + + + + + + + + + + + + +
ModelAccuracy (%)F1-Score (%)Precision (%)Recall (%)
Decision Tree74.5075.2073.0077.50
+ +
+

CONCLUSION

+ +

WHAT YOU HAVE LEARNED

+ +
+

Insights gained from the data

+
    +
  • Sleep Duration, Stress Level, and Physical Activity are the most indicative features for predicting sleep quality.
  • +
+
+ +
+

Improvements in understanding machine learning concepts

+
    +
  • Learned and implemented preprocessing techniques like encoding categorical variables and handling imbalanced datasets.
  • +
  • Gained insights into deploying a machine learning model using Flask for real-world use cases.
  • +
+
+ +
+

Challenges faced and how they were overcome

+
    +
  • Managing imbalanced classes: Overcame this by using SMOTE for oversampling the minority class.
  • +
  • Choosing a simple yet effective model: Selected Decision Tree for its interpretability and ease of deployment.
  • +
+
+ +
+

USE CASES OF THIS MODEL

+ +
+

Application 1

+

+ A health tracker app can integrate this model to assess and suggest improvements in sleep quality based on user inputs. +

+
+ +
+

Application 2

+

+ Healthcare providers can use this tool to make preliminary assessments of patients' sleep health, enabling timely interventions. +

+
+ +
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+ +
+

Feature 1

+

+ Advanced models such as Random Forest, AdaBoost, and Gradient Boosting were not implemented due to the project's focus on simplicity and interpretability. +

+
+ +
+

Feature 2

+

+ Integration with wearable device data for real-time predictions was not explored but remains a potential enhancement for future work. +

+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/machine-learning/used-cars-price-prediction/index.html b/projects/machine-learning/used-cars-price-prediction/index.html new file mode 100644 index 00000000..2d3a9e28 --- /dev/null +++ b/projects/machine-learning/used-cars-price-prediction/index.html @@ -0,0 +1,1009 @@ + + + + + + + + + + + + + + + + + + + + + Used Cars Price Prediction - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Used Cars Price Prediction

+

AIM

+

Predicting the prices of used cars based on their configuration and previous usage.

+ +

https://www.kaggle.com/datasets/avikasliwal/used-cars-price-prediction

+ +

https://www.kaggle.com/code/sid4ds/used-cars-price-prediction/

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn (>=1.5.0 required for Target Encoding)
  • +
  • xgboost
  • +
  • catboost
  • +
  • matplotlib
  • +
  • seaborn
  • +
+
+
+

DESCRIPTION

+
+

Why is it necessary?

+
    +
  • This project aims to predict the prices of used cars listed on an online marketplace based on their features and usage by previous owners. This model can be used by sellers to estimate an approximate price for their cars when they list them on the marketplace. Buyers can use the model to check if the listed price is fair when they decide to buy a used vehicle.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Researching previous projects and articles related to the problem.
  • +
  • Data exploration to understand the features.
  • +
  • Identifying different preprocessing strategies for different feature types.
  • +
  • Choosing key metrics for the problem - Root Mean Squared Error (for error estimation), R2-Score (for model explainability)
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescriptionTypeValues/Range
NameCar modelCategoricalNames of car models
LocationCity where the car is listed for saleCategoricalNames of cities
YearYear of original purchase of carNumericalYears (e.g., 2010, 2015, etc.)
Kilometers_DrivenOdometer reading of the carNumericalMeasured in kilometers
Fuel_TypeFuel type of the carCategorical[Petrol, Diesel, CNG, Electric, etc.]
TransmissionTransmission type of the carCategorical[Automatic, Manual]
Owner_TypeNumber of previous owners of the carNumericalWhole numbers
MileageCurrent mileage provided by the carNumericalMeasured in km/l or equivalent
EngineEngine capacity of the carNumericalMeasured in CC (Cubic Centimeters)
PowerEngine power output of the carNumericalMeasured in BHP (Brake Horsepower)
SeatsSeating capacity of the carNumericalWhole numbers
New_PriceOriginal price of the car at the time of purchaseNumericalMeasured in currency
+
+

WHAT I HAVE DONE

+
+
+
+

Exploratory Data Analysis

+
    +
  • Summary statistics
  • +
  • Data visualization for numerical feature distributions
  • +
  • Target splits for categorical features
  • +
+
+
+

Data cleaning and Preprocessing

+
    +
  • Removing rare categories of brands
  • +
  • Removing outliers for numerical features and target
  • +
  • Categorical feature encoding for low-cardinality features
  • +
  • Target encoding for high-cardinality categorical features (in model pipeline)
  • +
+
+
+

Feature engineering and selection

+
    +
  • Extracting brand name from model name for a lower-cardinality feature.
  • +
  • Converting categorical Owner_Type to numerical Num_Previous_Owners.
  • +
  • Feature selection based on model-based feature importances and statistical tests.
  • +
+
+
+

Modeling

+
    +
  • Holdout dataset created for model testing
  • +
  • Setting up a framework for easier testing of multiple models.
  • +
  • Models trained: LLinear Regression, K-Nearest Neighbors, Decision Tree, Random Forest, AdaBoost, Multi-Layer Perceptron, XGBoost and CatBoost.
  • +
  • Models were ensembled using Simple and Weighted averaging.
  • +
+
+
+

Result analysis

+
    +
  • Predictions made on holdout test set
  • +
  • Models compared based on chosen metrics: RMSE and R2-Score.
  • +
  • Visualized predicted prices vs actual prices to analyze errors.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Training time & Model complexity vs Reducing error

+
    +
  • Solution: Limiting depth and number of estimators for tree-based models. Overfitting detection and early stopping mechanism for neural network training.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Error?};
+    B -->|Yes| C[Hmm...];
+    C --> D[Debug];
+    D --> B;
+    B ---->|No| E[Yay!];
+
+
+Data Exploration +
+
+
+

target_dist

+
+
+

featdist_year

+
+
+

featdist_kmdriven

+
+
+

featdist_engine

+
+
+

featdist_power

+
+
+

featdist_mileage

+
+
+

featdist_seats

+
+
+
+
+
+Feature Selection +
+
+
+

featselect_corrfeatures

+
+
+

featselect_corrtarget

+
+
+

featselect_mutualinfo

+
+
+
+
+
+

MODELS USED AND THEIR PERFORMANCE

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ModelRMSER2-Score
Linear Regression3.58030.7915
K-Nearest Neighbors2.82610.8701
Decision Tree2.67900.8833
Random Forest2.46190.9014
AdaBoost2.36290.9092
Multi-layer Perceptron2.62550.8879
XGBoost w/o preprocessing2.16490.9238
XGBoost with preprocessing2.09870.9284
CatBoost w/o preprocessing2.17340.9232
Simple average ensemble2.28040.9154
Weighted average ensemble2.12960.9262
+
+

CONCLUSION

+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  1. Features related to car configuration such as Power, Engine and Transmission are some of the most informative features. Usage-related features such as Year and current Mileage are also important.
  2. +
  3. Seating capacity and Number of previous owners had relatively less predictive power. However, none of the features were candidates for removal.
  4. +
+
+
+Improvements in understanding machine learning concepts +
    +
  1. Implemented target-encoding for high-cardinality categorical features.
  2. +
  3. Designed pipelines to avoid data leakage.
  4. +
  5. Ensembling models using prediction averaging.
  6. +
+
+
+Challenges faced and how they were overcome +
    +
  1. Handling mixed feature types in preprocessing pipelines.
  2. +
  3. Regularization and overfitting detection to reduce training time while maintaining performance.
  4. +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+
    +
  • Sellers can use the model to estimate an approximate price for their cars when they list them on the marketplace.
  • +
+
+
+
    +
  • Buyers can use the model to check if the listed price is fair when they decide to buy a used vehicle.
  • +
+
+
+
+
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+
+
+
+
    +
  • Complex model-ensembling through stacking or hill-climbing was not implemented due to significantly longer training time.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/natural-language-processing/chatbot-project-implementation/index.html b/projects/natural-language-processing/chatbot-project-implementation/index.html new file mode 100644 index 00000000..bb2e06d2 --- /dev/null +++ b/projects/natural-language-processing/chatbot-project-implementation/index.html @@ -0,0 +1,845 @@ + + + + + + + + + + + + + + + + + + + + + Chatbot Implementation Project - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+ +
+
+ + + +
+
+ + + + +

Chatbot Implementation Project

+

AIM

+

To develop a chatbot using Natural Language Processing (NLP) and a Naive Bayes classifier for intent classification. The chatbot takes user input, predicts the intent, and generates an appropriate response based on predefined intents and responses stored in a CSV file.

+ +

https://drive.google.com/file/d/1J7mGS16EkgCEtN7UJtBlJACeqoDbdS4F/view?usp=drive_link

+ +

https://colab.research.google.com/drive/1L2LKfbVv4pb4yzczcRnnU4AkEW-kCZSZ?usp=sharing

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • nltk
  • +
  • scikit-learn
  • +
  • numpy
  • +
  • pickle
  • +
+
+
+

DESCRIPTION

+
+What is the Requirement of the Project? +

-A chatbot is required to automate conversations and provide immediate responses to user queries. It can be used to answer FAQs, provide customer support, and improve user interaction.

+
+
+Why is it Necessary? +
    +
  • Chatbots are essential for improving user engagement and providing 24/7 service.
  • +
  • They automate responses, saving time and providing immediate help.
  • +
+
+
+How is it Beneficial and Used? +
    +
  • Chatbots can be used for:
  • +
  • Customer service automation.
  • +
  • Answering user questions.
  • +
  • Guiding users through processes on websites or apps.
  • +
+
+

"Initial Thoughts and Planning"

+
    +
  • Intents and Responses: Gathered and stored in CSV format.
  • +
  • Preprocessing: Tokenized and lemmatized text to prepare for model training.
  • +
  • Model: Built a Naive Bayes classifier to predict intents.
  • +
  • Deployment: Deployed the model to predict user queries and return appropriate responses.
  • +
+
+

Additional Resources Used

+ +
+

FEATURES IN THE DATASET

+ + + + + + + + + + + + + + + + + +
FeatureDescription
intentsUser query categories like greetings, farewells.
responsesPredefined chatbot responses for each intent.
+
+

STEPS AND IMPLEMENTATION

+
+
+
+

Data Preprocessing +- Loaded the intents from CSV files. +- Cleaned data by removing duplicates and handling null values.

+
+
+

Vectorization +- Used TfidfVectorizer to convert text into vectors. +- Split data into training and testing sets.

+
+
+

Model Training +- Trained a Naive Bayes classifier on the preprocessed data. +- Saved the model for future use with pickle. +- Created an intent-response mapping.

+
+
+

Prediction and Response Generation** +- The chatbot predicts the intent based on user input. +- Fetches and returns the appropriate response.

+
+
+

Testing +- Conducted live interaction tests with the chatbot.

+
+
+
+
+

Features Not Implemented Yet

+
    +
  • Integration of a deep learning model (e.g., RNN or LSTM) for better context handling.
  • +
+
+

Flow Chart

+

graph TD
+    A[Data Preprocessing] --> B[Vectorization]
+    B --> C[Model Training]
+    C --> D[Prediction and Response Generation]
+    D --> E[Testing the Chatbot]
+
+    A1[Load intents from CSV] --> A2[Clean data: remove duplicates and handle nulls]
+    A --> A1
+    A --> A2
+
+    B1[Use TfidfVectorizer to convert text into vectors] --> B2[Split data into training and testing sets]
+    B --> B1
+    B --> B2
+
+    C1[Train Naive Bayes classifier] --> C2[Save model with pickle] --> C3[Create intent-response mapping]
+    C --> C1
+    C --> C2
+    C --> C3
+
+    D1[Chatbot predicts intent] --> D2[Fetch appropriate response based on intent] --> D3[Return response to user]
+    D --> D1
+    D --> D2
+    D --> D3
+
+    E1[Live interaction with chatbot] --> E2[Test accuracy and responses]
+    E --> E1
+    E --> E2
+
#### Example Chatbot Interaction:
+
+```text
+You: Hello
+Bot: Hi, How can I assist you?
+

+
+

MODELS AND EVALUATION METRICS

+ + + + + + + + + + + + + + + + + +
ModelAccuracyPrecisionRecall
Naive Bayes92%91%90%
+
+

CONCLUSION

+
+What Have You Learned? +
    +
  • Building a chatbot using NLP techniques can automate interactions and provide user-friendly interfaces for businesses. + -The Naive Bayes classifier is an effective yet simple model for intent prediction.
  • +
+
+
+

USE CASES

+
+
+
+

Customer Support Automation

+

-Provide 24/7 automated support for customers.

+
+
+

FAQ Automation

+
    +
  • Automatically respond to frequently asked questions on websites or apps.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/natural-language-processing/email_spam_detection/index.html b/projects/natural-language-processing/email_spam_detection/index.html new file mode 100644 index 00000000..6a48f5cf --- /dev/null +++ b/projects/natural-language-processing/email_spam_detection/index.html @@ -0,0 +1,933 @@ + + + + + + + + + + + + + + + + + + + + + Email Spam Detection - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Email Spam Detection

+

AIM

+

To develop a machine learning-based system that classifies email content as spam or ham (not spam).

+ +

https://www.kaggle.com/datasets/ashfakyeafi/spam-email-classification

+ +

https://www.kaggle.com/code/inshak9/email-spam-detection

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn
  • +
  • matplotlib
  • +
  • seaborn
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • A robust system to detect spam emails is essential to combat increasing spam content.
  • +
  • It improves user experience by automatically filtering unwanted messages.
  • +
+
+
+Why is it necessary? +
    +
  • Spam emails consume resources, time, and may pose security risks like phishing.
  • +
  • Helps organizations and individuals streamline their email communication.
  • +
+
+
+How is it beneficial and used? +
    +
  • Provides a quick and automated solution for spam classification.
  • +
  • Used in email services, IT systems, and anti-spam software to filter messages.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Analyzed the dataset and prepared features.
  • +
  • Implemented various machine learning models for comparison.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • Documentation from scikit-learn
  • +
  • Blog: Introduction to Spam Classification with ML
  • +
+
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+

The dataset contains features like word frequency, capital letter counts, and others that help in distinguishing spam emails from ham.

+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureDescription
word_freq_xFrequency of specific words in the email body
capital_run_lengthLength of consecutive capital letters
char_freqFrequency of special characters like ; and $
is_spamTarget variable (1 = Spam, 0 = Ham)
+
+

WHAT I HAVE DONE

+
+
+
+

Initial data exploration and understanding: + - Loaded the dataset using pandas. + - Explored dataset features and target variable distribution.

+
+
+

Data cleaning and preprocessing: + - Checked for missing values. + - Standardized features using scaling techniques.

+
+
+

Feature engineering and selection: + - Extracted relevant features for spam classification. + - Used correlation matrix to select significant features.

+
+
+

Model training and evaluation: + - Trained models: KNN, Naive Bayes, SVM, and Random Forest. + - Evaluated models using accuracy, precision, and recall.

+
+
+

Model optimization and fine-tuning: + - Tuned hyperparameters using GridSearchCV.

+
+
+

Validation and testing: + - Tested models on unseen data to check performance.

+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+
    +
  • Accuracy vs. Training Time:
  • +
  • Models like Random Forest took longer to train but achieved higher accuracy compared to Naive Bayes.
  • +
+
+
+
    +
  • Complexity vs. Interpretability:
  • +
  • Simpler models like Naive Bayes were more interpretable but slightly less accurate.
  • +
+
+
+
+
+

SCREENSHOTS

+ + +
+

Project flowchart

+
  graph LR
+    A[Start] --> B[Load Dataset];
+    B --> C[Preprocessing];
+    C --> D[Train Models];
+    D --> E{Compare Performance};
+    E -->|Best Model| F[Deploy];
+    E -->|Retry| C;
+
+
+Confusion Matrix +
+
+
+

Confusion Matrix - SVM

+
+
+

Confusion Matrix - Naive Bayes

+
+
+

Confusion Matrix - Decision Tree

+
+
+

Confusion Matrix - AdaBoost

+
+
+

Confusion Matrix - Random Forest

+
+
+
+
+
+

MODELS USED AND THEIR EVALUATION METRICS

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ModelAccuracyPrecisionRecall
KNN90%89%88%
Naive Bayes92%91%90%
SVM94%93%91%
Random Forest95%94%93%
AdaBoost97%97%100%
+
+

MODELS COMPARISON GRAPHS

+
+

Models Comparison Graphs

+
+
+
+

Model accracy comparison

+
+
+
+
+
+

CONCLUSION

+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  • Feature importance significantly impacts spam detection.
  • +
  • Simple models like Naive Bayes can achieve competitive performance.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Gained hands-on experience with classification models and model evaluation techniques.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Balancing between accuracy and training time was challenging, solved using model tuning.
  • +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+

Email Service Providers +- Automated filtering of spam emails for improved user experience.

+
+
+

Enterprise Email Security +- Used in enterprise software to detect phishing and spam emails.

+
+
+
+
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+
+
+
+
    +
  • Integration of deep learning models (LSTM) for improved accuracy.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/natural-language-processing/index.html b/projects/natural-language-processing/index.html new file mode 100644 index 00000000..a3216088 --- /dev/null +++ b/projects/natural-language-processing/index.html @@ -0,0 +1,501 @@ + + + + + + + + + + + + + + + + + + + + + Natural Language Processing 🗣️ - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/natural-language-processing/name_entity_recognition/index.html b/projects/natural-language-processing/name_entity_recognition/index.html new file mode 100644 index 00000000..cb8115c4 --- /dev/null +++ b/projects/natural-language-processing/name_entity_recognition/index.html @@ -0,0 +1,795 @@ + + + + + + + + + + + + + + + + + + + + + Name Entity Recognition (NER) Project - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Name Entity Recognition (NER) Project

+

AIM

+

To develop a system that identifies and classifies named entities (such as persons, organizations, locations, dates, etc.) in text using Named Entity Recognition (NER) with SpaCy.

+ +

N/A (This project uses text input for NER analysis, not a specific dataset) +- It uses real time data as input .

+ +

[Note book link ] +(https://colab.research.google.com/drive/1pBIEFA4a9LzyZKUFQMCypQ22M6bDbXM3?usp=sharing)

+

LIBRARIES NEEDED

+
    +
  • SpaCy
  • +
+

DESCRIPTION

+
+

What is the requirement of the project?

+
+
    +
  • Named Entity Recognition (NER) is essential to automatically extract and classify key entities from text, such as persons, organizations, locations, and more.
  • +
  • This helps in analyzing and organizing data efficiently, enabling various NLP applications like document analysis and information retrieval.
  • +
+
+Why is it necessary? +
+
    +
  • NER is used for understanding and structuring unstructured text, which is widely applied in industries such as healthcare, finance, and e-commerce.
  • +
  • It allows users to extract actionable insights from large volumes of text data
  • +
+
+How is it beneficial and used? +
+
    +
  • NER plays a key role in tasks such as document summarization, information retrieval.
  • +
  • It automates the extraction of relevant entities, which reduces manual effort and improves efficiency.
  • +
+
+How did you start approaching this project? (Initial thoughts and planning) +
+
    +
  • The project leverages SpaCy's pre-trained NER models, enabling easy text analysis without the need for training custom models.
  • +
+

Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.)

+
    +
  • SpaCy Documentation: SpaCy NER
  • +
  • NLP in Python by Steven Bird et al.
  • +
+

EXPLANATION

+

DETAILS OF THE DIFFERENT ENTITY TYPES

+

The system extracts the following entity types:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Entity TypeDescription
PERSONNames of people (e.g., "Anuska")
ORGOrganizations (e.g., "Google", "Tesla")
LOCLocations (e.g., "New York", "Mount Everest")
DATEDates (e.g., "January 1st, 2025")
GPEGeopolitical entities (e.g., "India", "California")
+

WHAT I HAVE DONE

+

Step 1: Data collection and preparation

+
    +
  • Gathered sample text for analysis (provided by users in the app).
  • +
  • Explored the text structure and identified entity types.
  • +
+

Step 2: NER model implementation

+
    +
  • Integrated SpaCy's pre-trained NER model (en_core_web_sm).
  • +
  • Extracted named entities and visualized them with labels and color coding.
  • +
+

Step 3: Testing and validation

+
    +
  • Validated results with multiple test cases to ensure entity accuracy.
  • +
  • Allowed users to input custom text for NER analysis in real-time.
  • +
+

PROJECT TRADE-OFFS AND SOLUTIONS

+

Trade Off 1: Pre-trained model vs. custom model

+
    +
  • Pre-trained models provide quick results but may lack accuracy for domain-specific entities.
  • +
  • Custom models can improve accuracy but require additional data and training time.
  • +
+

Trade Off 2: Real-time analysis vs. batch processing

+
    +
  • Real-time analysis in a web app enhances user interaction but might slow down with large text inputs.
  • +
  • Batch processing could be more efficient for larger datasets.
  • +
+

SCREENSHOTS

+

NER Example

+

``` mermaid +graph LR + A[Start] --> B[Text Input]; + B --> C[NER Analysis]; + C --> D{Entities Extracted}; + D -->|Person| E[Anuska]; + D -->|Location| F[New York]; + D -->|Organization| G[Google]; + D -->|Date| H[January 1st, 2025];

+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/natural-language-processing/next-word-pred/index.html b/projects/natural-language-processing/next-word-pred/index.html new file mode 100644 index 00000000..5511e923 --- /dev/null +++ b/projects/natural-language-processing/next-word-pred/index.html @@ -0,0 +1,869 @@ + + + + + + + + + + + + + + + + + + + + + Next Word Prediction using LSTM - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Next Word Prediction using LSTM

+

AIM

+

To predict the next word using LSTM.

+ +

Dataset

+ +

Code

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn
  • +
  • matplotlib
  • +
  • seaborn
  • +
  • tensorflow
  • +
  • keras
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • To create an intelligent system capable of predicting the next word in a sentence based on its context.
  • +
  • The need for such a system arises in applications like autocomplete, chatbots, and virtual assistants.
  • +
+
+
+Why is it necessary? +
    +
  • Enhances user experience in text-based applications by offering accurate suggestions.
  • +
  • Reduces typing effort, especially in mobile applications.
  • +
+
+
+How is it beneficial and used? +
    +
  • Improves productivity: By predicting words, users can complete sentences faster.
  • +
  • Supports accessibility: Assists individuals with disabilities in typing.
  • +
  • Boosts efficiency: Helps in real-time text generation in NLP applications like chatbots and email composition.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Studied LSTM architecture and its suitability for sequential data.
  • +
  • Explored similar projects and research papers to understand data preprocessing techniques.
  • +
  • Experimented with tokenization, padding, and sequence generation for the dataset.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). +
    +
  • Blogs on LSTM from Towards Data Science.
  • +
  • TensorFlow and Keras official documentation.
  • +
+
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+
+

PROJECT WORKFLOW

+
+
+
+

Initial data exploration and understanding:

+
    +
  • Gathered text data from open-source datasets.
  • +
  • Analyzed the structure of the data.
  • +
  • Performed basic text statistics to understand word frequency and distribution.
  • +
+
+
+

Data cleaning and preprocessing

+
    +
  • Removed punctuation and convert text to lowercase.
  • +
  • Tokenized text into sequences and pad them to uniform length.
  • +
+
+
+

Feature engineering and selection

+
    +
  • Created input-output pairs for next-word prediction using sliding window techniques on tokenized sequences.
  • +
+
+
+

Model training and evaluation:

+
    +
  • Used an embedding layer to represent words in a dense vector space.
  • +
  • Implemented LSTM-based sequential models to learn context and dependencies in text.
  • +
  • Experimented with hyperparameters like sequence length, LSTM units, learning rate, and batch size.
  • +
+
+
+

Model optimization and fine-tuning

+
    +
  • Adjusted hyperparameters like embedding size, LSTM units, and learning rate.
  • +
+
+
+

Validation and testing

+
    +
  • Used metrics like accuracy and perplexity to assess prediction quality.
  • +
  • Validated the model on unseen data to test generalization.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Accuracy vs Training Time:

+
    +
  • Solution: Balanced by reducing the model's complexity and using an efficient optimizer.
  • +
+
+
+

Model complexity vs. Overfitting:

+
    +
  • Solution: Implemented dropout layers and monitored validation loss during training.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Data Preprocessed?};
+    B -->|No| C[Clean and Tokenize];
+    C --> D[Create Sequences];
+    D --> B;
+    B -->|Yes| E[Model Designed?];
+    E -->|No| F[Build LSTM/Transformer];
+    F --> E;
+    E -->|Yes| G[Train Model];
+    G --> H{Performant?};
+    H -->|No| I[Optimize Hyperparameters];
+    I --> G;
+    H -->|Yes| J[Deploy Model];
+    J --> K[End];
+
+
+

MODELS USED AND THEIR EVALUATION METRICS

+ + + + + + + + + + + + + + + + + +
ModelAccuracyMSER2 Score
LSTM72%--
+
+

MODELS COMPARISON GRAPHS

+
+

Models Comparison Graphs

+
+
+
+

model perf

+
+
+
+
+
+

CONCLUSION

+

KEY LEARNINGS

+
+

Insights gained from the data

+
    +
  • The importance of preprocessing for NLP tasks.
  • +
  • How padding and embeddings improve the model’s ability to generalize.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Learned how LSTMs handle sequential dependencies.
  • +
  • Understood the role of softmax activation in predicting word probabilities.
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Challenge: Large vocabulary size causing high memory usage.
  • +
  • Solution: Limited vocabulary to the top frequent words.
  • +
+
+
+

USE CASES

+
+
+
+

Text Autocompletion

+
    +
  • Used in applications like Gmail and search engines to enhance typing speed.
  • +
+
+
+

Virtual Assistants

+
    +
  • Enables better conversational capabilities in chatbots and AI assistants.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/natural-language-processing/twitter_sentiment_analysis/index.html b/projects/natural-language-processing/twitter_sentiment_analysis/index.html new file mode 100644 index 00000000..37449b20 --- /dev/null +++ b/projects/natural-language-processing/twitter_sentiment_analysis/index.html @@ -0,0 +1,931 @@ + + + + + + + + + + + + + + + + + + + + + Twitter Sentiment Analysis - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Twitter Sentiment Analysis

+

AIM

+

To analyze sentiment in Twitter data using natural language processing techniques.

+ +

https://www.kaggle.com/datasets/kazanova/sentiment140

+ +

https://drive.google.com/drive/folders/1F6BLxvp6qIAgGZOZ2rC370EmKhj5W1FC?usp=sharing

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • pandas
  • +
  • numpy
  • +
  • scikit-learn
  • +
  • seaborn
  • +
  • matplotlib
  • +
  • tensorflow
  • +
  • keras
  • +
  • nltk
  • +
  • multiprocessing
  • +
  • tqdm
  • +
  • os
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • The project aims to perform sentiment analysis on Twitter data.
  • +
  • This involves extracting tweets related to specific topics or keywords, processing these tweets using natural language processing (NLP) techniques to determine the sentiment (positive or negative), and presenting insights derived from the analysis.
  • +
+
+
+Why is it necessary? +
    +
  • Twitter is a rich source of real-time public opinion and sentiment. Analyzing tweets can provide valuable insights into public perception of events, products, brands, or topics of interest.
  • +
  • This information is crucial for businesses, governments, and researchers to make informed decisions, understand public sentiment trends, and gauge the success of marketing campaigns or policy changes.
  • +
+
+
+How is it beneficial and used? +
    +
  • Business Insights: Companies can understand customer feedback and sentiments towards their products or services.
  • +
  • Brand Management: Monitor brand sentiment and respond to customer concerns or issues in real-time.
  • +
  • Market Research: Identify trends and sentiments related to specific topics or industries.
  • +
  • Social Listening: Understand public opinion on current events, policies, or social issues.
  • +
  • Customer Service Improvement: Improve customer service by analyzing sentiment towards customer interactions.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • +

    Choose appropriate NLP techniques for sentiment analysis, such as:

    +
      +
    • Bag-of-Words (BoW) and TF-IDF: Represent tweets as numerical vectors.
    • +
    • Sentiment Lexicons: Use dictionaries of words annotated with sentiment scores (e.g., Vader sentiment lexicon).
    • +
    • Machine Learning Models: Train supervised classifiers (e.g., Naive Bayes, SVM, or neural networks) on labeled data for sentiment prediction.
    • +
    +
  • +
  • +

    Model Evaluation: Evaluate the performance of the sentiment analysis model using metrics like accuracy. Cross-validation techniques can be used to ensure robustness.

    +
  • +
  • +

    Visualization and Insights: Visualize sentiment trends over time or across different categories using charts (e.g., line plots, bar charts). Generate insights based on the analysis results.

    +
  • +
  • +

    Deployment: Deploy the sentiment analysis system as a standalone application or integrate it into existing systems for real-time monitoring and analysis.

    +
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.) + +
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+
+

WHAT I HAVE DONE

+
+
+
+

Initial data exploration and understanding:

+
    +
  • Gathered Twitter data using pre-existing datasets (Kaggle).
  • +
  • Understand the structure of the data (e.g., tweet text, metadata like timestamps, user information).
  • +
  • Explore basic statistics and distributions of data features.
  • +
+
+
+

Data cleaning and preprocessing:

+
    +
  • Remove or handle noisy data such as URLs, special characters, and emojis.
  • +
  • Tokenize tweets into individual words or tokens.
  • +
  • Remove stopwords (commonly used words that do not carry significant meaning).
  • +
  • Normalize text through techniques like stemming to reduce variations of words.
  • +
+
+
+

Feature engineering and selection:

+
    +
  • Convert text data into numerical representations suitable for machine learning models (e.g., Bag-of-Words, TF-IDF).
  • +
  • Select relevant features that contribute most to the sentiment analysis task.
  • +
+
+
+

Model training and evaluation:

+
    +
  • Split the dataset into training and testing sets.
  • +
  • Choose appropriate machine learning models (e.g., Naive Bayes, RNN LSTM, logistic regression) for sentiment analysis.
  • +
  • Train the models on the training data and evaluate their performance using metrics like accuracy.
  • +
+
+
+

Model optimization and fine-tuning:

+
    +
  • Fine-tune the hyperparameters of the selected models to improve performance.
  • +
  • Consider techniques like grid search or random search to find optimal parameters.
  • +
  • Experiment with different models or combinations of models to achieve better results.
  • +
+
+
+

Validation and testing:

+
    +
  • Validate the trained models on a separate validation set to ensure generalizability.
  • +
  • Test the final model on unseen data (testing set or new tweets) to assess its performance in real-world scenarios.
  • +
  • Iterate on the model and preprocessing steps based on validation results to improve accuracy and robustness.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Stemming process took a lot of computational time to process over 1.6 million datapoints.

+
    +
  • Solution: Divided the data into batches and applied parallel processing.
  • +
+
+
+

In RNN based LSTM, overfitting problem occurred.

+
    +
  • Solution: Tried to fix it using Dropout layer, early stopping criteria.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project structure or tree diagram

+
  graph LR
+  A[Start] --> B{Error?};
+  B -->|Yes| C[Hmm...];
+  C --> D[Debug];
+  D --> B;
+  B ---->|No| E[Yay!];
+
+
+Visualizations and EDA of different features +
+
+
+

sentiment_distribution

+
+
+
+
+
+Model performance graphs +
+
+
+

confusion_matrix_logistic_regression

+
+
+

roc_curve_logistic_regression

+
+
+

confusion_matrix_naive_bayes

+
+
+

roc_curve_naive_bayes

+
+
+
+
+
+

MODELS USED AND THEIR EVALUATION METRICS

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ModelAccuracyMSER2 Score
Logistic Regression77%0.15317247039458240.3873101184216704
Naive Bayes75%0.174767737908748970.3009290483650041
RNN LSTM77.84%--
+
+

MODELS COMPARISON GRAPHS

+
+

Models Comparison Graphs

+
+
+
+

lstm_accuracy

+
+
+

lstm_loss

+
+
+
+
+
+

CONCLUSION

+

KEY LEARNINGS

+
+

Insights gained from the data

+
    +
  • Data Variety: Twitter data provides a rich source of real-time, diverse opinions and sentiments.
  • +
  • Text Preprocessing: Importance of cleaning and preprocessing text data (e.g., removing stopwords, stemming/lemmatization) for better analysis.
  • +
  • Feature Extraction: Techniques like TF-IDF (Term Frequency-Inverse Document Frequency) and word embeddings (e.g., Word2Vec, GloVe) to represent text numerically for machine learning models.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Model Selection: Experimenting with various algorithms to find the most suitable for sentiment classification (e.g., logistic regression, naive bayes, neural networks).
  • +
+
+
+Challenges faced and how they were overcome +
    +
  • Noise in Data: Dealing with noise from hashtags, emojis, and slang in tweets through effective preprocessing techniques.
  • +
  • Computational Resources: Managing large volumes of data and resource-intensive computations by optimizing code and leveraging cloud computing platforms if necessary.
  • +
+
+
+

USE CASES

+
+
+
+

Brand Monitoring and Customer Feedback Analysis

+
    +
  • This application allows businesses to leverage Twitter sentiment analysis as a valuable tool for customer relationship management, brand reputation management, and strategic decision-making based on real-time customer feedback and sentiment analysis.
  • +
+
+
+

Financial Market Analysis and Investment Decisions

+
    +
  • This application showcases how Twitter sentiment analysis can be leveraged in the financial sector to gain competitive advantages, improve investment strategies, and manage risks effectively based on public sentiment towards financial markets and specific stocks.
  • +
+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/statistics/bangladesh-premier-league-analysis/index.html b/projects/statistics/bangladesh-premier-league-analysis/index.html new file mode 100644 index 00000000..bd2867f6 --- /dev/null +++ b/projects/statistics/bangladesh-premier-league-analysis/index.html @@ -0,0 +1,1194 @@ + + + + + + + + + + + + + + + + + + + + + Bangladesh Premier League Analysis - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + + + + + +
+
+ + + + +

Bangladesh Premier League Analysis

+
+ +
+ +

AIM

+

The main goal of the project is to analyze the performance of the bangladesh players in their premier league and obtaining the top 5 players in all of them in different fields like bowling, batting, toss_winner, highest runner, man of the match, etc.

+ +

https://www.kaggle.com/abdunnoor11/bpl-data

+ +

https://colab.research.google.com/drive/1equud2jwKnmE1qbbTJLsi2BbjuA7B1Si?usp=sharing

+

LIBRARIES NEEDED

+
+LIBRARIES USED +
    +
  • matplotlib
  • +
  • pandas
  • +
  • sklearn
  • +
  • seaborn
  • +
  • numpy
  • +
  • scipy
  • +
  • xgboost
  • +
  • Tensorflow
  • +
  • Keras
  • +
+
+
+

DESCRIPTION

+
+

What is the requirement of the project?

+
    +
  • This project aims to analyze player performance data from the Bangladesh Premier League (BPL) to classify players into categories such as best, good, average, and poor based on their performance.
  • +
  • The analysis provides valuable insights for players and coaches, highlighting who needs more training and who requires less, which can aid in strategic planning for future matches.
  • +
+
+
+Why is it necessary? +
    +
  • Analyzing player performance helps in understanding strengths and weaknesses, which can significantly reduce the chances of losing and increase the chances of winning future matches.
  • +
  • It aids in making informed decisions about team selection and match strategies.
  • +
+
+
+How is it beneficial and used? +
    +
  • For Players: Provides feedback on their performance, helping them to improve specific aspects of their game.
  • +
  • For Coaches: Helps in identifying areas where players need improvement, which can be focused on during training sessions.
  • +
  • For Team Management: Assists in strategic decision-making regarding player selection and match planning.
  • +
  • For Fans and Analysts: Offers insights into player performances and trends over the league, enhancing the understanding and enjoyment of the game.
  • +
+
+
+How did you start approaching this project? (Initial thoughts and planning) +
    +
  • Perform initial data exploration to understand the structure and contents of the dataset.
  • +
  • To learn about the topic and searching the related content like what is league, About bangladesh league, their players and much more.
  • +
  • Learn about the features in details by searching on the google or quora.
  • +
+
+
+Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.). + +
+
+

EXPLANATION

+

DETAILS OF THE DIFFERENT FEATURES

+
There are 3 different types of the datasets.
+
+- Batsman Dataset
+- Bowler Dataset
+- BPL (Bangladesh Premier League) Dataset
+
+
    +
  • There are 12 features in Batsman Dataset
  • +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescription
idAll matches unique id
seasonSeason
match_noNumber of matches
dateDate of Play
player_namePlayer Name
commentHow did the batsman get out?
RBatsman's run
BHow many balls faced the batsman?
MHow long their innings was in minutes?
foursFours
sixsSixes
SRStrike rate
+
    +
  • There are 12 features in Bowler Dataset
  • +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescription
idAll matches unique id
seasonSeason
match_noNumber of matches
dateDate of Play
player_namePlayer Name
OOvers
Mmiddle overs
RRuns
WWickets
ECONThe average number of runs they have conceded per over bowled
WDWide balls
NBNo balls
+
    +
  • There are 19 features in BPL Dataset
  • +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Feature NameDescription
idAll matches unique id
seasonSeason
match_noNumber of matches
dateDate of Play
team_1First Team
team_1_scoreFirst Team Score
team_2Second Team
team_2_scoreSecond Team Score
player_of_matchWhich team won the toss?
toss_winnerWhich team won the toss?
toss_decisionToss winner team decision
winnerMatch Winner
venueVenue
cityCity
win_by_wicketsWin by wickets.
win_by_runsWin by runs
resultResult of the winner
umpire_1First Umpire Name
umpire_2Second Umpire Name
+
+

WHAT I HAVE DONE

+
+
+
+
    +
  • Performed Exploratory Data Analysis on data.
  • +
+
+
+
    +
  • Created data visualisations to understand the data in a better way.
  • +
+
+
+
    +
  • Found strong relationships between independent features and dependent feature using correlation.
  • +
+
+
+
    +
  • Handled missing values using strong correlations,dropping unnecessary ones.
  • +
+
+
+
    +
  • Used different Regression techniques like Linear Regression,Ridge Regression,Lasso Regression and deep neural networks to predict the dependent feature in most suitable manner.
  • +
+
+
+
    +
  • Compared various models and used best performance model to make predictions.
  • +
+
+
+
    +
  • Used Mean Squared Error and R2 Score for evaluating model's performance.
  • +
+
+
+
    +
  • Visualized best model's performance using matplotlib and seaborn library.
  • +
+
+
+
+
+

PROJECT TRADE-OFFS AND SOLUTIONS

+
+
+
+

Handling missing and inconsistent data entries.

+
    +
  • Solution
  • +
  • Data Imputation: For missing numerical values, I used techniques such as mean, median, or mode imputation based on the distribution of the data.
  • +
  • Data Cleaning: For inconsistent entries, I standardized the data by removing duplicates, correcting typos, and ensuring uniform formatting.
  • +
  • Dropping Irrelevant Data: In cases where the missing data was extensive and could not be reliably imputed, I decided to drop those rows/columns to maintain data integrity.
  • +
+
+
+

Extracting target variables from the dataset.

+
    +
  • Solution
  • +
  • Feature Engineering: Created new features that could serve as target variables, such as aggregating player statistics to determine top performers.
  • +
  • Domain Knowledge: Utilized cricket domain knowledge to identify relevant metrics (e.g., strike rate, economy rate) and used them to define target variables.
  • +
  • Label Encoding: For categorical target variables (e.g., player categories like best, good, average, poor), I used label encoding techniques to convert them into numerical format for analysis.
  • +
+
+
+

Creating clear and informative visualizations that effectively communicate the findings.

+
    +
  • Solution
  • +
  • Tool Selection: Used powerful visualization tools like Matplotlib and Seaborn in Python, which provide a wide range of customization options.
  • +
  • Visualization Best Practices: Followed best practices such as using appropriate chart types (e.g., bar charts for categorical data, scatter plots for correlations), adding labels and titles, and ensuring readability.
  • +
  • Iterative Refinement: Iteratively refined visualizations based on feedback and self-review to enhance clarity and informativeness.
  • +
+
+
+

Correctly interpreting the results to provide actionable insights.

+
    +
  • Solution
  • +
  • Cross-validation: Used cross-validation techniques to ensure the reliability and accuracy of the analysis results.
  • +
  • Collaboration with Experts: Engaged with cricket experts and enthusiasts to validate the findings and gain additional perspectives.
  • +
  • Contextual Understanding: Interpreted results within the context of the game, considering factors such as player roles, match conditions, and historical performance to provide meaningful and actionable insights.
  • +
+
+
+
+
+

SCREENSHOTS

+
+

Project workflow

+
  graph LR
+    A[Start] --> B{Error?};
+    B -->|Yes| C[Hmm...];
+    C --> D[Debug];
+    D --> B;
+    B ---->|No| E[Yay!];
+
+
+Visualizations and EDA of different features +
+
+
+

top-5-_player-of-_match

+
+
+

top-5-_batsman-_runners

+
+
+

top-5-_four-_runners

+
+
+

top-5-_overs

+
+
+

top-5-_runs

+
+
+

top-5-_umpires

+
+
+

top-5-_wickets

+
+
+

toss-_winners

+
+
+
+
+
+

MODELS USED AND THEIR ACCURACIES

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ModelMSER2
Random Forest Regression19.3559840.371316
Gradient Boosting Regression19.4204940.369221
XG Boost Regression21.3491680.306577
Ridge Regression26.8139810.129080
Linear Regression26.9168880.125737
Deep Neural Network27.7582160.098411
Decision Tree Regression29.0445330.056631
+
+

MODELS COMPARISON GRAPHS

+
+

Models Comparison Graphs

+
+
+
+

r_f_regression_plot

+
+
+

conclusion-_graph

+
+
+
+
+
+

CONCLUSION

+
We can see that R2 Score and Mean Absolute Error is best for Random Forest Regression.
+By Using Neural network, We cannot get the minimum Mean Squared Error value possible.
+Random Forest Regression model can predict most accurate results for predicting bangladesh premier league winning team which is the highest model performance in comparison with other Models.
+
+

WHAT YOU HAVE LEARNED

+
+

Insights gained from the data

+
    +
  • Identified key performance indicators for players in the Bangladesh Premier League, such as top scorers, best bowlers, and players with the most man of the match awards.
  • +
  • Discovered trends and patterns in player performances that could inform future strategies and training programs.
  • +
  • Gained a deeper understanding of the distribution of player performances across different matches and seasons.
  • +
+
+
+Improvements in understanding machine learning concepts +
    +
  • Enhanced knowledge of data cleaning and preprocessing techniques to handle real-world datasets.
  • +
  • Improved skills in exploratory data analysis (EDA) to extract meaningful insights from raw data.
  • +
  • Learned how to use visualization tools to effectively communicate data-driven findings.
  • +
+
+
+

USE CASES OF THIS MODEL

+
+
+
+

Team Selection and Strategy Planning

+
    +
  • Coaches and team managers can use the model to analyze player performance data and make informed decisions about team selection and match strategies. By identifying top performers and areas for improvement, the model can help optimize team composition and tactics for future matches.
  • +
+
+
+

Player Performance Monitoring and Training

+
    +
  • The model can be used to track player performance over time and identify trends in their performance. This information can be used by coaches to tailor training programs to address specific weaknesses and enhance overall player development. By monitoring performance metrics, the model can help ensure that players are continuously improving.
  • +
+
+
+
+
+

FEATURES PLANNED BUT NOT IMPLEMENTED

+
+
+
+

Real-time Performance Tracking

+
    +
  • Implementing a real-time tracking system to update player performance metrics during live matches.
  • +
+
+
+

Advanced Predictive Analytics +- Using advanced machine learning algorithms to predict future player performances and match outcomes.

+
+
+
+ + + + + + + + + + + + + +
+
+ + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/projects/statistics/index.html b/projects/statistics/index.html new file mode 100644 index 00000000..e1f420f8 --- /dev/null +++ b/projects/statistics/index.html @@ -0,0 +1,503 @@ + + + + + + + + + + + + + + + + + + + + + Statistics 📃 - AI Code + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + +
+ +
+ + + + + + +
+
+ + + +
+
+
+ + + + + +
+
+
+ + + +
+
+
+ + + +
+
+
+ + + + + + + +
+ +
+ + + +
+
+
+
+ + + + + + + + + + + + + + \ No newline at end of file diff --git a/search/search_index.json b/search/search_index.json new file mode 100644 index 00000000..78a3b23b --- /dev/null +++ b/search/search_index.json @@ -0,0 +1 @@ +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Welcome to AI Code \ud83d\udc4b","text":"

AI-Code is an open-source project designed to help individuals learn and understand foundational code implementations of various AI algorithms, providing structured guides, resources, and hands-on projects across multiple AI domains like ML, DL, NLP, and GANs.

"},{"location":"#overview","title":"\ud83c\udf1f Overview","text":"

AI-Code simplifies learning AI technologies with easy-to-follow code and real-world project guides for ML, DL, GAN, NLP, OpenCV, and more.

"},{"location":"#core-features","title":"\ud83d\udd11 Core Features","text":"
  • Scratch-level implementations of AI algorithms \ud83e\udde0
  • Guides, datasets, research papers, and step-by-step tutorials \ud83d\udcd8
  • Clear directories with focused README files \ud83d\udcc2
  • Fast learning with minimal complexity \ud83d\ude80
"},{"location":"#tech-stack","title":"\ud83d\udee0\ufe0f Tech Stack","text":"
  • Python 3.9+
  • Mk Docs (A Python Package)
  • Markdown
  • Git/GitHub
  • VS Code
"},{"location":"algorithm-readme-template/","title":"\ud83e\uddee Algorithm Template","text":""},{"location":"algorithm-readme-template/#algorithm-title","title":"\ud83e\uddee Algorithm Title","text":""},{"location":"algorithm-readme-template/#objective","title":"\ud83c\udfaf Objective","text":"
  • Example: \"This is a K-Nearest Neighbors (KNN) classifier algorithm used for classifying data points based on their proximity to other points in the dataset.\"
"},{"location":"algorithm-readme-template/#prerequisites","title":"\ud83d\udcda Prerequisites","text":"
  • Linear Algebra Basics
  • Probability and Statistics
  • Libraries: NumPy, TensorFlow, PyTorch (as applicable)
"},{"location":"algorithm-readme-template/#inputs","title":"\ud83e\udde9 Inputs","text":"
  • Example: The input dataset should be in CSV format with features and labels for supervised learning algorithms.
"},{"location":"algorithm-readme-template/#outputs","title":"\ud83d\udce4 Outputs","text":"
  • Example: The algorithm returns a predicted class label or a regression value for each input sample.
"},{"location":"algorithm-readme-template/#algorithm-architecture","title":"\ud83c\udfdb\ufe0f Algorithm Architecture","text":"
  • Example: \"The neural network consists of 3 layers: an input layer, one hidden layer with 128 units, and an output layer with 10 units for classification.\"
"},{"location":"algorithm-readme-template/#training-process","title":"\ud83c\udfcb\ufe0f\u200d\u2642\ufe0f Training Process","text":"
  • Example:
    • The model is trained using the gradient descent optimizer.
    • Learning rate: 0.01
    • Batch size: 32
    • Number of epochs: 50
    • Validation set: 20% of the training data
"},{"location":"algorithm-readme-template/#evaluation-metrics","title":"\ud83d\udcca Evaluation Metrics","text":"
  • Example: \"Accuracy and F1-Score are used to evaluate the classification performance of the model. Cross-validation is used to reduce overfitting.\"
"},{"location":"algorithm-readme-template/#code-implementation","title":"\ud83d\udcbb Code Implementation","text":"
# Example: Bayesian Regression implementation\n\nimport numpy as np\nfrom sklearn.linear_model import BayesianRidge\nimport matplotlib.pyplot as plt\n\n# Generate Synthetic Data\nnp.random.seed(42)\nX = np.random.rand(20, 1) * 10\ny = 3 * X.squeeze() + np.random.randn(20) * 2\n\n# Initialize and Train Bayesian Ridge Regression\nmodel = BayesianRidge(alpha_1=1e-6, lambda_1=1e-6, compute_score=True)\nmodel.fit(X, y)\n\n# Make Predictions\nX_test = np.linspace(0, 10, 100).reshape(-1, 1)\ny_pred, y_std = model.predict(X_test, return_std=True)\n\n# Display Results\nprint(\"Coefficients:\", model.coef_)\nprint(\"Intercept:\", model.intercept_)\n\n# Visualization\nplt.figure(figsize=(8, 5))\nplt.scatter(X, y, color=\"blue\", label=\"Training Data\")\nplt.plot(X_test, y_pred, color=\"red\", label=\"Mean Prediction\")\nplt.fill_between(\n    X_test.squeeze(),\n    y_pred - y_std,\n    y_pred + y_std,\n    color=\"orange\",\n    alpha=0.3,\n    label=\"Predictive Uncertainty\",\n)\nplt.title(\"Bayesian Regression with Predictive Uncertainty\")\nplt.xlabel(\"X\")\nplt.ylabel(\"y\")\nplt.legend()\nplt.show()\n
"},{"location":"algorithm-readme-template/#scratch-code-explanation","title":"\ud83d\udd0d Scratch Code Explanation","text":"

Bayesian Regression is a probabilistic approach to linear regression that incorporates prior beliefs and updates these beliefs based on observed data to form posterior distributions of the model parameters. Below is a breakdown of the implementation, structured for clarity and understanding.

"},{"location":"algorithm-readme-template/#1-class-constructor-initialization","title":"1. Class Constructor: Initialization","text":"
class BayesianRegression:\n    def __init__(self, alpha=1, beta=1):\n        \"\"\"\n        Constructor for the BayesianRegression class.\n\n        Parameters:\n        - alpha: Prior precision (controls the weight of the prior belief).\n        - beta: Noise precision (inverse of noise variance in the data).\n        \"\"\"\n        self.alpha = alpha\n        self.beta = beta\n        self.w_mean = None\n        self.w_precision = None\n
  • Key Idea
    • The alpha (Prior precision, representing our belief in the model parameters' variability) and beta (Precision of the noise in the data) hyperparameters are crucial to controlling the Bayesian framework. A higher alpha means stronger prior belief in smaller weights, while beta controls the confidence in the noise level of the observations.
    • w_mean - Posterior mean of weights (initialized as None)
    • w_precision - Posterior precision matrix (initialized as None)
"},{"location":"algorithm-readme-template/#2-fitting-the-model-bayesian-learning","title":"2. Fitting the Model: Bayesian Learning","text":"
def fit(self, X, y):\n    \"\"\"\n    Fit the Bayesian Regression model to the input data.\n\n    Parameters:\n    - X: Input features (numpy array of shape [n_samples, n_features]).\n    - y: Target values (numpy array of shape [n_samples]).\n    \"\"\"\n    # Add a bias term to X for intercept handling.\n    X = np.c_[np.ones(X.shape[0]), X]\n\n    # Compute the posterior precision matrix.\n    self.w_precision = (\n        self.alpha * np.eye(X.shape[1])  # Prior contribution.\n        + self.beta * X.T @ X  # Data contribution.\n    )\n\n    # Compute the posterior mean of the weights.\n    self.w_mean = np.linalg.solve(self.w_precision, self.beta * X.T @ y)\n

Key Steps in the Fitting Process

  1. Add Bias Term: The bias term (column of ones) is added to X to account for the intercept in the linear model.
  2. Posterior Precision Matrix: $$ \\mathbf{S}_w^{-1} = \\alpha \\mathbf{I} + \\beta \\mathbf{X}^\\top \\mathbf{X} $$

    • The prior contributes \\(\\alpha \\mathbf{I}\\), which regularizes the weights.
    • The likelihood contributes \\(\\beta \\mathbf{X}^\\top \\mathbf{X}\\), based on the observed data.
  3. Posterior Mean of Weights: $$ \\mathbf{m}_w = \\mathbf{S}_w \\beta \\mathbf{X}^\\top \\mathbf{y} $$

    • This reflects the most probable weights under the posterior distribution, balancing prior beliefs and observed data.
"},{"location":"algorithm-readme-template/#3-making-predictions-posterior-inference","title":"3. Making Predictions: Posterior Inference","text":"
def predict(self, X):\n    \"\"\"\n    Make predictions on new data.\n\n    Parameters:\n    - X: Input features for prediction (numpy array of shape [n_samples, n_features]).\n\n    Returns:\n    - Predicted values (numpy array of shape [n_samples]).\n    \"\"\"\n    # Add a bias term to X for intercept handling.\n    X = np.c_[np.ones(X.shape[0]), X]\n\n    # Compute the mean of the predictions using the posterior mean of weights.\n    y_pred = X @ self.w_mean\n\n    return y_pred\n

Key Prediction Details

  1. Adding Bias Term: The bias term ensures that predictions account for the intercept term in the model.
  2. Posterior Predictive Mean: $$ \\hat{\\mathbf{y}} = \\mathbf{X} \\mathbf{m}_w $$
    • This computes the expected value of the targets using the posterior mean of the weights.
"},{"location":"algorithm-readme-template/#4-code-walkthrough","title":"4. Code Walkthrough","text":"
  • Posterior Precision Matrix (\\(\\mathbf{S}_w^{-1}\\)): Balances the prior (\\(\\alpha \\mathbf{I}\\)) and the data (\\(\\beta \\mathbf{X}^\\top \\mathbf{X}\\)) to regularize and incorporate observed evidence.
  • Posterior Mean (\\(\\mathbf{m}_w\\)): Encodes the most likely parameter values given the data and prior.
  • Prediction (\\(\\hat{\\mathbf{y}}\\)): Uses the posterior mean to infer new outputs, accounting for both prior knowledge and learned data trends.
"},{"location":"algorithm-readme-template/#example-usage","title":"\ud83d\udee0\ufe0f Example Usage","text":"
# Example Data\nX = np.array([[1.0], [2.0], [3.0]])  # Features\ny = np.array([2.0, 4.0, 6.0])        # Targets\n\n# Initialize and Train Model\nmodel = BayesianRegression(alpha=1.0, beta=1.0)\nmodel.fit(X, y)\n\n# Predict on New Data\nX_new = np.array([[4.0], [5.0]])\ny_pred = model.predict(X_new)\n\nprint(f\"Predictions: {y_pred}\")\n
  • Explanation
    • A small dataset is provided where the relationship between \\(X\\) and \\(y\\) is linear.
    • The model fits this data by learning posterior distributions of the weights.
    • Predictions are made for new inputs using the learned posterior mean.
"},{"location":"algorithm-readme-template/#advantages","title":"\ud83c\udf1f Advantages","text":"
  • Encodes uncertainty explicitly, providing confidence intervals for predictions.
  • Regularization is naturally incorporated through prior distributions.
  • Handles small datasets effectively by leveraging prior knowledge.
"},{"location":"algorithm-readme-template/#limitations","title":"\u26a0\ufe0f Limitations","text":"
  • Computationally intensive for high-dimensional data due to matrix inversions.
  • Sensitive to prior hyperparameters (\\(\\alpha, \\beta\\)).
"},{"location":"algorithm-readme-template/#application","title":"\ud83d\ude80 Application","text":"Application 1Application 2

Explain your application

Explain your application

"},{"location":"contribute/","title":"\ud83d\udcdd How to Contribute?","text":"

Welcome to the AI-Code project! Whether you're a seasoned developer or just starting, this guide will help you contribute systematically and effectively. Let's build amazing AI projects together! \ud83d\ude80

"},{"location":"contribute/#getting-started","title":"Getting Started","text":""},{"location":"contribute/#star-this-repository","title":"\ud83c\udf1f Star This Repository","text":"

Show your support by starring the project! \ud83c\udf1f This helps others discover and contribute. Click here to star.

"},{"location":"contribute/#fork-the-repository","title":"\ud83c\udf74 Fork the Repository","text":"

Create a personal copy of the repository by clicking the Fork button at the top right corner of the GitHub page.

"},{"location":"contribute/#clone-your-forked-repository","title":"\ud83d\udce5 Clone Your Forked Repository","text":"

Clone your forked repository to your local machine using:

git clone https://github.com/<your-github-username>/AI-Code.git\n
"},{"location":"contribute/#navigate-to-the-project-directory","title":"\ud83d\udcc2 Navigate to the Project Directory","text":"

Move into the directory where you've cloned the project:

cd AI-Code\n
"},{"location":"contribute/#create-a-new-branch","title":"\ud83c\udf31 Create a New Branch","text":"

Create a separate branch for your changes to keep the main branch clean:

git checkout -b <your_branch_name>\n
"},{"location":"contribute/#set-up-the-development-environment","title":"\ud83d\udee0\ufe0f Set Up the Development Environment","text":""},{"location":"contribute/#1-create-a-virtual-environment","title":"1. Create a Virtual Environment","text":"

To isolate dependencies, create a virtual environment:

python -m venv myenv\n
"},{"location":"contribute/#2-activate-the-virtual-environment","title":"2. Activate the Virtual Environment","text":"
  • Windows:
    myenv\\Scripts\\activate\n
  • macOS/Linux:
    source myenv/bin/activate\n
"},{"location":"contribute/#3-install-required-dependencies","title":"3. Install Required Dependencies","text":"

Install all dependencies listed in the requirements.txt file:

pip install -r requirements.txt\n
"},{"location":"contribute/#4-preview-locally","title":"4. Preview Locally","text":"

Use MkDocs to start the development server and preview the project:

mkdocs serve\n

Access the site locally at:

http://127.0.0.1:8000/AI-Code/\n
"},{"location":"contribute/#making-contributions","title":"Making Contributions","text":""},{"location":"contribute/#make-changes","title":"\u270f\ufe0f Make Changes","text":"

Make your desired code edits, add features, or improve documentation. Follow the project's coding standards and contribution guidelines for consistency.

"},{"location":"contribute/#stage-and-commit-changes","title":"\ud83d\udcbe Stage and Commit Changes","text":""},{"location":"contribute/#1-stage-all-changes","title":"1. Stage All Changes:","text":"
git add .\n
"},{"location":"contribute/#2-commit-changes-with-a-descriptive-message","title":"2. Commit Changes with a Descriptive Message:","text":"
git commit -m \"<your_commit_message>\"\n
"},{"location":"contribute/#push-your-changes","title":"\ud83d\ude80 Push Your Changes","text":"

Push your branch to your forked repository:

git push -u origin <your_branch_name>\n
"},{"location":"contribute/#create-a-pull-request","title":"\ud83d\udcdd Create a Pull Request","text":"
  1. Navigate to your forked repository on GitHub.
  2. Click Pull Requests, then New Pull Request.
  3. Select your branch and describe your changes clearly before submitting.
"},{"location":"contribute/#contribution-guidelines","title":"Contribution Guidelines","text":""},{"location":"contribute/#file-naming-conventions","title":"\ud83d\udcc2 File Naming Conventions","text":"
  • Use kebab-case for file names (e.g., ai-code-example).
"},{"location":"contribute/#documentation-standards","title":"\ud83d\udcda Documentation Standards","text":"
  • Follow the PROJECT README TEMPLATE and ALGORITHM README TEMPLATE.
  • Use raw URLs for images and videos rather than direct uploads.
"},{"location":"contribute/#commit-best-practices","title":"\ud83d\udcbb Commit Best Practices","text":"
  • Keep commits concise and descriptive.
  • Group related changes into a single commit.
"},{"location":"contribute/#pull-request-guidelines","title":"\ud83d\udd00 Pull Request Guidelines","text":"
  • Do not commit directly to the main branch.
  • Use the PR Template and provide all requested details.
  • Include screenshots, video demonstrations, or work samples for UI/UX changes.
"},{"location":"contribute/#code-quality-standards","title":"\ud83e\uddd1\u200d\ud83d\udcbb Code Quality Standards","text":"
  • Write clean, maintainable, and well-commented code.
  • Ensure originality and adherence to project standards.
"},{"location":"contribute/#learning-resources","title":"\ud83d\udcd8 Learning Resources","text":""},{"location":"contribute/#git-github-basics","title":"\ud83e\uddd1\u200d\ud83d\udcbb Git & GitHub Basics","text":"
  • Forking a Repository
  • Cloning a Repository
  • Creating a Pull Request
  • GitHub Learning Lab
"},{"location":"contribute/#general-programming","title":"\ud83d\udcbb General Programming","text":"
  • Learn Python
  • MkDocs Documentation
"},{"location":"project-readme-template/","title":"\ud83d\udcdc Project Template","text":""},{"location":"project-readme-template/#project-title","title":"\ud83d\udcdc Project Title","text":""},{"location":"project-readme-template/#aim","title":"\ud83c\udfaf AIM","text":""},{"location":"project-readme-template/#dataset-link","title":"\ud83d\udcca DATASET LINK","text":"

https://www.google.com

"},{"location":"project-readme-template/#notebook","title":"\ud83d\udcd3 NOTEBOOK","text":"Kaggle Notebook"},{"location":"project-readme-template/#tech-stack","title":"\u2699\ufe0f TECH STACK","text":"Category Technologies Languages Python, JavaScript Libraries/Frameworks TensorFlow, Keras, Flask Databases MongoDB, PostgreSQL Tools Docker, Git, Jupyter, VS Code Deployment AWS, Heroku"},{"location":"project-readme-template/#description","title":"\ud83d\udcdd DESCRIPTION","text":"

What is the requirement of the project?

  • Write the answer here in simple bullet points.
How is it beneficial and used?
  • Write the answer here in simple bullet points.
How did you start approaching this project? (Initial thoughts and planning)
  • Write the answer here in simple bullet points.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Write the answer here in simple bullet points.
"},{"location":"project-readme-template/#explanation","title":"\ud83d\udd0d EXPLANATION","text":""},{"location":"project-readme-template/#details-of-the-different-features","title":"\ud83e\udde9 DETAILS OF THE DIFFERENT FEATURES","text":""},{"location":"project-readme-template/#datasetcsv","title":"\ud83d\udcc2 dataset.csv","text":"Feature Name Description feature 1 explain 1"},{"location":"project-readme-template/#developed-features","title":"\ud83d\udee0 Developed Features","text":"Feature Name Description Reason feature 1 explain 1 reason 1"},{"location":"project-readme-template/#project-workflow","title":"\ud83d\udee4 PROJECT WORKFLOW","text":"

Project workflow

  graph LR\n    A[Start] --> B{Error?};\n    B -->|Yes| C[Hmm...];\n    C --> D[Debug];\n    D --> B;\n    B ---->|No| E[Yay!];
Step 1Step 2Step 3Step 4Step 5Step 6
  • Explanation
  • Explanation
  • Explanation
  • Explanation
  • Explanation
  • Explanation
"},{"location":"project-readme-template/#code-explanation","title":"\ud83d\udda5 CODE EXPLANATION","text":"Section 1
  • Explanation
"},{"location":"project-readme-template/#project-trade-offs-and-solutions","title":"\u2696\ufe0f PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Describe the trade-off encountered (e.g., accuracy vs. computational efficiency).
  • Explain how you addressed this trade-off (e.g., by optimizing hyperparameters, using a more efficient algorithm, etc.).
  • Describe another trade-off (e.g., model complexity vs. interpretability).
  • Explain the solution (e.g., by selecting a model that balances both aspects effectively).
"},{"location":"project-readme-template/#screenshots","title":"\ud83d\uddbc SCREENSHOTS","text":"Visualizations and EDA of different features Image Topic Model performance graphs Image Topic"},{"location":"project-readme-template/#models-used-and-their-evaluation-metrics","title":"\ud83d\udcc9 MODELS USED AND THEIR EVALUATION METRICS","text":"Model Accuracy MSE R2 Score Model Name 95% 0.022 0.90 Model Name 93% 0.033 0.88"},{"location":"project-readme-template/#conclusion","title":"\u2705 CONCLUSION","text":""},{"location":"project-readme-template/#key-learnings","title":"\ud83d\udd11 KEY LEARNINGS","text":"

Insights gained from the data

  • Write from here in bullet points
Improvements in understanding machine learning concepts
  • Write from here in bullet points
"},{"location":"project-readme-template/#use-cases","title":"\ud83c\udf0d USE CASES","text":"Headline 1Headline 2
  • Explain your application
  • Explain your application
"},{"location":"project-readme-template/#useful-links","title":"\ud83d\udd17 USEFUL LINKS","text":"Deployed ModelGitHub RepositoryBinary Model File
  • https://www.google.com
  • https://www.google.com
  • https://www.google.com
"},{"location":"algorithms/","title":"Algorithms \ud83d\udd37","text":"Statistics

Understanding data through statistical analysis and inference methods.

Machine Learning

Dive into the world of algorithms and models in Machine Learning.

Deep Learning

Explore the fascinating world of deep learning.

Computer Vision

Learn computer vision with OpenCV for real-time image processing applications.

Natural Language Processing

Dive into how machines understand and generate human language.

Generative Adversarial Networks

Learn about the power of Generative Adversarial Networks for creative AI solutions.

Large Language Models

Explore the cutting-edge techniques behind large language models like GPT and BERT.

Artificial Intelligence

Explore the fundamentals and advanced concepts of Artificial Intelligence.

"},{"location":"algorithms/artificial-intelligence/","title":"Artificial Intelligence \ud83d\udca1","text":"Search and Optimization

Efficient problem-solving through systematic searching and optimization.

Knowledge Based Systems

AI-driven systems that mimic human expertise for decision-making.

Expert Systems

Simulating expert knowledge to solve complex real-world problems.

Reinforcement Learning

Learning through interaction to maximize rewards and improve performance.

Evolutionary Algorithms

Optimizing solutions through processes inspired by natural selection.

"},{"location":"algorithms/artificial-intelligence/evolutionary-algorithms/","title":"Evolutionary Algorithms \ud83d\udca1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/artificial-intelligence/expert-systems/","title":"Expert Systems \ud83d\udca1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/artificial-intelligence/knowledge-based-systems/","title":"Knowledge Based Systems \ud83d\udca1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/artificial-intelligence/reinforcement-learning/","title":"Reinforcement Learning \ud83d\udca1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/artificial-intelligence/search-and-optimization/","title":"Search and Optimization \ud83d\udca1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/computer-vision/","title":"Computer Vision \ud83c\udfa5","text":"Image Processing

Techniques for manipulating images to improve quality or extract features.

Object Detection

Identifying and localizing objects within images or video.

Semantic Segmentation

Dividing an image into regions with semantic meaning for analysis.

Image Augmentation

Enhancing training data by applying transformations to images.

"},{"location":"algorithms/computer-vision/image-augmentation/","title":"Image Augmentation \ud83c\udfa5","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/computer-vision/image-processing/","title":"Image Processing \ud83c\udfa5","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/computer-vision/object-detection/","title":"Object Detection \ud83c\udfa5","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/computer-vision/semantic-segmentation/","title":"Semantic Segmentation \ud83c\udfa5","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/deep-learning/","title":"Deep Learning \u2728","text":"Neural Networks

Fundamental models of deep learning, inspired by biological neurons.

Optimization Algorithms

Methods to improve the performance of neural networks through efficient learning.

Architectures

Different structures for neural networks, designed for specific tasks and challenges.

Pre-Trained Models

Ready-to-use models that can be fine-tuned for specific tasks.

"},{"location":"algorithms/deep-learning/architectures/","title":"Architectures \u2728","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/deep-learning/neural-networks/","title":"Neural Networks \u2728","text":"Convolutional Neural Networks

Deep learning algorithm for image and video recognition.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 3 mins

"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/","title":"Convolutional Neural Networks","text":""},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#overview","title":"Overview","text":"

Convolutional Neural Networks (CNNs) are a type of deep learning algorithm specifically designed for processing structured grid data such as images. They are widely used in computer vision tasks like image classification, object detection, and image segmentation.

"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#how-cnns-work","title":"How CNNs Work","text":""},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#1-architecture","title":"1. Architecture","text":"

CNNs are composed of the following layers: - Convolutional Layers: Extract spatial features from the input data. - Pooling Layers: Reduce the spatial dimensions of feature maps to lower computational costs. - Fully Connected Layers: Perform high-level reasoning for final predictions.

"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#2-key-concepts","title":"2. Key Concepts","text":"
  • Filters (Kernels): Small matrices that slide over the input to extract features.
  • Strides: Step size of the filter movement.
  • Padding: Adding borders to the input for better filter coverage.
  • Activation Functions: Introduce non-linearity (e.g., ReLU).
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#cnn-algorithms","title":"CNN Algorithms","text":""},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#1-lenet","title":"1. LeNet","text":"
  • Proposed By: Yann LeCun (1998)
  • Use Case: Handwritten digit recognition (e.g., MNIST dataset).
  • Architecture:
  • Input \u2192 Convolution \u2192 Pooling \u2192 Convolution \u2192 Pooling \u2192 Fully Connected \u2192 Output
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#2-alexnet","title":"2. AlexNet","text":"
  • Proposed By: Alex Krizhevsky (2012)
  • Use Case: ImageNet classification challenge.
  • Key Features:
  • Uses ReLU for activation.
  • Includes dropout to prevent overfitting.
  • Designed for GPUs for faster computation.
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#3-vggnet","title":"3. VGGNet","text":"
  • Proposed By: Visual Geometry Group (2014)
  • Use Case: Image classification and transfer learning.
  • Key Features:
  • Uses small 3x3 filters.
  • Depth of the network increases (e.g., VGG-16, VGG-19).
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#4-resnet","title":"4. ResNet","text":"
  • Proposed By: Kaiming He et al. (2015)
  • Use Case: Solving vanishing gradient problems in deep networks.
  • Key Features:
  • Introduces residual blocks with skip connections.
  • Enables training of very deep networks (e.g., ResNet-50, ResNet-101).
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#5-mobilenet","title":"5. MobileNet","text":"
  • Proposed By: Google (2017)
  • Use Case: Mobile and embedded vision applications.
  • Key Features:
  • Utilizes depthwise separable convolutions.
  • Lightweight architecture suitable for mobile devices.
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#code-example-implementing-a-simple-cnn","title":"Code Example: Implementing a Simple CNN","text":"

Here\u2019s a Python example of a CNN using TensorFlow/Keras:

  • Sequential: Used to stack layers to create a neural network model.
  • Conv2D: Implements the convolutional layers to extract features from input images.
  • MaxPooling2D: Reduces the size of feature maps while retaining important features.
  • Flatten: Converts 2D feature maps into a 1D vector to pass into fully connected layers.
  • Dense: Implements fully connected (dense) layers, responsible for decision-making.
from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n\n# Build the CNN\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')  # Replace 10 with the number of classes in your dataset\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Summary\nmodel.summary()\n
"},{"location":"algorithms/deep-learning/neural-networks/convolutional-neural-network/#visualizations","title":"Visualizations","text":"
  • Filters and Feature Maps: Visualizing how the CNN learns features from images.
  • Training Metrics: Plotting accuracy and loss during training.
import matplotlib.pyplot as plt\n\n# Example: Visualizing accuracy and loss\nplt.plot(history.history['accuracy'], label='Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n
"},{"location":"algorithms/deep-learning/optimization-algorithms/","title":"Optimization Algorithms \u2728","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/deep-learning/pre-trained-models/","title":"Pre-Trained Models \u2728","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/generative-adversarial-networks/","title":"Generative Adversarial Networks \ud83d\udcb1","text":"Auxiliary Classifier Generative Adversarial Network

Empowering GANs with Class-Specific Data Generation.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 3 mins

Basic Generative Adversarial Network

Hand writing digit resembler from MNIST dataset

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

Conditional Generative Adversarial Network

Controlled Image synthesis

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

Energy Based Generative Adversarial Network

Minimize the energy functiom to more stable training process.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

Information Maximizing Generative Adversarial Network

Empowering Data-Driven Insights with Generative Adversarial Networks for Advanced Information Synthesis.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

"},{"location":"algorithms/generative-adversarial-networks/ac-gan/","title":"AC GAN","text":""},{"location":"algorithms/generative-adversarial-networks/ac-gan/#overview","title":"Overview","text":"

Auxiliary Classifier Generative Adversarial Network (ACGAN) is an extension of the traditional GAN architecture. It incorporates class information into both the generator and discriminator, enabling controlled generation of samples with specific characteristics.

ACGANs can: - Generate high-quality images conditioned on specific classes. - Predict class labels of generated images via the discriminator.

This dual capability allows for more controlled and targeted image synthesis.

"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#key-concepts","title":"Key Concepts","text":"
  1. Generator:

    • Takes random noise and class labels as input to generate synthetic images conditioned on the class labels.
  2. Discriminator:

    • Differentiates between real and fake images.
    • Predicts the class labels of images.
  3. Class Conditioning:

    • By integrating label embeddings, the generator learns to associate specific features with each class, enhancing image quality and controllability.
"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#implementation-overview","title":"Implementation Overview","text":"

Below is a high-level explanation of the ACGAN implementation:

  1. Dataset:

    • The MNIST dataset is used for training, consisting of grayscale images of digits (0-9).
  2. Model Architecture:

    • Generator:
      • Takes random noise (latent vector) and class labels as input.
      • Outputs images that correspond to the input class labels.
    • Discriminator:
      • Classifies images as real or fake.
      • Simultaneously predicts the class label of the image.
  3. Training Process:

    • The generator is trained to fool the discriminator into classifying fake images as real.
    • The discriminator is trained to:
      • Differentiate real from fake images.
      • Accurately predict the class labels of real images.
  4. Loss Functions:

    • Binary Cross-Entropy Loss for real/fake classification.
    • Categorical Cross-Entropy Loss for class label prediction.
"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#implementation-code","title":"Implementation Code","text":""},{"location":"algorithms/generative-adversarial-networks/ac-gan/#core-components","title":"Core Components","text":""},{"location":"algorithms/generative-adversarial-networks/ac-gan/#discriminator","title":"Discriminator","text":"
class Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.label_emb = nn.Embedding(num_classes, num_classes)\n        self.model = nn.Sequential(\n            nn.Linear(image_size + num_classes, hidden_size),\n            nn.LeakyReLU(0.2),\n            nn.Dropout(0.3),\n            nn.Linear(hidden_size, hidden_size),\n            nn.LeakyReLU(0.2),\n            nn.Dropout(0.3),\n            nn.Linear(hidden_size, 1),\n            nn.Sigmoid()\n        )\n\n    def forward(self, x, labels):\n        x = x.view(x.size(0), image_size)\n        c = self.label_emb(labels)\n        x = torch.cat([x, c], 1)\n        return self.model(x)\n
"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#generator","title":"Generator","text":"
class Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.label_emb = nn.Embedding(num_classes, num_classes)\n        self.model = nn.Sequential(\n            nn.Linear(latent_size + num_classes, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z, labels):\n        z = z.view(z.size(0), latent_size)\n        c = self.label_emb(labels)\n        x = torch.cat([z, c], 1)\n        return self.model(x)\n
"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#training-loop","title":"Training Loop","text":"
for epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_loader):\n        batch_size = images.size(0)\n        images = images.to(device)\n        labels = labels.to(device)\n\n        # Real and fake labels\n        real_labels = torch.ones(batch_size, 1).to(device)\n        fake_labels = torch.zeros(batch_size, 1).to(device)\n\n        # Train Discriminator\n        outputs = D(images, labels)\n        d_loss_real = criterion(outputs, real_labels)\n\n        z = create_noise(batch_size, latent_size)\n        fake_images = G(z, labels)\n        outputs = D(fake_images, labels)\n        d_loss_fake = criterion(outputs, fake_labels)\n\n        d_loss = d_loss_real + d_loss_fake\n        D.zero_grad()\n        d_loss.backward()\n        d_optimizer.step()\n\n        # Train Generator\n        z = create_noise(batch_size, latent_size)\n        fake_images = G(z, labels)\n        outputs = D(fake_images, labels)\n        g_loss = criterion(outputs, real_labels)\n\n        G.zero_grad()\n        g_loss.backward()\n        g_optimizer.step()\n\n        if (i+1) % 200 == 0:\n            print(f\"Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}\")\n
"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#applications-of-acgan","title":"Applications of ACGAN","text":"
  1. Image Synthesis:

    • Generate diverse images conditioned on specific labels.
  2. Data Augmentation:

    • Create synthetic data to augment existing datasets.
  3. Creative Domains:

    • Design tools for controlled image generation in fashion, gaming, and media.
"},{"location":"algorithms/generative-adversarial-networks/ac-gan/#additional-resources","title":"Additional Resources","text":"
  • PyTorch Documentation
  • Original ACGAN Paper
  • MNIST Dataset
"},{"location":"algorithms/generative-adversarial-networks/basic-gan/","title":"Basic GAN","text":"
Basic GAN stands for Basic Generative Adversarial Network\n

This folder contains a basic implementation of a Generative Adversarial Network (GAN) using PyTorch. GANs are a type of neural network architecture that consists of two networks: a generator and a discriminator. The generator learns to create realistic data samples (e.g., images) from random noise, while the discriminator learns to distinguish between real and generated samples.

"},{"location":"algorithms/generative-adversarial-networks/basic-gan/#overview","title":"Overview","text":"

This project implements a simple GAN architecture to generate hand-written digits resembling those from the MNIST dataset. The generator network creates fake images, while the discriminator network tries to differentiate between real and generated images. The networks are trained simultaneously in a minimax game until the generator produces realistic images.

"},{"location":"algorithms/generative-adversarial-networks/basic-gan/#files","title":"Files","text":"
import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.datasets as dsets\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nimage_size = 784  # 28x28\nhidden_size = 256\nlatent_size = 64\nnum_epochs = 200\nbatch_size = 100\nlearning_rate = 0.0002\n\n# MNIST dataset\ndataset = dsets.MNIST(root='../data/',\n                      train=True,\n                      transform=transforms.ToTensor(),\n                      download=True)\n\n# Data loader\ndata_loader = DataLoader(dataset=dataset,\n                         batch_size=batch_size, \n                         shuffle=True)\n\n# Discriminator\nD = nn.Sequential(\n    nn.Linear(image_size, hidden_size),\n    nn.LeakyReLU(0.2),\n    nn.Linear(hidden_size, hidden_size),\n    nn.LeakyReLU(0.2),\n    nn.Linear(hidden_size, 1),\n    nn.Sigmoid())\n\n# Generator\nG = nn.Sequential(\n    nn.Linear(latent_size, hidden_size),\n    nn.ReLU(),\n    nn.Linear(hidden_size, hidden_size),\n    nn.ReLU(),\n    nn.Linear(hidden_size, image_size),\n    nn.Tanh())\n\n# Device setting\nD = D.to(device)\nG = G.to(device)\n\n# Binary cross entropy loss and optimizer\ncriterion = nn.BCELoss()\nd_optimizer = optim.Adam(D.parameters(), lr=learning_rate)\ng_optimizer = optim.Adam(G.parameters(), lr=learning_rate)\n\n# Utility function to create real and fake labels\ndef create_real_labels(size):\n    data = torch.ones(size, 1)\n    return data.to(device)\n\ndef create_fake_labels(size):\n    data = torch.zeros(size, 1)\n    return data.to(device)\n\n# Utility function to generate random noise\ndef create_noise(size, latent_dim):\n    return torch.randn(size, latent_dim).to(device)\n\n# Training the GAN\ntotal_step = len(data_loader)\nfor epoch in range(num_epochs):\n    for i, (images, _) in enumerate(data_loader):\n        batch_size = images.size(0)\n        images = images.reshape(batch_size, -1).to(device)\n\n        # Create the labels which are later used as input for the BCE loss\n        real_labels = create_real_labels(batch_size)\n        fake_labels = create_fake_labels(batch_size)\n\n        # ================================================================== #\n        #                      Train the discriminator                       #\n        # ================================================================== #\n        # Compute BCELoss using real images\n        # Second term of the loss is always zero since real_labels == 1\n        outputs = D(images)\n        d_loss_real = criterion(outputs, real_labels)\n        real_score = outputs\n\n        # Compute BCELoss using fake images\n        noise = create_noise(batch_size, latent_size)\n        fake_images = G(noise)\n        outputs = D(fake_images)\n        d_loss_fake = criterion(outputs, fake_labels)\n        fake_score = outputs\n\n        # Backprop and optimize\n        d_loss = d_loss_real + d_loss_fake\n        d_optimizer.zero_grad()\n        d_loss.backward()\n        d_optimizer.step()\n\n        # ================================================================== #\n        #                        Train the generator                         #\n        # ================================================================== #\n        # Compute loss with fake images\n        noise = create_noise(batch_size, latent_size)\n        fake_images = G(noise)\n        outputs = D(fake_images)\n\n        # We train G to maximize log(D(G(z)) instead of minimizing log(1-D(G(z)))\n        # For the reason, look at the last part of section 3 of the paper:\n        # https://arxiv.org/pdf/1406.2661.pdf\n        g_loss = criterion(outputs, real_labels)\n\n        # Backprop and optimize\n        g_optimizer.zero_grad()\n        g_loss.backward()\n        g_optimizer.step()\n\n        if (i+1) % 200 == 0:\n            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}, D(x): {real_score.mean().item():.2f}, D(G(z)): {fake_score.mean().item():.2f}')\n\n# Save the trained models\ntorch.save(G.state_dict(), 'G.pth')\ntorch.save(D.state_dict(), 'D.pth')\n\n# Plot some generated images\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\nG.eval()\nwith torch.no_grad():\n    noise = create_noise(64, latent_size)\n    fake_images = G(noise)\n    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)\n    fake_images = denorm(fake_images)\n    grid = np.transpose(fake_images.cpu(), (0, 2, 3, 1)).numpy()\n\n    plt.figure(figsize=(8, 8))\n    for i in range(grid.shape[0]):\n        plt.subplot(8, 8, i+1)\n        plt.imshow(grid[i, :, :, 0], cmap='gray')\n        plt.axis('off')\n    plt.show()\n
  • BasicGAN.py: Contains the implementation of the GAN model, training loop, and saving of trained models.
import torch\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nlatent_size = 64\nhidden_size = 256\nimage_size = 784  # 28x28\n\n# Generator\nG = nn.Sequential(\n    nn.Linear(latent_size, hidden_size),\n    nn.ReLU(),\n    nn.Linear(hidden_size, hidden_size),\n    nn.ReLU(),\n    nn.Linear(hidden_size, image_size),\n    nn.Tanh())\n\n# Load the trained generator model\nG.load_state_dict(torch.load('G.pth'))\nG.to(device)\nG.eval()\n\n# Utility function to generate random noise\ndef create_noise(size, latent_dim):\n    return torch.randn(size, latent_dim).to(device)\n\n# Utility function to denormalize the images\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\n# Generate images\nwith torch.no_grad():\n    noise = create_noise(64, latent_size)\n    fake_images = G(noise)\n    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)\n    fake_images = denorm(fake_images)\n    grid = np.transpose(fake_images.cpu(), (0, 2, 3, 1)).numpy()\n\n    plt.figure(figsize=(8, 8))\n    for i in range(grid.shape[0]):\n        plt.subplot(8, 8, i+1)\n        plt.imshow(grid[i, :, :, 0], cmap='gray')\n        plt.axis('off')\n    plt.show()\n
  • test_BasicGAN.py: Uses the trained generator to generate sample images after training.
"},{"location":"algorithms/generative-adversarial-networks/c-gan/","title":"C GAN","text":"

This folder contains an implementation of a Conditional Generative Adversarial Network (cGAN) using PyTorch. cGANs generate images conditioned on specific class labels, allowing for controlled image synthesis.

"},{"location":"algorithms/generative-adversarial-networks/c-gan/#overview","title":"Overview","text":"

cGANs extend the traditional GAN architecture by including class information in both the generator and discriminator. The generator learns to generate images conditioned on given class labels, while the discriminator not only distinguishes between real and fake images but also predicts the class labels of the generated images.

"},{"location":"algorithms/generative-adversarial-networks/c-gan/#files","title":"Files","text":"
import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.datasets as dsets\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nimage_size = 28 * 28\nnum_classes = 10\nlatent_size = 100\nhidden_size = 256\nnum_epochs = 100\nbatch_size = 64\nlearning_rate = 0.0002\n\n# MNIST dataset\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(mean=(0.5,), std=(0.5,))\n])\n\ntrain_dataset = dsets.MNIST(root='../data/',\n                            train=True,\n                            transform=transform,\n                            download=True)\n\ntrain_loader = DataLoader(dataset=train_dataset,\n                          batch_size=batch_size,\n                          shuffle=True)\n\n# Discriminator\nclass Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.label_emb = nn.Embedding(num_classes, num_classes)\n\n        self.model = nn.Sequential(\n            nn.Linear(image_size + num_classes, hidden_size),\n            nn.LeakyReLU(0.2),\n            nn.Dropout(0.3),\n            nn.Linear(hidden_size, hidden_size),\n            nn.LeakyReLU(0.2),\n            nn.Dropout(0.3),\n            nn.Linear(hidden_size, 1),\n            nn.Sigmoid()\n        )\n\n    def forward(self, x, labels):\n        x = x.view(x.size(0), image_size)\n        c = self.label_emb(labels)\n        x = torch.cat([x, c], 1)\n        out = self.model(x)\n        return out\n\n# Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.label_emb = nn.Embedding(num_classes, num_classes)\n\n        self.model = nn.Sequential(\n            nn.Linear(latent_size + num_classes, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z, labels):\n        z = z.view(z.size(0), latent_size)\n        c = self.label_emb(labels)\n        x = torch.cat([z, c], 1)\n        out = self.model(x)\n        return out\n\n# Initialize models\nD = Discriminator().to(device)\nG = Generator().to(device)\n\n# Loss function and optimizer\ncriterion = nn.BCELoss()\nd_optimizer = optim.Adam(D.parameters(), lr=learning_rate)\ng_optimizer = optim.Adam(G.parameters(), lr=learning_rate)\n\n# Utility functions\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\ndef create_noise(batch_size, latent_size):\n    return torch.randn(batch_size, latent_size).to(device)\n\ndef create_labels(batch_size):\n    return torch.randint(0, num_classes, (batch_size,)).to(device)\n\n# Training the cGAN\ntotal_step = len(train_loader)\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_loader):\n        batch_size = images.size(0)\n        images = images.to(device)\n        labels = labels.to(device)\n\n        # Create the labels which are later used as input for the discriminator\n        real_labels = torch.ones(batch_size, 1).to(device)\n        fake_labels = torch.zeros(batch_size, 1).to(device)\n\n        # ================================================================== #\n        #                      Train the discriminator                       #\n        # ================================================================== #\n\n        # Compute BCELoss using real images\n        outputs = D(images, labels)\n        d_loss_real = criterion(outputs, real_labels)\n        real_score = outputs\n\n        # Compute BCELoss using fake images\n        z = create_noise(batch_size, latent_size)\n        fake_images = G(z, labels)\n        outputs = D(fake_images, labels)\n        d_loss_fake = criterion(outputs, fake_labels)\n        fake_score = outputs\n\n        # Backprop and optimize\n        d_loss = d_loss_real + d_loss_fake\n        D.zero_grad()\n        d_loss.backward()\n        d_optimizer.step()\n\n        # ================================================================== #\n        #                        Train the generator                         #\n        # ================================================================== #\n\n        # Compute loss with fake images\n        z = create_noise(batch_size, latent_size)\n        fake_images = G(z, labels)\n        outputs = D(fake_images, labels)\n\n        # We train G to maximize log(D(G(z)))\n        g_loss = criterion(outputs, real_labels)\n\n        # Backprop and optimize\n        G.zero_grad()\n        g_loss.backward()\n        g_optimizer.step()\n\n        if (i+1) % 200 == 0:\n            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}, D(x): {real_score.mean().item():.2f}, D(G(z)): {fake_score.mean().item():.2f}')\n\n# Save the trained models\ntorch.save(G.state_dict(), 'G_cgan.pth')\ntorch.save(D.state_dict(), 'D_cgan.pth')\n
  • cGAN.py: Contains the implementation of the ACGAN model, training loop, and saving of trained models.
import torch\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nlatent_size = 100\nnum_classes = 10\nimage_size = 28 * 28\n\n# Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.label_emb = nn.Embedding(num_classes, num_classes)\n\n        self.model = nn.Sequential(\n            nn.Linear(latent_size + num_classes, 256),\n            nn.ReLU(),\n            nn.Linear(256, 512),\n            nn.ReLU(),\n            nn.Linear(512, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z, labels):\n        z = z.view(z.size(0), latent_size)\n        c = self.label_emb(labels)\n        x = torch.cat([z, c], 1)\n        out = self.model(x)\n        return out\n\n# Load the trained generator model\nG = Generator()\nG.load_state_dict(torch.load('G_cgan.pth', map_location=torch.device('cpu')))\nG.eval()\n\n# Utility function to generate random noise\ndef create_noise(size, latent_dim):\n    return torch.randn(size, latent_dim)\n\n# Utility function to generate labels\ndef create_labels(size):\n    return torch.randint(0, num_classes, (size,))\n\n# Utility function to denormalize the images\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\n# Generate images\nwith torch.no_grad():\n    noise = create_noise(64, latent_size)\n    labels = create_labels(64)\n    fake_images = G(noise, labels)\n    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)\n    fake_images = denorm(fake_images)\n    grid = np.transpose(fake_images, (0, 2, 3, 1)).numpy()\n\n    plt.figure(figsize=(8, 8))\n    for i in range(grid.shape[0]):\n        plt.subplot(8, 8, i+1)\n        plt.imshow(grid[i, :, :, 0], cmap='gray')\n        plt.axis('off')\n    plt.show()\n
  • test_cGAN.py: Uses the trained generator to generate sample images after training.
"},{"location":"algorithms/generative-adversarial-networks/eb-gan/","title":"EB GAN","text":"

This folder contains an implementation of an Energy-Based Generative Adversarial Network (EBGAN) using PyTorch. EBGAN focuses on matching the energy distribution of generated samples to that of real data, optimizing both a discriminator and a generator network.

"},{"location":"algorithms/generative-adversarial-networks/eb-gan/#overview","title":"Overview","text":"

EBGAN introduces an energy function that is used to measure the quality of generated samples. The discriminator (autoencoder-like) network tries to minimize this energy function while the generator tries to maximize it. This results in a more stable training process compared to traditional GANs.

"},{"location":"algorithms/generative-adversarial-networks/eb-gan/#files","title":"Files","text":"
import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.datasets as dsets\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nimage_size = 28 * 28\nlatent_size = 64\nhidden_size = 256\nnum_epochs = 100\nbatch_size = 64\nlearning_rate = 0.0002\nk = 3  # Number of iterations for optimizing D\n\n# MNIST dataset\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(mean=(0.5,), std=(0.5,))\n])\n\ntrain_dataset = dsets.MNIST(root='../data/',\n                            train=True,\n                            transform=transform,\n                            download=True)\n\ntrain_loader = DataLoader(dataset=train_dataset,\n                          batch_size=batch_size,\n                          shuffle=True)\n\n# Discriminator\nclass Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.encoder = nn.Sequential(\n            nn.Linear(image_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, latent_size)\n        )\n        self.decoder = nn.Sequential(\n            nn.Linear(latent_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, x):\n        encoded = self.encoder(x)\n        decoded = self.decoder(encoded)\n        return decoded, encoded\n\n# Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(latent_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, hidden_size),\n            nn.ReLU(),\n            nn.Linear(hidden_size, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z):\n        out = self.model(z)\n        return out\n\n# Initialize models\nD = Discriminator().to(device)\nG = Generator().to(device)\n\n# Loss function and optimizer\ncriterion_rec = nn.MSELoss()\nd_optimizer = optim.Adam(D.parameters(), lr=learning_rate)\ng_optimizer = optim.Adam(G.parameters(), lr=learning_rate)\n\n# Utility functions\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\n# Training the EBGAN\ntotal_step = len(train_loader)\nfor epoch in range(num_epochs):\n    for i, (images, _) in enumerate(train_loader):\n        batch_size = images.size(0)\n        images = images.view(-1, image_size).to(device)\n\n        # ================================================================== #\n        #                      Train the discriminator                       #\n        # ================================================================== #\n\n        encoded_real, _ = D(images)\n        decoded_real = D.decoder(encoded_real)\n\n        rec_loss_real = criterion_rec(decoded_real, images)\n\n        z = torch.randn(batch_size, latent_size).to(device)\n        fake_images = G(z)\n        encoded_fake, _ = D(fake_images.detach())\n        decoded_fake = D.decoder(encoded_fake)\n\n        rec_loss_fake = criterion_rec(decoded_fake, fake_images.detach())\n\n        d_loss = rec_loss_real + torch.max(torch.zeros(1).to(device), k * rec_loss_real - rec_loss_fake)\n\n        D.zero_grad()\n        d_loss.backward()\n        d_optimizer.step()\n\n        # ================================================================== #\n        #                        Train the generator                         #\n        # ================================================================== #\n\n        z = torch.randn(batch_size, latent_size).to(device)\n        fake_images = G(z)\n        encoded_fake, _ = D(fake_images)\n        decoded_fake = D.decoder(encoded_fake)\n\n        rec_loss_fake = criterion_rec(decoded_fake, fake_images)\n\n        g_loss = rec_loss_fake\n\n        G.zero_grad()\n        g_loss.backward()\n        g_optimizer.step()\n\n        if (i+1) % 200 == 0:\n            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}, Rec_loss_real: {rec_loss_real.item():.4f}, Rec_loss_fake: {rec_loss_fake.item():.4f}')\n\n# Save the trained models\ntorch.save(G.state_dict(), 'G_ebgan.pth')\ntorch.save(D.state_dict(), 'D_ebgan.pth')\n
  • EBGAN.py: Contains the implementation of the ACGAN model, training loop, and saving of trained models.
import torch\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nlatent_size = 64\nimage_size = 28 * 28\n\n# Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(latent_size, 256),\n            nn.ReLU(),\n            nn.Linear(256, 512),\n            nn.ReLU(),\n            nn.Linear(512, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z):\n        out = self.model(z)\n        return out\n\n# Load the trained generator model\nG = Generator()\nG.load_state_dict(torch.load('G_ebgan.pth', map_location=torch.device('cpu')))\nG.eval()\n\n# Utility function to generate random noise\ndef create_noise(size, latent_dim):\n    return torch.randn(size, latent_dim)\n\n# Utility function to denormalize the images\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\n# Generate images\nwith torch.no_grad():\n    noise = create_noise(64, latent_size)\n    fake_images = G(noise)\n    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)\n    fake_images = denorm(fake_images)\n    grid = np.transpose(fake_images, (0, 2, 3, 1)).numpy()\n\n    plt.figure(figsize=(8, 8))\n    for i in range(grid.shape[0]):\n        plt.subplot(8, 8, i+1)\n        plt.imshow(grid[i, :, :, 0], cmap='gray')\n        plt.axis('off')\n    plt.show()\n
  • test_EBGAN.py: Uses the trained generator to generate sample images after training.
"},{"location":"algorithms/generative-adversarial-networks/info-gan/","title":"Info GAN","text":"
Information Maximizing Generative Adversarial Network\n

This folder contains an implementation of InfoGAN using PyTorch. InfoGAN extends the traditional GAN framework by incorporating unsupervised learning of interpretable and disentangled representations.

"},{"location":"algorithms/generative-adversarial-networks/info-gan/#overview","title":"Overview","text":"

InfoGAN introduces latent codes that can be split into categorical and continuous variables, allowing for more control over the generated outputs. The generator is conditioned on these latent codes, which are learned in an unsupervised manner during training.

"},{"location":"algorithms/generative-adversarial-networks/info-gan/#files","title":"Files","text":"
import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.datasets as dsets\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nimage_size = 28 * 28\nnum_epochs = 50\nbatch_size = 100\nlatent_size = 62\nnum_continuous = 2\nnum_categories = 10\nlearning_rate = 0.0002\n\n# MNIST dataset\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(mean=(0.5,), std=(0.5,))\n])\n\ntrain_dataset = dsets.MNIST(root='../data/',\n                            train=True,\n                            transform=transform,\n                            download=True)\n\ntrain_loader = DataLoader(dataset=train_dataset,\n                          batch_size=batch_size,\n                          shuffle=True)\n\n# Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.fc = nn.Sequential(\n            nn.Linear(latent_size + num_categories + num_continuous, 256),\n            nn.ReLU(),\n            nn.Linear(256, 512),\n            nn.ReLU(),\n            nn.Linear(512, 1024),\n            nn.ReLU(),\n            nn.Linear(1024, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z, c_cat, c_cont):\n        inputs = torch.cat([z, c_cat, c_cont], dim=1)\n        return self.fc(inputs)\n\n\n# Discriminator\nclass Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.fc = nn.Sequential(\n            nn.Linear(image_size, 1024),\n            nn.ReLU(),\n            nn.Dropout(0.3),\n            nn.Linear(1024, 512),\n            nn.ReLU(),\n            nn.Dropout(0.3),\n        )\n\n        self.fc_disc = nn.Linear(512, num_categories)\n        self.fc_mu = nn.Linear(512, num_continuous)\n        self.fc_var = nn.Linear(512, num_continuous)\n\n    def forward(self, x):\n        x = self.fc(x)\n        disc_logits = self.fc_disc(x)\n        mu = self.fc_mu(x)\n        var = torch.exp(self.fc_var(x))\n        return disc_logits, mu, var\n\n\n# Initialize networks\nG = Generator().to(device)\nD = Discriminator().to(device)\n\n# Loss functions\ncriterion_cat = nn.CrossEntropyLoss()\ncriterion_cont = nn.MSELoss()\n\n# Optimizers\ng_optimizer = optim.Adam(G.parameters(), lr=learning_rate)\nd_optimizer = optim.Adam(D.parameters(), lr=learning_rate)\n\n# Utility functions\ndef sample_noise(batch_size, latent_size):\n    return torch.randn(batch_size, latent_size).to(device)\n\ndef sample_categorical(batch_size, num_categories):\n    return torch.randint(0, num_categories, (batch_size,)).to(device)\n\ndef sample_continuous(batch_size, num_continuous):\n    return torch.rand(batch_size, num_continuous).to(device)\n\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\n# Training InfoGAN\ntotal_step = len(train_loader)\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_loader):\n        batch_size = images.size(0)\n        images = images.view(-1, image_size).to(device)\n\n        # Create labels for discriminator\n        real_labels = torch.ones(batch_size, dtype=torch.long, device=device)\n        fake_labels = torch.zeros(batch_size, dtype=torch.long, device=device)\n\n        # Sample noise, categorical, and continuous latent codes\n        z = sample_noise(batch_size, latent_size)\n        c_cat = sample_categorical(batch_size, num_categories)\n        c_cont = sample_continuous(batch_size, num_continuous)\n\n        # Generate fake images\n        fake_images = G(z, c_cat, c_cont)\n\n        # Train discriminator\n        d_optimizer.zero_grad()\n        d_real_cat, d_real_mu, d_real_var = D(images)\n        d_real_loss_cat = criterion_cat(d_real_cat, labels)\n        d_fake_cat, d_fake_mu, d_fake_var = D(fake_images.detach())\n        d_fake_loss_cat = criterion_cat(d_fake_cat, c_cat)\n\n        d_loss_cat = d_real_loss_cat + d_fake_loss_cat\n\n        d_real_loss_cont = torch.mean(0.5 * torch.sum(torch.div((d_real_mu - c_cont)**2, d_real_var), dim=1))\n        d_fake_loss_cont = torch.mean(0.5 * torch.sum(torch.div((d_fake_mu - c_cont)**2, d_fake_var), dim=1))\n\n        d_loss_cont = d_real_loss_cont + d_fake_loss_cont\n\n        d_loss = d_loss_cat + d_loss_cont\n        d_loss.backward()\n        d_optimizer.step()\n\n        # Train generator\n        g_optimizer.zero_grad()\n        _, d_fake_mu, d_fake_var = D(fake_images)\n\n        g_loss_cat = criterion_cat(_, c_cat)\n        g_loss_cont = torch.mean(0.5 * torch.sum(torch.div((d_fake_mu - c_cont)**2, d_fake_var), dim=1))\n\n        g_loss = g_loss_cat + g_loss_cont\n        g_loss.backward()\n        g_optimizer.step()\n\n        if (i+1) % 200 == 0:\n            print(f'Epoch [{epoch}/{num_epochs}], Step [{i+1}/{total_step}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}')\n\n# Save the trained models\ntorch.save(G.state_dict(), 'G_infogan.pth')\ntorch.save(D.state_dict(), 'D_infogan.pth')\n
  • InfoGAN.py: Contains the implementation of the ACGAN model, training loop, and saving of trained models.
import torch\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Hyper-parameters\nlatent_size = 62\nnum_categories = 10\nnum_continuous = 2\nimage_size = 28 * 28\n\n# Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.fc = nn.Sequential(\n            nn.Linear(latent_size + num_categories + num_continuous, 256),\n            nn.ReLU(),\n            nn.Linear(256, 512),\n            nn.ReLU(),\n            nn.Linear(512, 1024),\n            nn.ReLU(),\n            nn.Linear(1024, image_size),\n            nn.Tanh()\n        )\n\n    def forward(self, z, c_cat, c_cont):\n        inputs = torch.cat([z, c_cat, c_cont], dim=1)\n        return self.fc(inputs)\n\n# Load the trained generator model\nG = Generator().to(device)\nG.load_state_dict(torch.load('G_infogan.pth', map_location=torch.device('cpu')))\nG.eval()\n\n# Utility functions to generate samples\ndef sample_noise(batch_size, latent_size):\n    return torch.randn(batch_size, latent_size).to(device)\n\ndef sample_categorical(batch_size, num_categories):\n    return torch.randint(0, num_categories, (batch_size,)).to(device)\n\ndef sample_continuous(batch_size, num_continuous):\n    return torch.rand(batch_size, num_continuous).to(device)\n\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)\n\n# Generate images\nwith torch.no_grad():\n    noise = sample_noise(64, latent_size)\n    c_cat = sample_categorical(64, num_categories)\n    c_cont = sample_continuous(64, num_continuous)\n    fake_images = G(noise, c_cat, c_cont)\n    fake_images = fake_images.reshape(fake_images.size(0), 1, 28, 28)\n    fake_images = denorm(fake_images)\n    grid = np.transpose(fake_images, (0, 2, 3, 1)).numpy()\n\n    plt.figure(figsize=(8, 8))\n    for i in range(grid.shape[0]):\n        plt.subplot(8, 8, i+1)\n        plt.imshow(grid[i, :, :, 0], cmap='gray')\n        plt.axis('off')\n    plt.show()\n
  • test_InfoGAN.py: Uses the trained generator to generate sample images after training.
"},{"location":"algorithms/large-language-models/","title":"Large Language Models \ud83e\udde0","text":"GPT-Series

Generative Pretrained Transformers for advanced natural language processing tasks.

BERT

Bidirectional Encoder Representations for language understanding and NLP tasks.

T5

Text-to-Text Transfer Transformer for a unified approach to various NLP tasks.

Bloom

A large multilingual model designed for natural language understanding and generation.

"},{"location":"algorithms/large-language-models/bert/","title":"BERT \ud83e\udde0","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/large-language-models/bloom/","title":"Bloom \ud83e\udde0","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/large-language-models/gpt-series/","title":"GPT Series \ud83e\udde0","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/large-language-models/t5/","title":"T5 \ud83e\udde0","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/machine-learning/","title":"Machine Learning \ud83e\udd16","text":"Data Preprocessing

Technique that is used to convert the raw data into a clean data set.

Supervised

Uses labeled datasets to train algorithms to predict outcomes and recognize patterns.

Unsupervised

Uses algorithms to analyze unlabeled data without human intervention

Boosting

Modeling technique that attempts to build a strong classifier from the number of weak classifiers.

"},{"location":"algorithms/machine-learning/boosting/","title":"Boosting \ud83e\udd16","text":"Light Gradient Boosting Machine

Powerful gradient-boosting framework that can be used for both regression and classification tasks.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 4 mins

"},{"location":"algorithms/machine-learning/boosting/light-gbm/","title":"Light gbm","text":""},{"location":"algorithms/machine-learning/boosting/light-gbm/#lightgbm-a-comprehensive-guide-to-scratch-implementation","title":"LightGBM: A Comprehensive Guide to Scratch Implementation","text":"

Overview: LightGBM (Light Gradient Boosting Machine) is an advanced gradient boosting framework that efficiently handles large datasets. Unlike traditional boosting methods, LightGBM uses leaf-wise tree growth, which improves accuracy and reduces computation time.

"},{"location":"algorithms/machine-learning/boosting/light-gbm/#key-highlights","title":"Key Highlights:","text":"
  • Speed and Efficiency: Faster training on large datasets compared to XGBoost.
  • Memory Optimization: Lower memory usage, making it scalable.
  • Built-in Handling of Categorical Data: No need for manual one-hot encoding.
  • Parallel and GPU Training: Supports multi-threading and GPU acceleration for faster computation.
"},{"location":"algorithms/machine-learning/boosting/light-gbm/#how-lightgbm-works-scratch-implementation-guide","title":"How LightGBM Works (Scratch Implementation Guide):","text":""},{"location":"algorithms/machine-learning/boosting/light-gbm/#1-core-concept-leaf-wise-tree-growth","title":"1. Core Concept (Leaf-Wise Tree Growth):","text":"
  • Level-wise (XGBoost): Grows all leaves at the same depth before moving to the next.
  • Leaf-wise (LightGBM): Grows the leaf that reduces the most loss, potentially leading to deeper, more accurate trees.

Example Visualization:

Level-wise (XGBoost)                Leaf-wise (LightGBM)\n        O                                 O\n       / \\                               / \\\n      O   O                             O   O\n     / \\                                 \\\n    O   O                                 O\n

"},{"location":"algorithms/machine-learning/boosting/light-gbm/#algorithm-breakdown","title":"Algorithm Breakdown:","text":"
  1. Initialize Model: Start with a simple model (like mean predictions).
  2. Compute Residuals: Calculate errors between actual and predicted values.
  3. Train Trees to Predict Residuals: Fit new trees to minimize residuals.
  4. Update Model: Adjust predictions by adding the new tree\u2019s results.
  5. Repeat Until Convergence or Early Stopping.
"},{"location":"algorithms/machine-learning/boosting/light-gbm/#parameters-explained","title":"Parameters Explained:","text":"
  • num_leaves: Limits the number of leaves in a tree (complexity control).
  • max_depth: Constrains tree depth to prevent overfitting.
  • learning_rate: Scales the contribution of each tree to control convergence.
  • n_estimators: Number of boosting rounds (trees).
  • min_data_in_leaf: Minimum number of data points in a leaf to avoid overfitting small branches.
"},{"location":"algorithms/machine-learning/boosting/light-gbm/#scratch-code-example-from-the-ground-up","title":"Scratch Code Example (From the Ground Up):","text":"

File: lightgbm_model.py

import lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\n\nclass LightGBMModel:\n    def __init__(self, params=None):\n        self.params = params if params else {\n            'objective': 'regression',\n            'metric': 'rmse',\n            'boosting_type': 'gbdt',\n            'num_leaves': 31,\n            'learning_rate': 0.05,\n            'n_estimators': 100\n        }\n        self.model = None\n\n    def fit(self, X_train, y_train):\n        d_train = lgb.Dataset(X_train, label=y_train)\n        self.model = lgb.train(self.params, d_train)\n\n    def predict(self, X_test):\n        return self.model.predict(X_test)\n

"},{"location":"algorithms/machine-learning/boosting/light-gbm/#testing-the-model","title":"Testing the Model:","text":"

File: lightgbm_model_test.py

import unittest\nimport numpy as np\nfrom sklearn.datasets import load_diabetes\nfrom sklearn.model_selection import train_test_split\nfrom lightgbm_model import LightGBMModel\n\nclass TestLightGBMModel(unittest.TestCase):\n\n    def test_lightgbm(self):\n        # Load Dataset\n        data = load_diabetes()\n        X_train, X_test, y_train, y_test = train_test_split(\n            data.data, data.target, test_size=0.2, random_state=42)\n\n        # Train Model\n        model = LightGBMModel()\n        model.fit(X_train, y_train)\n\n        # Predict and Evaluate\n        predictions = model.predict(X_test)\n        mse = mean_squared_error(y_test, predictions)\n        self.assertTrue(mse < 3500, \"MSE is too high, LightGBM not performing well\")\n\nif __name__ == '__main__':\n    unittest.main()\n

"},{"location":"algorithms/machine-learning/boosting/light-gbm/#additional-insights-to-aid-understanding","title":"Additional Insights to Aid Understanding:","text":"
  • Feature Importance:
    lgb.plot_importance(model.model)\n
  • Early Stopping Implementation:
    self.model = lgb.train(self.params, d_train, valid_sets=[d_train], early_stopping_rounds=10)\n
"},{"location":"algorithms/machine-learning/boosting/light-gbm/#testing-and-validation","title":"Testing and Validation:","text":"

Use sklearn datasets to validate the implementation. Compare performance with other boosting models to highlight LightGBM\u2019s efficiency.

"},{"location":"algorithms/machine-learning/data-preprocessing/","title":"Data Pre-processing \ud83e\udd16","text":"Encoding

Process of converting categorical data into numerical values

Imputation

Technique that replaces missing values in a dataset with estimated values.

Scaling and Normalization

Techniques that transform numerical data values into a common scale.

"},{"location":"algorithms/machine-learning/data-preprocessing/encoding/","title":"Encoding Algorithms \ud83e\udd16","text":"Ordinal Encoder

Transforming categorical data into meaningful numerical order.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 3 mins

"},{"location":"algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/","title":"ORDINAL ENCODER","text":"

A custom implementation of an OrdinalEncoder class for encoding categorical data into ordinal integers using a pandas DataFrame. The class maps each unique category to an integer based on the order of appearance.

"},{"location":"algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/#features","title":"Features","text":"
  • fit: Learn the mapping of categories to ordinal integers for each column.
  • transform: Transform the categorical data to ordinal integers based on the learned mapping.
  • fit_transform: Fit the encoder and transform the data in one step.
"},{"location":"algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/#methods","title":"Methods","text":"
  1. __init__(self)
    • Initializes the OrdinalEncoding class.
    • No parameters are required.
  2. fit(self, data)
    • Learns the mapping of categories to ordinal integers for each column.
    • Parameters:
      • data (pandas.DataFrame): The data to fit.
    • Raises:
      • TypeError: If the input data is not a pandas DataFrame.
  3. transform(self, data)
    • Transforms the categorical data to ordinal integers based on the learned mapping.
    • Parameters:
      • data (pandas.DataFrame): The data to transform.
    • Returns:
      • pandas.DataFrame: The transformed data.
    • Raises:
      • Error: If transform is called before fit or fit_transform.
  4. fit_transform(self, data)
    • Fits the encoder to the data and transforms the data in one step.
    • Parameters:
      • data (pandas.DataFrame): The data to fit and transform.
    • Returns:
      • pandas.DataFrame: The transformed data.
"},{"location":"algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/#error-handling","title":"Error Handling","text":"
  • Raises a TypeError if the input data is not a pandas DataFrame in the fit method.
  • Raises an error if transform is called before fit or fit_transform.
"},{"location":"algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/#use-case","title":"Use Case","text":""},{"location":"algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/#output","title":"Output","text":"
  • ordinal_encoder.py file
import pandas as pd\n\nclass OrdinalEncoding:\n    def __init__(self):\n        self.category_mapping = {}\n\n    def fit(self, data):\n        # Fit the encoder to the data (pandas DataFrame).\n        # type check\n        if not type(data)==pd.DataFrame:\n            raise f\"Type of data should be Pandas.DataFrame; {type(data)} found\"\n        for column in data.columns:\n            unique_categories = sorted(set(data[column]))\n            self.category_mapping[column] = {category: idx for idx, category in enumerate(unique_categories)}\n\n    def transform(self, data):\n        # Transform the data (pandas DataFrame) to ordinal integers.\n        # checking for empty mapping\n        if not self.category_mapping:\n            raise \"Catrgorical Mapping not found. Call OrdinalExcoding.fit() method or call OrdinalEncoding.fit_transform() method\"\n\n        data_transformed = data.copy()\n        for column in data.columns:\n            data_transformed[column] = data[column].map(self.category_mapping[column])\n        return data_transformed\n\n    def fit_transform(self, data):\n        # Fit the encoder and transform the data in one step.\n        self.fit(data)\n        return self.transform(data)\n
  • test_ordinal_encoder.py file
import os\nimport sys\n# for resolving any path conflict\ncurrent = os.path.dirname(os.path.realpath(\"ordinal_encoder.py\"))\nparent = os.path.dirname(current)\nsys.path.append(current)\n\nimport pandas as pd\n\nfrom Ordinal_Encoder.ordinal_encoder import OrdinalEncoding\n\n# Example usage\ndata = {\n    'Category1': ['low', 'medium', 'high', 'medium', 'low', 'high', 'medium'],\n    'Category2': ['A', 'B', 'A', 'B', 'A', 'B', 'A'],\n    'Category3': ['X', 'Y', 'X', 'Y', 'X', 'Y', 'X']\n}\ndf = pd.DataFrame(data)\n\nencoder = OrdinalEncoding()\nencoded_df = encoder.fit_transform(df)\n\nprint(\"Original DataFrame:\")\nprint(df)\nprint(\"\\nEncoded DataFrame:\")\nprint(encoded_df)\n
"},{"location":"algorithms/machine-learning/data-preprocessing/imputation/","title":"Imputation Algorithm \ud83e\udd16","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/","title":"Scaling and Normalization \ud83e\udd16","text":"Min Max Scaler

Scaling features to a standardized range for improved model performance.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 3 mins

Standard Scaler

Centering and scaling features for a balanced dataset.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 3 mins

"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/","title":"MIN MAX SCALER","text":"

A custom implementation of a MinMaxScaler class for scaling numerical data in a pandas DataFrame. The class scales the features to a specified range, typically between 0 and 1.

"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/#features","title":"Features","text":"
  • fit: Calculate the minimum and maximum values of the data.
  • transform: Scale the data to the specified feature range.
  • fit_transform: Fit the scaler and transform the data in one step.
  • get_params: Retrieve the minimum and maximum values calculated during fitting.
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/#methods","title":"Methods","text":"
  1. __init__(self, feature_range=(0, 1))
    • Initializes the MinMaxScaling class.
    • Parameters:
      • feature_range (tuple): Desired range of transformed data. Default is (0, 1).
  2. fit(self, data)
    • Calculates the minimum and maximum values of the data.
    • Parameters:
      • data (pandas.DataFrame): The data to fit.
  3. transform(self, data)
    • Transforms the data to the specified feature range.
    • Parameters:
      • data (pandas.DataFrame): The data to transform.
    • Returns:
      • pandas.DataFrame: The scaled data.
  4. fit_transform(self, data)
    • Fits the scaler to the data and transforms the data in one step.
    • Parameters:
      • data (pandas.DataFrame): The data to fit and transform.
    • Returns:
      • pandas.DataFrame: The scaled data.
  5. get_params(self)
    • Retrieves the minimum and maximum values calculated during fitting.
    • Returns:
      • dict: Dictionary containing the minimum and maximum values.
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/#error-handling","title":"Error Handling","text":"
  • Raises a TypeError if the input data is not a pandas DataFrame in the fit method.
  • Raises an error if transform is called before fit or fit_transform.
  • Raises an error in get_params if called before fit.
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/#use-case","title":"Use Case","text":""},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/#output","title":"Output","text":"
  • min_max_scaler.py file
import pandas as pd\n\n# Custom MinMaxScaler class\nclass MinMaxScaling:\n    # init function\n    def __init__(self, feature_range=(0, 1)):  # feature range can be specified by the user else it takes (0,1)\n        self.min = feature_range[0]\n        self.max = feature_range[1]\n        self.data_min_ = None\n        self.data_max_ = None\n\n    # fit function to calculate min and max value of the data\n    def fit(self, data):\n        # type check\n        if not type(data)==pd.DataFrame:\n            raise f\"TypeError : parameter should be a Pandas.DataFrame; {type(data)} found\"\n        else:\n            self.data_min_ = data.min()\n            self.data_max_ = data.max()\n\n    # transform function\n    def transform(self, data):\n        if self.data_max_ is None or self.data_min_ is None:\n            raise \"Call MinMaxScaling.fit() first or call MinMaxScaling.fit_transform() as the required params not found\"\n        else:\n            data_scaled = (data - self.data_min_) / (self.data_max_ - self.data_min_)\n            data_scaled = data_scaled * (self.max - self.min) + self.min\n            return data_scaled\n\n    # fit_tranform function\n    def fit_transform(self, data):\n        self.fit(data)\n        return self.transform(data)\n\n    # get_params function\n    def get_params(self):\n        if self.data_max_ is None or self.data_min_ is None:\n            raise \"Params not found! Call MinMaxScaling.fit() first\"\n        else:\n            return {\"Min\" : self.data_min_,\n                    \"Max\" : self.data_max_}\n
  • test_min_max_scaler.py file
import os\nimport sys\n# for resolving any path conflict\ncurrent = os.path.dirname(os.path.realpath(\"min_max_scaler.py\"))\nparent = os.path.dirname(current)\nsys.path.append(current)\n\nimport pandas as pd\n\nfrom Min_Max_Scaler.min_max_scaler import MinMaxScaling\n\n# Example DataFrame\ndata = {\n    'A': [1, 2, 3, 4, 5],\n    'B': [10, 20, 30, 40, 50],\n    'C': [100, 200, 300, 400, 500]\n}\n\ndf = pd.DataFrame(data)\n\n# Initialize the CustomMinMaxScaler\nscaler = MinMaxScaling()\n\n# Fit the scaler to the data and transform the data\nscaled_df = scaler.fit_transform(df)\n\nprint(\"Original DataFrame:\")\nprint(df)\nprint(\"\\nScaled DataFrame:\")\nprint(scaled_df)\n
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/","title":"STANDARD SCALER","text":"

A custom implementation of a StandardScaler class for scaling numerical data in a pandas DataFrame or NumPy array. The class scales the features to have zero mean and unit variance.

"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/#features","title":"Features","text":"
  • fit: Calculate the mean and standard deviation of the data.
  • transform: Scale the data to have zero mean and unit variance.
  • fit_transform: Fit the scaler and transform the data in one step.
  • get_params: Retrieve the mean and standard deviation calculated during fitting.
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/#methods","title":"Methods","text":"
  1. __init__(self)
    • Initializes the StandardScaling class.
    • No parameters are required.
  2. fit(self, data)
    • Calculates the mean and standard deviation of the data.
    • Parameters:
      • data (pandas.DataFrame or numpy.ndarray): The data to fit.
    • Raises:
      • TypeError: If the input data is not a pandas DataFrame or NumPy array.
  3. transform(self, data)
    • Transforms the data to have zero mean and unit variance.
    • Parameters:
      • data (pandas.DataFrame or numpy.ndarray): The data to transform.
    • Returns:
      • numpy.ndarray: The scaled data.
    • Raises:
      • Error: If transform is called before fit or fit_transform.
  4. fit_transform(self, data)
    • Fits the scaler to the data and transforms the data in one step.
    • Parameters:
      • data (pandas.DataFrame or numpy.ndarray): The data to fit and transform.
    • Returns:
      • numpy.ndarray: The scaled data.
  5. get_params(self)
    • Retrieves the mean and standard deviation calculated during fitting.
    • Returns:
      • dict: Dictionary containing the mean and standard deviation.
    • Raises:
      • Error: If get_params is called before fit.
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/#error-handling","title":"Error Handling","text":"
  • Raises a TypeError if the input data is not a pandas DataFrame or NumPy array in the fit method.
  • Raises an error if transform is called before fit or fit_transform.
  • Raises an error in get_params if called before fit.
"},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/#use-case","title":"Use Case","text":""},{"location":"algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/#output","title":"Output","text":"
  • standard_scaler.py file
import pandas as pd\nimport numpy as np\n\n# Custom MinMaxScaler class\nclass StandardScaling:\n    # init function\n    def __init__(self):     \n        self.data_mean_ = None\n        self.data_std_ = None\n\n    # fit function to calculate min and max value of the data\n    def fit(self, data):\n        # type check\n        if not (type(data)==pd.DataFrame or type(data)==np.ndarray):\n            raise f\"TypeError : parameter should be a Pandas.DataFrame or Numpy.ndarray; {type(data)} found\"\n        elif type(data)==pd.DataFrame:\n            data = data.to_numpy()\n\n        self.data_mean_ = np.mean(data, axis=0)\n        self.data_std_ = np.sqrt(np.var(data, axis=0))\n\n    # transform function\n    def transform(self, data):\n        if self.data_mean_ is None or self.data_std_ is None:\n            raise \"Call StandardScaling.fit() first or call StandardScaling.fit_transform() as the required params not found\"\n        else:\n            data_scaled = (data - self.data_mean_) / (self.data_std_)\n            return data_scaled\n\n    # fit_tranform function\n    def fit_transform(self, data):\n        self.fit(data)\n        return self.transform(data)\n\n    # get_params function\n    def get_params(self):\n        if self.data_mean_ is None or self.data_std_ is None:\n            raise \"Params not found! Call StandardScaling.fit() first\"\n        else:\n            return {\"Mean\" : self.data_mean_,\n                    \"Standard Deviation\" : self.data_std_}\n
  • test_standard_scaler.py file
import os\nimport sys\n# for resolving any path conflict\ncurrent = os.path.dirname(os.path.realpath(\"standard_scaler.py\"))\nparent = os.path.dirname(current)\nsys.path.append(current)\n\nimport pandas as pd\n\nfrom Standard_Scaler.standard_scaler import StandardScaling\n\n# Example DataFrame\ndata = {\n    'A': [1, 2, 3, 4, 5],\n    'B': [10, 20, 30, 40, 50],\n    'C': [100, 200, 300, 400, 500]\n}\n\ndf = pd.DataFrame(data)\n\n# Initialize the CustomMinMaxScaler\nscaler = StandardScaling()\n\n# Fit the scaler to the data and transform the data\nscaled_df = scaler.fit_transform(df)\n\nprint(\"Original DataFrame:\")\nprint(df)\nprint(\"\\nScaled DataFrame:\")\nprint(scaled_df)\nprint(\"\\nAssociated Parameters:\")\nprint(scaler.get_params())\n
"},{"location":"algorithms/machine-learning/supervised/","title":"Supervised Machine Learning \ud83e\udd16","text":"Regression

Predicting continuous outcomes with precision.

Classification

Categorizing data for informed decisions.

"},{"location":"algorithms/machine-learning/supervised/classifications/","title":"Classification Algorithms \ud83e\udd16","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/machine-learning/supervised/regressions/","title":"Regression Algorithms \ud83e\udd16","text":"Linear Regression

Understanding the relationship between two variables.

\ud83d\udcc5 2025-01-19 | \u23f1\ufe0f 2 mins

Bayesian Regression

Infusing uncertainty with predictions for smarter decision-making.

\ud83d\udcc5 2025-01-19 | \u23f1\ufe0f 3 mins

"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/","title":"AdaBoost","text":"

Overview: AdaBoost (Adaptive Boosting) is one of the most popular ensemble methods for boosting weak learners to create a strong learner. It works by combining multiple \"weak\" models, typically decision stumps, and focusing more on the errors from previous models. This iterative process improves accuracy and reduces bias.

"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#key-highlights","title":"Key Highlights:","text":"
  • Boosting Concept: Builds an ensemble by sequentially focusing on harder-to-classify instances.
  • Adaptive Weighting: Misclassified instances get higher weights, and correctly classified instances get lower weights in subsequent rounds.
  • Simple and Effective: Often uses decision stumps (single-level decision trees) as base models.
  • Versatility: Applicable to both regression and classification problems.
"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#how-adaboost-works-scratch-implementation-guide","title":"How AdaBoost Works (Scratch Implementation Guide):","text":""},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#1-core-concept-error-weight-adjustment","title":"1. Core Concept (Error Weight Adjustment):","text":"
  • Assigns equal weights to all data points initially.
  • In each iteration:
  • A weak model (e.g., a decision stump) is trained on the weighted dataset.
  • Misclassified points are assigned higher weights for the next iteration.
  • A final strong model is constructed by combining all weak models, weighted by their accuracy.

Visualization:

Iteration 1: Train weak model -> Update weights  \nIteration 2: Train weak model -> Update weights  \n...  \nFinal Model: Combine weak models with weighted contributions  \n

"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#algorithm-breakdown","title":"Algorithm Breakdown:","text":"
  1. Initialize Weights: Assign equal weights to all instances.
  2. Train a Weak Model: Use weighted data to train a weak learner.
  3. Calculate Model Error: Compute the weighted error rate of the model.
  4. Update Instance Weights: Increase weights for misclassified points and decrease weights for correctly classified points.
  5. Update Model Weight: Calculate the model\u2019s contribution based on its accuracy.
  6. Repeat for a Set Number of Iterations or Until Convergence.
"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#parameters-explained","title":"Parameters Explained:","text":"
  • n_estimators: Number of weak learners (iterations).
  • learning_rate: Shrinks the contribution of each weak learner to avoid overfitting.
  • base_estimator: The weak learner used (e.g., DecisionTreeRegressor or DecisionTreeClassifier).
"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#scratch-code-example-from-the-ground-up","title":"Scratch Code Example (From the Ground Up):","text":"

File: adaboost_model.py

import numpy as np\nfrom sklearn.tree import DecisionTreeRegressor\n\nclass AdaBoostRegressor:\n    def __init__(self, n_estimators=50, learning_rate=1.0):\n        self.n_estimators = n_estimators\n        self.learning_rate = learning_rate\n        self.models = []\n        self.model_weights = []\n\n    def fit(self, X, y):\n        n_samples = X.shape[0]\n        # Initialize weights\n        sample_weights = np.ones(n_samples) / n_samples\n\n        for _ in range(self.n_estimators):\n            # Train weak model\n            model = DecisionTreeRegressor(max_depth=1)\n            model.fit(X, y, sample_weight=sample_weights)\n            predictions = model.predict(X)\n\n            # Calculate weighted error\n            error = np.sum(sample_weights * (y != predictions)) / np.sum(sample_weights)\n            if error > 0.5:\n                break\n\n            # Calculate model weight\n            model_weight = self.learning_rate * np.log((1 - error) / error)\n\n            # Update sample weights\n            sample_weights *= np.exp(model_weight * (y != predictions))\n            sample_weights /= np.sum(sample_weights)\n\n            self.models.append(model)\n            self.model_weights.append(model_weight)\n\n    def predict(self, X):\n        # Combine predictions from all models\n        final_prediction = sum(weight * model.predict(X) for model, weight in zip(self.models, self.model_weights))\n        return np.sign(final_prediction)\n

"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#testing-the-model","title":"Testing the Model:","text":"

File: adaboost_model_test.py

import unittest\nimport numpy as np\nfrom sklearn.datasets import make_regression\nfrom sklearn.metrics import mean_squared_error\nfrom adaboost_model import AdaBoostRegressor\n\nclass TestAdaBoostRegressor(unittest.TestCase):\n\n    def test_adaboost(self):\n        # Generate synthetic dataset\n        X, y = make_regression(n_samples=100, n_features=1, noise=15, random_state=42)\n        y = np.sign(y)  # Convert to classification-like regression\n\n        # Train AdaBoost Regressor\n        model = AdaBoostRegressor(n_estimators=10)\n        model.fit(X, y)\n\n        # Predict and Evaluate\n        predictions = model.predict(X)\n        mse = mean_squared_error(y, predictions)\n        self.assertTrue(mse < 0.5, \"MSE is too high, AdaBoost not performing well\")\n\nif __name__ == '__main__':\n    unittest.main()\n

"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#additional-insights-to-aid-understanding","title":"Additional Insights to Aid Understanding:","text":"
  • Feature Importance:
    for i, model in enumerate(model.models):\n    print(f\"Model {i} weight: {model_weights[i]}\")\n
  • Early Stopping Implementation: Use validation metrics to stop training if performance does not improve over several iterations.
"},{"location":"algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/#testing-and-validation","title":"Testing and Validation:","text":"

Use datasets from sklearn (e.g., make_regression) to validate the implementation. Compare AdaBoost with other boosting models like Gradient Boosting and LightGBM to analyze performance differences.

"},{"location":"algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/","title":"Decision Tree Regression","text":"

This module contains an implementation of Decision Tree Regression, a versatile algorithm for predicting a continuous outcome based on input features.

"},{"location":"algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/#parameters","title":"Parameters","text":"
  • max_depth: Maximum depth of the decision tree. Controls the complexity of the model.
"},{"location":"algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/#scratch-code","title":"Scratch Code","text":"
  • decision_tree_regression.py file
import numpy as np\n\nclass DecisionTreeRegression:\n\n    def __init__(self, max_depth=None):\n        \"\"\"\n        Constructor for the DecisionTreeRegression class.\n\n        Parameters:\n        - max_depth: Maximum depth of the decision tree.\n        \"\"\"\n        self.max_depth = max_depth\n        self.tree = None\n\n    def _calculate_variance(self, y):\n        \"\"\"\n        Calculate the variance of a set of target values.\n\n        Parameters:\n        - y: Target values (numpy array).\n\n        Returns:\n        - Variance of the target values.\n        \"\"\"\n        return np.var(y)\n\n    def _split_dataset(self, X, y, feature_index, threshold):\n        \"\"\"\n        Split the dataset based on a feature and threshold.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        - feature_index: Index of the feature to split on.\n        - threshold: Threshold value for the split.\n\n        Returns:\n        - Left and right subsets of the dataset.\n        \"\"\"\n        left_mask = X[:, feature_index] <= threshold\n        right_mask = ~left_mask\n        return X[left_mask], X[right_mask], y[left_mask], y[right_mask]\n\n    def _find_best_split(self, X, y):\n        \"\"\"\n        Find the best split for the dataset.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n\n        Returns:\n        - Index of the best feature and the corresponding threshold.\n        \"\"\"\n        m, n = X.shape\n        best_feature_index = None\n        best_threshold = None\n        best_variance_reduction = 0\n\n        initial_variance = self._calculate_variance(y)\n\n        for feature_index in range(n):\n            thresholds = np.unique(X[:, feature_index])\n\n            for threshold in thresholds:\n                # Split the dataset\n                _, _, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)\n\n                # Calculate variance reduction\n                left_weight = len(y_left) / m\n                right_weight = len(y_right) / m\n                variance_reduction = initial_variance - (left_weight * self._calculate_variance(y_left) + right_weight * self._calculate_variance(y_right))\n\n                # Update the best split if variance reduction is greater\n                if variance_reduction > best_variance_reduction:\n                    best_feature_index = feature_index\n                    best_threshold = threshold\n                    best_variance_reduction = variance_reduction\n\n        return best_feature_index, best_threshold\n\n    def _build_tree(self, X, y, depth):\n        \"\"\"\n        Recursively build the decision tree.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        - depth: Current depth of the tree.\n\n        Returns:\n        - Node of the decision tree.\n        \"\"\"\n        # Check if max depth is reached or if all target values are the same\n        if depth == self.max_depth or np.all(y == y[0]):\n            return {'value': np.mean(y)}\n\n        # Find the best split\n        feature_index, threshold = self._find_best_split(X, y)\n\n        if feature_index is not None:\n            # Split the dataset\n            X_left, X_right, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)\n\n            # Recursively build left and right subtrees\n            left_subtree = self._build_tree(X_left, y_left, depth + 1)\n            right_subtree = self._build_tree(X_right, y_right, depth + 1)\n\n            return {'feature_index': feature_index,\n                    'threshold': threshold,\n                    'left': left_subtree,\n                    'right': right_subtree}\n        else:\n            # If no split is found, return a leaf node\n            return {'value': np.mean(y)}\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Decision Tree Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        self.tree = self._build_tree(X, y, depth=0)\n\n    def _predict_single(self, node, x):\n        \"\"\"\n        Recursively predict a single data point.\n\n        Parameters:\n        - node: Current node in the decision tree.\n        - x: Input features for prediction.\n\n        Returns:\n        - Predicted value.\n        \"\"\"\n        if 'value' in node:\n            return node['value']\n        else:\n            if x[node['feature_index']] <= node['threshold']:\n                return self._predict_single(node['left'], x)\n            else:\n                return self._predict_single(node['right'], x)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        return np.array([self._predict_single(self.tree, x) for x in X])\n
  • decision_tree_regression_test.py file
import unittest\nimport numpy as np\nfrom DecisionTreeRegressor import DecisionTreeRegression\n\nclass TestDecisionTreeRegressor(unittest.TestCase):\n\n    def setUp(self):\n        # Create sample data for testing\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 2)\n        self.y_train = 2 * self.X_train[:, 0] + 3 * self.X_train[:, 1] + np.random.normal(0, 0.1, 100)\n\n        self.X_test = np.random.rand(10, 2)\n\n    def test_fit_predict(self):\n        # Test if the model can be fitted and predictions are made\n        dt_model = DecisionTreeRegression(max_depth=3)\n        dt_model.fit(self.X_train, self.y_train)\n\n        # Ensure predictions are made without errors\n        predictions = dt_model.predict(self.X_test)\n\n        # Add your specific assertions based on the expected behavior of your model\n        self.assertIsInstance(predictions, np.ndarray)\n        self.assertEqual(predictions.shape, (10,))\n\n    # Add more test cases as needed\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/","title":"Elastic Net Regression","text":"

This module contains an implementation of Elastic Net Regression, a powerful linear regression technique that combines both L1 (Lasso) and L2 (Ridge) regularization. Elastic Net is particularly useful when dealing with high-dimensional datasets and can effectively handle correlated features.

"},{"location":"algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/#parameters","title":"Parameters","text":"
  • alpha: The regularization strength. A positive float value.
  • l1_ratio: The ratio of L1 regularization to L2 regularization. Should be between 0 and 1.
  • max_iter: The maximum number of iterations to run the optimization algorithm.
  • tol: The tolerance for the optimization. If the updates are smaller than this value, the optimization will stop.
"},{"location":"algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/#scratch-code","title":"Scratch Code","text":"
  • elastic_net_regression.py file
import numpy as np\n\nclass ElasticNetRegression:\n    def __init__(self, alpha=1.0, l1_ratio=0.5, max_iter=1000, tol=1e-4):\n        self.alpha = alpha\n        self.l1_ratio = l1_ratio\n        self.max_iter = max_iter\n        self.tol = tol\n        self.coef_ = None\n        self.intercept_ = None\n\n    def fit(self, X, y):\n        n_samples, n_features = X.shape\n        self.coef_ = np.zeros(n_features)\n        self.intercept_ = 0\n        learning_rate = 0.01  \n\n        for iteration in range(self.max_iter):\n            y_pred = np.dot(X, self.coef_) + self.intercept_\n            error = y - y_pred\n\n            gradient_w = (-2 / n_samples) * (X.T.dot(error)) + self.alpha * (self.l1_ratio * np.sign(self.coef_) + (1 - self.l1_ratio) * 2 * self.coef_)\n            gradient_b = (-2 / n_samples) * np.sum(error)\n\n            new_coef = self.coef_ - learning_rate * gradient_w\n            new_intercept = self.intercept_ - learning_rate * gradient_b\n\n            if np.all(np.abs(new_coef - self.coef_) < self.tol) and np.abs(new_intercept - self.intercept_) < self.tol:\n                break\n\n            self.coef_ = new_coef\n            self.intercept_ = new_intercept\n\n    def predict(self, X):\n        return np.dot(X, self.coef_) + self.intercept_\n
  • elastic_net_regression_test.py file
import unittest\nimport numpy as np\nfrom sklearn.linear_model import ElasticNet\nfrom ElasticNetRegression import ElasticNetRegression\n\nclass TestElasticNetRegression(unittest.TestCase):\n\n    def test_elastic_net_regression(self):\n        np.random.seed(42)\n        X_train = np.random.rand(100, 1) * 10\n        y_train = 2 * X_train.squeeze() + np.random.randn(100) * 2 \n\n        X_test = np.array([[2.5], [5.0], [7.5]])\n\n        custom_model = ElasticNetRegression(alpha=1.0, l1_ratio=0.5)\n        custom_model.fit(X_train, y_train)\n        custom_predictions = custom_model.predict(X_test)\n\n        sklearn_model = ElasticNet(alpha=1.0, l1_ratio=0.5, max_iter=1000, tol=1e-4)\n        sklearn_model.fit(X_train, y_train)\n        sklearn_predictions = sklearn_model.predict(X_test)\n\n        np.testing.assert_allclose(custom_predictions, sklearn_predictions, rtol=1e-1)\n\n        train_predictions_custom = custom_model.predict(X_train)\n        train_predictions_sklearn = sklearn_model.predict(X_train)\n\n        custom_mse = np.mean((y_train - train_predictions_custom) ** 2)\n        sklearn_mse = np.mean((y_train - train_predictions_sklearn) ** 2)\n\n        print(f\"Custom Model MSE: {custom_mse}\")\n        print(f\"Scikit-learn Model MSE: {sklearn_mse}\")\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/","title":"Gradient Boosting Regression","text":"

This module contains an implementation of Gradient Boosting Regression, an ensemble learning method that combines multiple weak learners (typically decision trees) to create a more robust and accurate model for predicting continuous outcomes based on input features.

"},{"location":"algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/#parameters","title":"Parameters","text":"
  • n_estimators: Number of boosting stages (trees) to be run.
  • learning_rate: Step size shrinkage to prevent overfitting.
  • max_depth: Maximum depth of each decision tree.
"},{"location":"algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/#scratch-code","title":"Scratch Code","text":"
  • gradient_boosting_regression.py file
import numpy as np\n\nclass GradientBoostingRegression:\n    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):\n        \"\"\"\n        Constructor for the GradientBoostingRegression class.\n\n        Parameters:\n        - n_estimators: Number of trees in the ensemble.\n        - learning_rate: Step size for each tree's contribution.\n        - max_depth: Maximum depth of each decision tree.\n        \"\"\"\n        self.n_estimators = n_estimators\n        self.learning_rate = learning_rate\n        self.max_depth = max_depth\n        self.trees = []\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the gradient boosting regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        # Initialize predictions with the mean of the target values\n        predictions = np.mean(y) * np.ones_like(y)\n\n        for _ in range(self.n_estimators):\n            # Compute residuals\n            residuals = y - predictions\n\n            # Fit a decision tree to the residuals\n            tree = self._fit_tree(X, residuals, depth=0)\n\n            # Update predictions using the tree's contribution scaled by the learning rate\n            predictions += self.learning_rate * self._predict_tree(X, tree)\n\n            # Save the tree in the ensemble\n            self.trees.append(tree)\n\n    def _fit_tree(self, X, y, depth):\n        \"\"\"\n        Fit a decision tree to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        - depth: Current depth of the tree.\n\n        Returns:\n        - Tree structure (dictionary).\n        \"\"\"\n        if depth == self.max_depth:\n            # If the maximum depth is reached, return the mean of the target values\n            return np.mean(y)\n\n        # Find the best split point\n        feature_index, threshold = self._find_best_split(X, y)\n\n        if feature_index is None:\n            # If no split improves the purity, return the mean of the target values\n            return np.mean(y)\n\n        # Split the data\n        mask = X[:, feature_index] < threshold\n        left_tree = self._fit_tree(X[mask], y[mask], depth + 1)\n        right_tree = self._fit_tree(X[~mask], y[~mask], depth + 1)\n\n        # Return the tree structure\n        return {'feature_index': feature_index, 'threshold': threshold,\n                'left_tree': left_tree, 'right_tree': right_tree}\n\n    def _find_best_split(self, X, y):\n        \"\"\"\n        Find the best split point for a decision tree.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n\n        Returns:\n        - Best feature index and threshold for the split.\n        \"\"\"\n        m, n = X.shape\n        if m <= 1:\n            return None, None  # No split is possible\n\n        # Calculate the initial impurity\n        initial_impurity = self._calculate_impurity(y)\n\n        # Initialize variables to store the best split parameters\n        best_feature_index, best_threshold, best_impurity_reduction = None, None, 0\n\n        for feature_index in range(n):\n            # Sort the feature values and corresponding target values\n            sorted_indices = np.argsort(X[:, feature_index])\n            sorted_X = X[sorted_indices, feature_index]\n            sorted_y = y[sorted_indices]\n\n            # Initialize variables to keep track of impurity and counts for the left and right nodes\n            left_impurity, left_count = 0, 0\n            right_impurity, right_count = initial_impurity, m\n\n            for i in range(1, m):\n                # Update impurity and counts for the left and right nodes\n                value = sorted_X[i]\n                left_impurity += (i / m) * self._calculate_impurity(sorted_y[i-1:i+1])\n                left_count += 1\n                right_impurity -= ((i-1) / m) * self._calculate_impurity(sorted_y[i-1:i+1])\n                right_count -= 1\n\n                # Calculate impurity reduction\n                impurity_reduction = initial_impurity - (left_count / m * left_impurity + right_count / m * right_impurity)\n\n                # Check if this is the best split so far\n                if impurity_reduction > best_impurity_reduction:\n                    best_feature_index = feature_index\n                    best_threshold = value\n                    best_impurity_reduction = impurity_reduction\n\n        return best_feature_index, best_threshold\n\n    def _calculate_impurity(self, y):\n        \"\"\"\n        Calculate the impurity of a node.\n\n        Parameters:\n        - y: Target values (numpy array).\n\n        Returns:\n        - Impurity.\n        \"\"\"\n        # For regression, impurity is the variance of the target values\n        return np.var(y)\n\n    def _predict_tree(self, X, tree):\n        \"\"\"\n        Make predictions using a decision tree.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - tree: Tree structure (dictionary).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        if 'feature_index' not in tree:\n            # If the node is a leaf, return the constant value\n            return tree\n        else:\n            # Recursively traverse the tree\n            mask = X[:, tree['feature_index']] < tree['threshold']\n            return np.where(mask, self._predict_tree(X, tree['left_tree']), self._predict_tree(X, tree['right_tree']))\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data using the Gradient Boosting Regression.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        predictions = np.sum(self.learning_rate * self._predict_tree(X, tree) for tree in self.trees)\n        return predictions\n
  • gradient_boosting_regression_test.py file
import unittest\nimport numpy as np\nfrom GradientBoostingRegressor import GradientBoostingRegression\n\nclass TestGradientBoostingRegressor(unittest.TestCase):\n\n    def setUp(self):\n        # Create sample data for testing\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 2)\n        self.y_train = 2 * self.X_train[:, 0] + 3 * self.X_train[:, 1] + np.random.normal(0, 0.1, 100)\n\n        self.X_test = np.random.rand(10, 2)\n\n    def test_fit_predict(self):\n        # Test if the model can be fitted and predictions are made\n        gbr_model = GradientBoostingRegression(n_estimators=5, learning_rate=0.1, max_depth=3)\n        gbr_model.fit(self.X_train, self.y_train)\n\n        # Ensure predictions are made without errors\n        predictions = gbr_model.predict(self.X_test)\n\n        # Add your specific assertions based on the expected behavior of your model\n        self.assertIsInstance(predictions, np.ndarray)\n        self.assertEqual(predictions.shape, (10,))\n\n    # Add more test cases as needed\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Huber_Regression/","title":"Huber Regression","text":"

This module contains an implementation of Huber Regression, a robust linear regression technique that combines the properties of both least squares and absolute error loss functions. Huber Regression is particularly useful when dealing with datasets that have outliers, as it is less sensitive to outliers compared to standard linear regression.

"},{"location":"algorithms/machine-learning/supervised/regressions/Huber_Regression/#overview","title":"Overview","text":"

Huber Regression is a regression algorithm that adds a penalty based on the Huber loss function. This loss function is quadratic for small errors and linear for large errors, providing robustness against outliers.

"},{"location":"algorithms/machine-learning/supervised/regressions/Huber_Regression/#parameters","title":"Parameters","text":"
  • alpha: The regularization strength. A positive float value.
  • epsilon: The threshold for the Huber loss function. A positive float value.
  • max_iter: The maximum number of iterations to run the optimization algorithm.
  • tol: The tolerance for the optimization. If the updates are smaller than this value, the optimization will stop.
"},{"location":"algorithms/machine-learning/supervised/regressions/Huber_Regression/#scratch-code","title":"Scratch Code","text":"
  • huber_regression.py file
import numpy as np\n\nclass HuberRegression:\n    def __init__(self, alpha=1.0, epsilon=1.35, max_iter=1000, tol=1e-4):\n        self.alpha = alpha\n        self.epsilon = epsilon\n        self.max_iter = max_iter\n        self.tol = tol\n        self.coef_ = None\n        self.intercept_ = None\n\n    def fit(self, X, y):\n        n_samples, n_features = X.shape\n        self.coef_ = np.zeros(n_features)\n        self.intercept_ = 0\n        learning_rate = 0.01\n\n        for iteration in range(self.max_iter):\n            y_pred = np.dot(X, self.coef_) + self.intercept_\n            error = y - y_pred\n\n            # Compute Huber gradient\n            mask = np.abs(error) <= self.epsilon\n            gradient_w = (-2 / n_samples) * (X.T.dot(error * mask) + self.epsilon * np.sign(error) * (~mask)) + self.alpha * self.coef_\n            gradient_b = (-2 / n_samples) * (np.sum(error * mask) + self.epsilon * np.sign(error) * (~mask))\n\n            new_coef = self.coef_ - learning_rate * gradient_w\n            new_intercept = self.intercept_ - learning_rate * gradient_b\n\n            if np.all(np.abs(new_coef - self.coef_) < self.tol) and np.abs(new_intercept - self.intercept_) < self.tol:\n                break\n\n            self.coef_ = new_coef\n            self.intercept_ = new_intercept\n\n    def predict(self, X):\n        return np.dot(X, self.coef_) + self.intercept_\n
  • huber_regression_test.py file
import unittest\nimport numpy as np\nfrom sklearn.linear_model import HuberRegressor\nfrom HuberRegression import HuberRegression\n\nclass TestHuberRegression(unittest.TestCase):\n\n    def test_huber_regression(self):\n        np.random.seed(42)\n        X_train = np.random.rand(100, 1) * 10\n        y_train = 2 * X_train.squeeze() + np.random.randn(100) * 2\n\n        X_test = np.array([[2.5], [5.0], [7.5]])\n\n        huber_model = HuberRegression(alpha=1.0, epsilon=1.35)\n        huber_model.fit(X_train, y_train)\n        huber_predictions = huber_model.predict(X_test)\n\n        sklearn_model = HuberRegressor(alpha=1.0, epsilon=1.35, max_iter=1000, tol=1e-4)\n        sklearn_model.fit(X_train, y_train)\n        sklearn_predictions = sklearn_model.predict(X_test)\n\n        np.testing.assert_allclose(huber_predictions, sklearn_predictions, rtol=1e-1)\n\n        train_predictions_huber = huber_model.predict(X_train)\n        train_predictions_sklearn = sklearn_model.predict(X_train)\n\n        huber_mse = np.mean((y_train - train_predictions_huber) ** 2)\n        sklearn_mse = np.mean((y_train - train_predictions_sklearn) ** 2)\n\n        print(f\"Huber Model MSE: {huber_mse}\")\n        print(f\"Scikit-learn Model MSE: {sklearn_mse}\")\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/","title":"K Nearest Neighbors Regression","text":"

This module contains an implementation of K-Nearest Neighbors Regression, a simple yet effective algorithm for predicting continuous outcomes based on input features.

"},{"location":"algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/#parameters","title":"Parameters","text":"
  • k: Number of neighbors to consider for prediction.
"},{"location":"algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/#scratch-code","title":"Scratch Code","text":"
  • k_nearest_neighbors_regression.py file
import numpy as np\n\nclass KNNRegression:\n    def __init__(self, k=5):\n        \"\"\"\n        Constructor for the KNNRegression class.\n\n        Parameters:\n        - k: Number of neighbors to consider.\n        \"\"\"\n        self.k = k\n        self.X_train = None\n        self.y_train = None\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the KNN model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        self.X_train = X\n        self.y_train = y\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        predictions = []\n        for x in X:\n            # Calculate Euclidean distances between the input point and all training points\n            distances = np.linalg.norm(self.X_train - x, axis=1)\n\n            # Get indices of k-nearest neighbors\n            indices = np.argsort(distances)[:self.k]\n\n            # Average the target values of k-nearest neighbors\n            predicted_value = np.mean(self.y_train[indices])\n            predictions.append(predicted_value)\n\n        return np.array(predictions)\n
  • k_nearest_neighbors_regression_test.py file
import unittest\nimport numpy as np\nfrom KNearestNeighborsRegression import KNNRegression\n\nclass TestKNNRegression(unittest.TestCase):\n\n    def test_knn_regression(self):\n        # Create synthetic data\n        np.random.seed(42)\n        X_train = np.random.rand(100, 1) * 10\n        y_train = 2 * X_train.squeeze() + np.random.randn(100) * 2  # Linear relationship with noise\n\n        X_test = np.array([[2.5], [5.0], [7.5]])\n\n        # Initialize and fit the KNN Regression model\n        knn_model = KNNRegression(k=3)\n        knn_model.fit(X_train, y_train)\n\n        # Test predictions\n        predictions = knn_model.predict(X_test)\n        expected_predictions = [2 * 2.5, 2 * 5.0, 2 * 7.5]  # Assuming a linear relationship\n\n        # Check if predictions are close to the expected values\n        np.testing.assert_allclose(predictions, expected_predictions, rtol=1e-5)\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Lasso_Regression/","title":"Lasso Regression","text":"

This module contains an implementation of Lasso Regression, a linear regression technique with L1 regularization.

"},{"location":"algorithms/machine-learning/supervised/regressions/Lasso_Regression/#overview","title":"Overview","text":"

Lasso Regression is a regression algorithm that adds a penalty term based on the absolute values of the coefficients. This penalty term helps in feature selection by driving some of the coefficients to exactly zero, effectively ignoring certain features.

"},{"location":"algorithms/machine-learning/supervised/regressions/Lasso_Regression/#parameters","title":"Parameters","text":"
  • learning_rate: The step size for gradient descent.
  • lambda_param: Regularization strength (L1 penalty).
  • n_iterations: The number of iterations for gradient descent.
"},{"location":"algorithms/machine-learning/supervised/regressions/Lasso_Regression/#scratch-code","title":"Scratch Code","text":"
  • lasso_regression.py file
import numpy as np\n\nclass LassoRegression:\n    def __init__(self, learning_rate=0.01, lambda_param=0.01, n_iterations=1000):\n        \"\"\"\n        Constructor for the LassoRegression class.\n\n        Parameters:\n        - learning_rate: The step size for gradient descent.\n        - lambda_param: Regularization strength.\n        - n_iterations: The number of iterations for gradient descent.\n        \"\"\"\n        self.learning_rate = learning_rate\n        self.lambda_param = lambda_param\n        self.n_iterations = n_iterations\n        self.weights = None\n        self.bias = None\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Lasso Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        # Initialize weights and bias\n        num_samples, num_features = X.shape\n        self.weights = np.zeros(num_features)\n        self.bias = 0\n\n        # Perform gradient descent\n        for _ in range(self.n_iterations):\n            predictions = np.dot(X, self.weights) + self.bias\n            errors = y - predictions\n\n            # Update weights and bias\n            self.weights += self.learning_rate * (1/num_samples) * (np.dot(X.T, errors) - self.lambda_param * np.sign(self.weights))\n            self.bias += self.learning_rate * (1/num_samples) * np.sum(errors)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        return np.dot(X, self.weights) + self.bias\n
  • lasso_regression_test.py file
import unittest\nimport numpy as np\nfrom LassoRegression import LassoRegression\n\nclass TestLassoRegression(unittest.TestCase):\n    def setUp(self):\n        # Create a sample dataset\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 2)\n        self.y_train = 3 * self.X_train[:, 0] + 2 * self.X_train[:, 1] + np.random.randn(100)\n\n        self.X_test = np.random.rand(10, 2)\n\n    def test_fit_predict(self):\n        # Test the fit and predict methods\n        model = LassoRegression(learning_rate=0.01, lambda_param=0.1, n_iterations=1000)\n        model.fit(self.X_train, self.y_train)\n        predictions = model.predict(self.X_test)\n\n        # Ensure predictions are of the correct shape\n        self.assertEqual(predictions.shape, (10,))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Logistic_Regression/","title":"Logistic Regression","text":"

This module contains an implementation of Logistic Regression, a popular algorithm for binary classification.

"},{"location":"algorithms/machine-learning/supervised/regressions/Logistic_Regression/#parameters","title":"Parameters","text":"
  • learning_rate: Step size for gradient descent.
  • n_iterations: Number of iterations for gradient descent.
"},{"location":"algorithms/machine-learning/supervised/regressions/Logistic_Regression/#scratch-code","title":"Scratch Code","text":"
  • logistic_regression.py file
import numpy as np\n\nclass LogisticRegression:\n    def __init__(self, learning_rate=0.01, n_iterations=1000):\n        \"\"\"\n        Constructor for the LogisticRegression class.\n\n        Parameters:\n        - learning_rate: The step size for gradient descent.\n        - n_iterations: The number of iterations for gradient descent.\n        \"\"\"\n        self.learning_rate = learning_rate\n        self.n_iterations = n_iterations\n        self.weights = None\n        self.bias = None\n\n    def _sigmoid(self, z):\n        \"\"\"\n        Sigmoid activation function.\n\n        Parameters:\n        - z: Linear combination of input features and weights.\n\n        Returns:\n        - Sigmoid of z.\n        \"\"\"\n        return 1 / (1 + np.exp(-z))\n\n    def _initialize_parameters(self, n_features):\n        \"\"\"\n        Initialize weights and bias.\n\n        Parameters:\n        - n_features: Number of input features.\n\n        Returns:\n        - Initialized weights and bias.\n        \"\"\"\n        self.weights = np.zeros(n_features)\n        self.bias = 0\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Logistic Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target labels (numpy array).\n        \"\"\"\n        n_samples, n_features = X.shape\n        self._initialize_parameters(n_features)\n\n        for _ in range(self.n_iterations):\n            # Linear combination of features and weights\n            linear_combination = np.dot(X, self.weights) + self.bias\n\n            # Predictions using the sigmoid function\n            predictions = self._sigmoid(linear_combination)\n\n            # Update weights and bias using gradient descent\n            dw = (1 / n_samples) * np.dot(X.T, (predictions - y))\n            db = (1 / n_samples) * np.sum(predictions - y)\n\n            self.weights -= self.learning_rate * dw\n            self.bias -= self.learning_rate * db\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted labels (numpy array).\n        \"\"\"\n        linear_combination = np.dot(X, self.weights) + self.bias\n        predictions = self._sigmoid(linear_combination)\n\n        # Convert probabilities to binary predictions (0 or 1)\n        return np.round(predictions)\n
  • logistic_regression_test.py file
import numpy as np\nimport unittest\nfrom LogisticRegression import LogisticRegression\n\nclass TestLogisticRegression(unittest.TestCase):\n    def setUp(self):\n        # Generate synthetic data for testing\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 2)\n        self.y_train = (np.random.rand(100) > 0.5).astype(int)\n\n        self.X_test = np.random.rand(20, 2)\n\n    def test_fit_predict(self):\n        model = LogisticRegression(learning_rate=0.01, n_iterations=1000)\n        model.fit(self.X_train, self.y_train)\n        predictions = model.predict(self.X_test)\n\n        self.assertEqual(predictions.shape, (20,))\n        self.assertTrue(np.all(predictions == 0) or np.all(predictions == 1))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/","title":"Neural Network Regression","text":"

This module contains an implementation of Neural Network Regression, a powerful algorithm for predicting continuous outcomes based on input features.

"},{"location":"algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/#parameters","title":"Parameters","text":"
  • input_size: Number of features in the input data.
  • hidden_size: Number of neurons in the hidden layer.
  • output_size: Number of output neurons.
  • learning_rate: Step size for updating weights during training.
  • n_iterations: Number of iterations for training the neural network.
"},{"location":"algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/#scratch-code","title":"Scratch Code","text":"
  • neural_network_regression.py file
import numpy as np\n\nclass NeuralNetworkRegression:\n    def __init__(self, input_size, hidden_size, output_size, learning_rate=0.01, n_iterations=1000):\n        \"\"\"\n        Constructor for the NeuralNetworkRegression class.\n\n        Parameters:\n        - input_size: Number of input features.\n        - hidden_size: Number of neurons in the hidden layer.\n        - output_size: Number of output neurons.\n        - learning_rate: Step size for gradient descent.\n        - n_iterations: Number of iterations for gradient descent.\n        \"\"\"\n        self.input_size = input_size\n        self.hidden_size = hidden_size\n        self.output_size = output_size\n        self.learning_rate = learning_rate\n        self.n_iterations = n_iterations\n\n        # Initialize weights and biases\n        self.weights_input_hidden = np.random.rand(self.input_size, self.hidden_size)\n        self.bias_hidden = np.zeros((1, self.hidden_size))\n        self.weights_hidden_output = np.random.rand(self.hidden_size, self.output_size)\n        self.bias_output = np.zeros((1, self.output_size))\n\n    def sigmoid(self, x):\n        \"\"\"Sigmoid activation function.\"\"\"\n        return 1 / (1 + np.exp(-x))\n\n    def sigmoid_derivative(self, x):\n        \"\"\"Derivative of the sigmoid function.\"\"\"\n        return x * (1 - x)\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Neural Network model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        for _ in range(self.n_iterations):\n            # Forward pass\n            hidden_layer_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden\n            hidden_layer_output = self.sigmoid(hidden_layer_input)\n\n            output_layer_input = np.dot(hidden_layer_output, self.weights_hidden_output) + self.bias_output\n            predicted_output = self.sigmoid(output_layer_input)\n\n            # Backpropagation\n            error = y - predicted_output\n            output_delta = error * self.sigmoid_derivative(predicted_output)\n\n            hidden_layer_error = output_delta.dot(self.weights_hidden_output.T)\n            hidden_layer_delta = hidden_layer_error * self.sigmoid_derivative(hidden_layer_output)\n\n            # Update weights and biases\n            self.weights_hidden_output += hidden_layer_output.T.dot(output_delta) * self.learning_rate\n            self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * self.learning_rate\n\n            self.weights_input_hidden += X.T.dot(hidden_layer_delta) * self.learning_rate\n            self.bias_hidden += np.sum(hidden_layer_delta, axis=0, keepdims=True) * self.learning_rate\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        hidden_layer_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden\n        hidden_layer_output = self.sigmoid(hidden_layer_input)\n\n        output_layer_input = np.dot(hidden_layer_output, self.weights_hidden_output) + self.bias_output\n        predicted_output = self.sigmoid(output_layer_input)\n\n        return predicted_output\n
  • neural_network_regression_test.py file
import numpy as np\nimport unittest\nfrom NeuralNetworkRegression import NeuralNetworkRegression\n\nclass TestNeuralNetworkRegression(unittest.TestCase):\n    def setUp(self):\n        # Generate synthetic data for testing\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 3)\n        self.y_train = np.random.rand(100, 1)\n\n        self.X_test = np.random.rand(10, 3)\n\n    def test_fit_predict(self):\n        # Initialize and fit the model\n        model = NeuralNetworkRegression(input_size=3, hidden_size=4, output_size=1, learning_rate=0.01, n_iterations=1000)\n        model.fit(self.X_train, self.y_train)\n\n        # Ensure predictions have the correct shape\n        predictions = model.predict(self.X_test)\n        self.assertEqual(predictions.shape, (10, 1))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Polynomial_Regression/","title":"Polynomial Regression","text":"

This module contains an implementation of Polynomial Regression, an extension of Linear Regression that models the relationship between the independent variable and the dependent variable as a polynomial.

"},{"location":"algorithms/machine-learning/supervised/regressions/Polynomial_Regression/#parameters","title":"Parameters","text":"
  • degree: Degree of the polynomial.
  • learning_rate: The step size for gradient descent.
  • n_iterations: The number of iterations for gradient descent.
"},{"location":"algorithms/machine-learning/supervised/regressions/Polynomial_Regression/#scratch-code","title":"Scratch Code","text":"
  • polynomial_regression.py file
import numpy as np\n\n# Polynomial regression implementation\nclass PolynomialRegression:\n    def __init__(self, degree=2, learning_rate=0.01, n_iterations=1000):\n        \"\"\"\n        Constructor for the PolynomialRegression class.\n\n        Parameters:\n        - degree: Degree of the polynomial.\n        - learning_rate: The step size for gradient descent.\n        - n_iterations: The number of iterations for gradient descent.\n        \"\"\"\n        self.degree = degree\n        self.learning_rate = learning_rate\n        self.n_iterations = n_iterations\n        self.weights = None\n        self.bias = None\n\n    def _polynomial_features(self, X):\n        \"\"\"\n        Create polynomial features up to the specified degree.\n\n        Parameters:\n        - X: Input features (numpy array).\n\n        Returns:\n        - Polynomial features (numpy array).\n        \"\"\"\n        return np.column_stack([X ** i for i in range(1, self.degree + 1)])\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the polynomial regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        X_poly = self._polynomial_features(X)\n        self.weights = np.zeros((X_poly.shape[1], 1))\n        self.bias = 0\n\n        for _ in range(self.n_iterations):\n            predictions = np.dot(X_poly, self.weights) + self.bias\n            errors = predictions - y\n\n            self.weights -= self.learning_rate * (1 / len(X_poly)) * np.dot(X_poly.T, errors)\n            self.bias -= self.learning_rate * (1 / len(X_poly)) * np.sum(errors)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        X_poly = self._polynomial_features(X)\n        return np.dot(X_poly, self.weights) + self.bias\n
  • polynomial_regression_test.py file
import unittest\nimport numpy as np\nfrom PolynomialRegression import PolynomialFeatures\n\nclass TestPolynomialRegression(unittest.TestCase):\n\n    def setUp(self):\n        # Create synthetic data for testing\n        np.random.seed(42)\n        self.X_train = 2 * np.random.rand(100, 1)\n        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)\n\n    def test_fit_predict(self):\n        # Test the fit and predict methods\n        poly_model = PolynomialFeatures(degree=2)\n        poly_model.fit(self.X_train, self.y_train)\n\n        # Create test data\n        X_test = np.array([[1.5], [2.0]])\n\n        # Make predictions\n        predictions = poly_model.predict(X_test)\n\n        # Assert that the predictions are NumPy arrays\n        self.assertTrue(isinstance(predictions, np.ndarray))\n\n        # Assert that the shape of predictions is as expected\n        self.assertEqual(predictions.shape, (X_test.shape[0], 1))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/","title":"Random Forest Regression","text":"

This module contains an implementation of Random Forest Regression, an ensemble learning method that combines multiple decision trees to create a more robust and accurate model for predicting continuous outcomes based on input features.

"},{"location":"algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/#parameters","title":"Parameters","text":"
  • n_trees: Number of trees in the random forest.
  • max_depth: Maximum depth of each decision tree.
  • max_features: Maximum number of features to consider for each split.
"},{"location":"algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/#scratch-code","title":"Scratch Code","text":"
  • random_forest_regression.py file
import numpy as np\n\nclass RandomForestRegression:\n\n    def __init__(self, n_trees=100, max_depth=None, max_features=None):\n        \"\"\"\n        Constructor for the RandomForestRegression class.\n\n        Parameters:\n        - n_trees: Number of trees in the random forest.\n        - max_depth: Maximum depth of each decision tree.\n        - max_features: Maximum number of features to consider for each split.\n        \"\"\"\n        self.n_trees = n_trees\n        self.max_depth = max_depth\n        self.max_features = max_features\n        self.trees = []\n\n    def _bootstrap_sample(self, X, y):\n        \"\"\"\n        Create a bootstrap sample of the dataset.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n\n        Returns:\n        - Bootstrap sample of X and y.\n        \"\"\"\n        indices = np.random.choice(len(X), len(X), replace=True)\n        return X[indices], y[indices]\n\n    def _build_tree(self, X, y, depth):\n        \"\"\"\n        Recursively build a decision tree.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        - depth: Current depth of the tree.\n\n        Returns:\n        - Node of the decision tree.\n        \"\"\"\n        if depth == self.max_depth or np.all(y == y[0]):\n            return {'value': np.mean(y)}\n\n        n_features = X.shape[1]\n        if self.max_features is None:\n            subset_features = np.arange(n_features)\n        else:\n            subset_features = np.random.choice(n_features, self.max_features, replace=False)\n\n        # Create a random subset of features for this tree\n        X_subset = X[:, subset_features]\n\n        # Create a bootstrap sample\n        X_bootstrap, y_bootstrap = self._bootstrap_sample(X_subset, y)\n\n        # Find the best split using the selected subset of features\n        feature_index, threshold = self._find_best_split(X_bootstrap, y_bootstrap, subset_features)\n\n        if feature_index is not None:\n            # Split the dataset\n            X_left, X_right, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)\n\n            # Recursively build left and right subtrees\n            left_subtree = self._build_tree(X_left, y_left, depth + 1)\n            right_subtree = self._build_tree(X_right, y_right, depth + 1)\n\n            return {'feature_index': feature_index,\n                    'threshold': threshold,\n                    'left': left_subtree,\n                    'right': right_subtree}\n        else:\n            # If no split is found, return a leaf node\n            return {'value': np.mean(y)}\n\n    def _find_best_split(self, X, y, subset_features):\n        \"\"\"\n        Find the best split for a subset of features.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        - subset_features: Subset of features to consider.\n\n        Returns:\n        - Index of the best feature and the corresponding threshold.\n        \"\"\"\n        m, n = X.shape\n        best_feature_index = None\n        best_threshold = None\n        best_variance_reduction = 0\n\n        initial_variance = self._calculate_variance(y)\n\n        for feature_index in subset_features:\n            thresholds = np.unique(X[:, feature_index])\n\n            for threshold in thresholds:\n                # Split the dataset\n                _, _, y_left, y_right = self._split_dataset(X, y, feature_index, threshold)\n\n                # Calculate variance reduction\n                left_weight = len(y_left) / m\n                right_weight = len(y_right) / m\n                variance_reduction = initial_variance - (left_weight * self._calculate_variance(y_left) + right_weight * self._calculate_variance(y_right))\n\n                # Update the best split if variance reduction is greater\n                if variance_reduction > best_variance_reduction:\n                    best_feature_index = feature_index\n                    best_threshold = threshold\n                    best_variance_reduction = variance_reduction\n\n        return best_feature_index, best_threshold\n\n    def _calculate_variance(self, y):\n        \"\"\"\n        Calculate the variance of a set of target values.\n\n        Parameters:\n        - y: Target values (numpy array).\n\n        Returns:\n        - Variance of the target values.\n        \"\"\"\n        return np.var(y)\n\n    def _split_dataset(self, X, y, feature_index, threshold):\n        \"\"\"\n        Split the dataset based on a feature and threshold.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        - feature_index: Index of the feature to split on.\n        - threshold: Threshold value for the split.\n\n        Returns:\n        - Left and right subsets of the dataset.\n        \"\"\"\n        left_mask = X[:, feature_index] <= threshold\n        right_mask = ~left_mask\n        return X[left_mask], X[right_mask], y[left_mask], y[right_mask]\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Random Forest Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        self.trees = []\n        for _ in range(self.n_trees):\n            # Create a bootstrap sample for each tree\n            X_bootstrap, y_bootstrap = self._bootstrap_sample(X, y)\n\n            # Build a decision tree and add it to the forest\n            tree = self._build_tree(X_bootstrap, y_bootstrap, depth=0)\n            self.trees.append(tree)\n\n    def _predict_single(self, tree, x):\n        \"\"\"\n        Recursively predict a single data point using a decision tree.\n\n        Parameters:\n        - tree: Decision tree node.\n        - x: Input features for prediction.\n\n        Returns:\n        - Predicted value.\n        \"\"\"\n        if 'value' in tree:\n            return tree['value']\n        else:\n            if x[tree['feature_index']] <= tree['threshold']:\n                return self._predict_single(tree['left'], x)\n            else:\n                return self._predict_single(tree['right'], x)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data using the Random Forest.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        predictions = np.array([self._predict_single(tree, x) for x in X for tree in self.trees])\n        return np.mean(predictions.reshape(-1, len(self.trees)), axis=1)\n
  • random_forest_regression_test.py file
import unittest\nimport numpy as np\nfrom RandomForestRegressor import RandomForestRegression\n\nclass TestRandomForestRegressor(unittest.TestCase):\n    def setUp(self):\n        # Create sample data for testing\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 2)\n        self.y_train = 2 * self.X_train[:, 0] + 3 * self.X_train[:, 1] + np.random.normal(0, 0.1, 100)\n\n        self.X_test = np.random.rand(10, 2)\n\n    def test_fit_predict(self):\n        # Test if the model can be fitted and predictions are made\n        rfr_model = RandomForestRegression(n_trees=5, max_depth=3, max_features=2)\n        rfr_model.fit(self.X_train, self.y_train)\n\n        # Ensure predictions are made without errors\n        predictions = rfr_model.predict(self.X_test)\n\n        # Add your specific assertions based on the expected behavior of your model\n        self.assertIsInstance(predictions, np.ndarray)\n        self.assertEqual(predictions.shape, (10,))\n\n    # Add more test cases as needed\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Ridge_Regression/","title":"Ridge Regression","text":"

This module contains an implementation of Ridge Regression, a linear regression variant that includes regularization to prevent overfitting.

"},{"location":"algorithms/machine-learning/supervised/regressions/Ridge_Regression/#overview","title":"Overview","text":"

Ridge Regression is a linear regression technique with an added regularization term to handle multicollinearity and prevent the model from becoming too complex.

"},{"location":"algorithms/machine-learning/supervised/regressions/Ridge_Regression/#parameters","title":"Parameters","text":"
  • alpha: Regularization strength. A higher alpha increases the penalty for large coefficients.
"},{"location":"algorithms/machine-learning/supervised/regressions/Ridge_Regression/#scratch-code","title":"Scratch Code","text":"
  • ridge_regression.py file
import numpy as np\n\nclass RidgeRegression:\n    def __init__(self, alpha=1.0):\n        \"\"\"\n        Constructor for the Ridge Regression class.\n\n        Parameters:\n        - alpha: Regularization strength. Higher values specify stronger regularization.\n        \"\"\"\n        self.alpha = alpha\n        self.weights = None\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Ridge Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        # Add a column of ones to the input features for the bias term\n        X_bias = np.c_[np.ones(X.shape[0]), X]\n\n        # Compute the closed-form solution for Ridge Regression\n        identity_matrix = np.identity(X_bias.shape[1])\n        self.weights = np.linalg.inv(X_bias.T @ X_bias + self.alpha * identity_matrix) @ X_bias.T @ y\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        # Add a column of ones to the input features for the bias term\n        X_bias = np.c_[np.ones(X.shape[0]), X]\n\n        # Make predictions using the learned weights\n        predictions = X_bias @ self.weights\n\n        return predictions\n
  • ridge_regression_test.py file
import numpy as np\nimport unittest\nfrom RidgeRegression import RidgeRegression  # Assuming your RidgeRegression class is in a separate file\n\nclass TestRidgeRegression(unittest.TestCase):\n    def test_fit_predict(self):\n        # Generate synthetic data for testing\n        np.random.seed(42)\n        X_train = np.random.rand(100, 2)\n        y_train = 3 * X_train[:, 0] + 5 * X_train[:, 1] + 2 + 0.1 * np.random.randn(100)\n        X_test = np.random.rand(20, 2)\n\n        # Create a Ridge Regression model\n        ridge_model = RidgeRegression(alpha=0.1)\n\n        # Fit the model to training data\n        ridge_model.fit(X_train, y_train)\n\n        # Make predictions on test data\n        predictions = ridge_model.predict(X_test)\n\n        # Ensure the predictions have the correct shape\n        self.assertEqual(predictions.shape, (20,))\n\n    def test_invalid_alpha(self):\n        # Check if an exception is raised for an invalid alpha value\n        with self.assertRaises(ValueError):\n            RidgeRegression(alpha=-1)\n\n    # Add more test cases as needed\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/","title":"Support Vector Regression","text":"

This module contains an implementation of Support Vector Regression (SVR), a regression technique using Support Vector Machines (SVM) principles.

"},{"location":"algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/#parameters","title":"Parameters","text":"
  • epsilon: Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function.
  • C: Regularization parameter. The strength of the regularization is inversely proportional to C.
"},{"location":"algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/#scratch-code","title":"Scratch Code","text":"
  • support_vector_regression.py file
import numpy as np\n\nclass SupportVectorRegression:\n\n    def __init__(self, epsilon=0.1, C=1.0):\n        \"\"\"\n        Constructor for the SupportVectorRegression class.\n\n        Parameters:\n        - epsilon: Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function.\n        - C: Regularization parameter. The strength of the regularization is inversely proportional to C.\n        \"\"\"\n        self.epsilon = epsilon\n        self.C = C\n        self.weights = None\n        self.bias = None\n\n    def _linear_kernel(self, X1, X2):\n        \"\"\"\n        Linear kernel function.\n\n        Parameters:\n        - X1, X2: Input data (numpy arrays).\n\n        Returns:\n        - Linear kernel result (numpy array).\n        \"\"\"\n        return np.dot(X1, X2.T)\n\n    def _compute_kernel_matrix(self, X):\n        \"\"\"\n        Compute the kernel matrix for the linear kernel.\n\n        Parameters:\n        - X: Input data (numpy array).\n\n        Returns:\n        - Kernel matrix (numpy array).\n        \"\"\"\n        m = X.shape[0]\n        kernel_matrix = np.zeros((m, m))\n\n        for i in range(m):\n            for j in range(m):\n                kernel_matrix[i, j] = self._linear_kernel(X[i, :], X[j, :])\n\n        return kernel_matrix\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Support Vector Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        m, n = X.shape\n\n        # Create the kernel matrix\n        kernel_matrix = self._compute_kernel_matrix(X)\n\n        # Quadratic programming problem coefficients\n        P = np.vstack([np.hstack([kernel_matrix, -kernel_matrix]),\n                       np.hstack([-kernel_matrix, kernel_matrix])])\n        q = np.vstack([self.epsilon * np.ones((m, 1)) - y, self.epsilon * np.ones((m, 1)) + y])\n\n        # Constraints matrix\n        G = np.vstack([np.eye(2 * m), -np.eye(2 * m)])\n        h = np.vstack([self.C * np.ones((2 * m, 1)), np.zeros((2 * m, 1))])\n\n        # Solve the quadratic programming problem\n        solution = np.linalg.solve(P, q)\n\n        # Extract weights and bias\n        self.weights = solution[:n]\n        self.bias = solution[n]\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        predictions = np.dot(X, self.weights) + self.bias\n        return predictions\n
  • support_vector_regression_test.py file
import unittest\nimport numpy as np\nfrom SVR import SupportVectorRegression\n\nclass TestSupportVectorRegression(unittest.TestCase):\n\n    def setUp(self):\n        # Create synthetic data for testing\n        np.random.seed(42)\n        self.X_train = 2 * np.random.rand(100, 1)\n        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)\n\n    def test_fit_predict(self):\n        # Test the fit and predict methods\n        svr_model = SupportVectorRegression(epsilon=0.1, C=1.0)\n        svr_model.fit(self.X_train, self.y_train)\n\n        # Create test data\n        X_test = np.array([[1.5], [2.0]])\n\n        # Make predictions\n        predictions = svr_model.predict(X_test)\n\n        # Assert that the predictions are NumPy arrays\n        self.assertTrue(isinstance(predictions, np.ndarray))\n\n        # Assert that the shape of predictions is as expected\n        self.assertEqual(predictions.shape, (X_test.shape[0], 1))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/","title":"XG Boost Regression","text":"

This module contains an implementation of the XGBoost Regressor, a popular ensemble learning algorithm that combines the predictions from multiple decision trees to create a more robust and accurate model for regression tasks.

"},{"location":"algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/#parameters","title":"Parameters","text":"
  • n_estimators: Number of boosting rounds (trees).
  • learning_rate: Step size shrinkage to prevent overfitting.
  • max_depth: Maximum depth of each tree.
  • gamma: Minimum loss reduction required to make a further partition.
"},{"location":"algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/#scratch-code","title":"Scratch Code","text":"
  • x_g_boost_regression.py file
import numpy as np\nfrom sklearn.tree import DecisionTreeRegressor\n\nclass XGBoostRegressor:\n\n    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3, gamma=0):\n        \"\"\"\n        Constructor for the XGBoostRegressor class.\n\n        Parameters:\n        - n_estimators: Number of boosting rounds (trees).\n        - learning_rate: Step size shrinkage to prevent overfitting.\n        - max_depth: Maximum depth of each tree.\n        - gamma: Minimum loss reduction required to make a further partition.\n        \"\"\"\n        self.n_estimators = n_estimators\n        self.learning_rate = learning_rate\n        self.max_depth = max_depth\n        self.gamma = gamma\n        self.trees = []\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the XGBoost model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        # Initialize residuals\n        residuals = np.copy(y)\n\n        for _ in range(self.n_estimators):\n            # Fit a weak learner (decision tree) to the residuals\n            tree = DecisionTreeRegressor(max_depth=self.max_depth, min_samples_split=self.gamma)\n            tree.fit(X, residuals)\n\n            # Compute predictions from the weak learner\n            predictions = tree.predict(X)\n\n            # Update residuals with the weighted sum of previous residuals and predictions\n            residuals -= self.learning_rate * predictions\n\n            # Store the tree in the list\n            self.trees.append(tree)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        # Initialize predictions with zeros\n        predictions = np.zeros(X.shape[0])\n\n        # Make predictions using each tree and update the overall prediction\n        for tree in self.trees:\n            predictions += self.learning_rate * tree.predict(X)\n\n        return predictions\n
  • x_g_boost_regression_test.py file
import unittest\nimport numpy as np\nfrom XGBoostRegressor import XGBoostRegressor\n\nclass TestXGBoostRegressor(unittest.TestCase):\n\n    def setUp(self):\n        # Generate synthetic data for testing\n        np.random.seed(42)\n        self.X_train = np.random.rand(100, 5)\n        self.y_train = np.random.rand(100)\n        self.X_test = np.random.rand(20, 5)\n\n    def test_fit_predict(self):\n        # Test the fit and predict methods\n        xgb_model = XGBoostRegressor(n_estimators=50, learning_rate=0.1, max_depth=3, gamma=0.1)\n        xgb_model.fit(self.X_train, self.y_train)\n        predictions = xgb_model.predict(self.X_test)\n\n        # Ensure predictions have the correct shape\n        self.assertEqual(predictions.shape, (20,))\n\n    def test_invalid_parameters(self):\n        # Test invalid parameter values\n        with self.assertRaises(ValueError):\n            XGBoostRegressor(n_estimators=-1, learning_rate=0.1, max_depth=3, gamma=0.1)\n\n        with self.assertRaises(ValueError):\n            XGBoostRegressor(n_estimators=50, learning_rate=-0.1, max_depth=3, gamma=0.1)\n\n        with self.assertRaises(ValueError):\n            XGBoostRegressor(n_estimators=50, learning_rate=0.1, max_depth=-3, gamma=0.1)\n\n    def test_invalid_fit(self):\n        # Test fitting with mismatched X_train and y_train shapes\n        xgb_model = XGBoostRegressor(n_estimators=50, learning_rate=0.1, max_depth=3, gamma=0.1)\n        with self.assertRaises(ValueError):\n            xgb_model.fit(self.X_train, np.random.rand(50))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/bayesian/","title":"Bayesian Regression","text":"

This module contains an implementation of Bayesian Regression, a probabilistic approach to linear regression that provides uncertainty estimates for predictions.

"},{"location":"algorithms/machine-learning/supervised/regressions/bayesian/#overview","title":"Overview","text":"

Bayesian Regression is an extension of traditional linear regression that models the distribution of coefficients, allowing for uncertainty in the model parameters. It's particularly useful when dealing with limited data and provides a full probability distribution over the possible values of the regression coefficients.

"},{"location":"algorithms/machine-learning/supervised/regressions/bayesian/#parameters","title":"Parameters","text":"
  • alpha: Prior precision for the coefficients.
  • beta: Precision of the noise in the observations.
"},{"location":"algorithms/machine-learning/supervised/regressions/bayesian/#scratch-code","title":"Scratch Code","text":"
  • bayesian_regression.py file
import numpy as np\n\nclass BayesianRegression:\n    def __init__(self, alpha=1, beta=1):\n        \"\"\"\n        Constructor for the BayesianRegression class.\n\n        Parameters:\n        - alpha: Prior precision.\n        - beta: Noise precision.\n        \"\"\"\n        self.alpha = alpha\n        self.beta = beta\n        self.w_mean = None\n        self.w_precision = None\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the Bayesian Regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        # Add a bias term to X\n        X = np.c_[np.ones(X.shape[0]), X]\n\n        # Compute posterior precision and mean\n        self.w_precision = self.alpha * np.eye(X.shape[1]) + self.beta * X.T @ X\n        self.w_mean = self.beta * np.linalg.solve(self.w_precision, X.T @ y)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        # Add a bias term to X\n        X = np.c_[np.ones(X.shape[0]), X]\n\n        # Compute predicted mean\n        y_pred = X @ self.w_mean\n\n        return y_pred\n
  • bayesian_regression_test.py file
import unittest\nimport numpy as np\nfrom BayesianRegression import BayesianRegression\n\nclass TestBayesianRegression(unittest.TestCase):\n    def setUp(self):\n        # Generate synthetic data for testing\n        np.random.seed(42)\n        self.X_train = 2 * np.random.rand(100, 1)\n        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)\n\n        self.X_test = 2 * np.random.rand(20, 1)\n\n    def test_fit_predict(self):\n        blr = BayesianRegression()\n        blr.fit(self.X_train, self.y_train)\n        y_pred = blr.predict(self.X_test)\n\n        self.assertTrue(y_pred.shape == (20, 1))\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/supervised/regressions/linear/","title":"Linear Regression","text":"

This module contains an implementation of the Linear Regression algorithm, a fundamental technique in machine learning for predicting a continuous outcome based on input features.

"},{"location":"algorithms/machine-learning/supervised/regressions/linear/#parameters","title":"Parameters","text":"
  • learning_rate: The step size for gradient descent.
  • n_iterations: The number of iterations for gradient descent.
"},{"location":"algorithms/machine-learning/supervised/regressions/linear/#scratch-code","title":"Scratch Code","text":"
  • linear_regression.py file
import numpy as np\n\n# Linear regression implementation\nclass LinearRegression:\n    def __init__(self, learning_rate=0.01, n_iterations=1000):\n        \"\"\"\n        Constructor for the LinearRegression class.\n\n        Parameters:\n        - learning_rate: The step size for gradient descent.\n        - n_iterations: The number of iterations for gradient descent.\n        - n_iterations: n_epochs.\n        \"\"\"\n        self.learning_rate = learning_rate\n        self.n_iterations = n_iterations\n        self.weights = None\n        self.bias = None\n\n    def fit(self, X, y):\n        \"\"\"\n        Fit the linear regression model to the input data.\n\n        Parameters:\n        - X: Input features (numpy array).\n        - y: Target values (numpy array).\n        \"\"\"\n        # Initialize weights and bias\n        self.weights = np.zeros((X.shape[1], 1))\n        self.bias = 0\n\n        # Gradient Descent\n        for _ in range(self.n_iterations):\n            # Compute predictions\n            predictions = np.dot(X, self.weights) + self.bias\n\n            # Calculate errors\n            errors = predictions - y\n\n            # Update weights and bias\n            self.weights -= self.learning_rate * (1 / len(X)) * np.dot(X.T, errors)\n            self.bias -= self.learning_rate * (1 / len(X)) * np.sum(errors)\n\n    def predict(self, X):\n        \"\"\"\n        Make predictions on new data.\n\n        Parameters:\n        - X: Input features for prediction (numpy array).\n\n        Returns:\n        - Predicted values (numpy array).\n        \"\"\"\n        return np.dot(X, self.weights) + self.bias\n
  • linear_regression_test.py file
import unittest\nimport numpy as np\nfrom LinearRegression import LinearRegression\n\nclass TestLinearRegression(unittest.TestCase):\n\n    def setUp(self):\n        # Set up some common data for testing\n        np.random.seed(42)\n        self.X_train = 2 * np.random.rand(100, 1)\n        self.y_train = 4 + 3 * self.X_train + np.random.randn(100, 1)\n\n        self.X_test = 2 * np.random.rand(20, 1)\n        self.y_test = 4 + 3 * self.X_test + np.random.randn(20, 1)\n\n    def test_fit_predict(self):\n        # Test the fit and predict methods\n\n        # Create a LinearRegression model\n        lr_model = LinearRegression()\n\n        # Fit the model to the training data\n        lr_model.fit(self.X_train, self.y_train)\n\n        # Make predictions on the test data\n        predictions = lr_model.predict(self.X_test)\n\n        # Check that the predictions are of the correct shape\n        self.assertEqual(predictions.shape, self.y_test.shape)\n\n    def test_predict_with_unfitted_model(self):\n        # Test predicting with an unfitted model\n\n        # Create a LinearRegression model (not fitted)\n        lr_model = LinearRegression()\n\n        # Attempt to make predictions without fitting the model\n        with self.assertRaises(ValueError):\n            _ = lr_model.predict(self.X_test)\n\nif __name__ == '__main__':\n    unittest.main()\n
"},{"location":"algorithms/machine-learning/unsupervised/","title":"Unsupervised Machine Learning \ud83e\udd16","text":"Clustering

Grouping data to uncover hidden patterns.

Dimensionality Reduction

Simplifying data while preserving its essence.

"},{"location":"algorithms/machine-learning/unsupervised/clustering/","title":"Clustering Algorithms \ud83e\udd16","text":"KMeans Clustering

Unveiling hidden patterns by grouping data into cohesive clusters.

\ud83d\udcc5 2025-01-19 | \u23f1\ufe0f 3 mins

"},{"location":"algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/","title":"K Means Clustering","text":"

Overview: K-means clustering is an unsupervised machine learning algorithm for grouping similar data points together into clusters based on their features.

"},{"location":"algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/#advantages-of-k-means","title":"Advantages of K-means:","text":"
  • Simple and Easy to implement
  • Efficiency: K-means is computationally efficient and can handle large datasets with high dimensionality.
  • Flexibility: K-means offers flexibility as it can be easily customized for different applications, allowing the use of various distance metrics and initialization techniques.
  • Scalability: K-means can handle large datasets with many data points

How K-means Works (Scratch Implementation Guide):

"},{"location":"algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/#algorithm-overview","title":"Algorithm Overview:","text":"
  1. Initialization:
  2. Choose k initial centroids randomly from the dataset.

  3. Iterative Process:

  4. Assign Data Points: For each data point, calculate the Euclidean distance to all centroids and assign the data point to the nearest centroid.
  5. Update Centroids: Recalculate the centroids by averaging the data points assigned to each cluster.
  6. Check for Convergence: If the centroids do not change significantly between iterations (i.e., they converge), stop. Otherwise, repeat the process.

  7. Termination:

  8. The algorithm terminates either when the centroids have converged or when the maximum number of iterations is reached.

  9. Output:

  10. The final cluster assignments for each data point.
"},{"location":"algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/#parameters","title":"Parameters","text":"
  • num_clusters: Number of clusters to form.
  • max_iterations: Maximum number of iterations before stopping.
  • show_steps: Whether to visualize the clustering process step by step (Boolean).
"},{"location":"algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/#scratch-code","title":"Scratch Code","text":"
  • kmeans_scratch.py file
import numpy as np\nimport matplotlib.pyplot as plt\n\ndef euclidean_distance(point1, point2):\n    \"\"\"\n    Calculate the Euclidean distance between two points in space.\n    \"\"\"\n    return np.sqrt(np.sum((point1 - point2) ** 2))\n\nclass KMeansClustering:\n    def __init__(self, num_clusters=5, max_iterations=100, show_steps=False):\n        \"\"\"\n        Initialize the KMeans clustering model with the following parameters:\n        - num_clusters: Number of clusters we want to form\n        - max_iterations: Maximum number of iterations for the algorithm\n        - show_steps: Boolean flag to visualize the clustering process step by step\n        \"\"\"\n        self.num_clusters = num_clusters\n        self.max_iterations = max_iterations\n        self.show_steps = show_steps\n        self.clusters = [[] for _ in range(self.num_clusters)]  # Initialize empty clusters\n        self.centroids = []  # List to store the centroids of clusters\n\n    def fit_predict(self, data):\n        \"\"\"\n        Fit the KMeans model on the data and predict the cluster labels for each data point.\n        \"\"\"\n        self.data = data\n        self.num_samples, self.num_features = data.shape  # Get number of samples and features\n        initial_sample_indices = np.random.choice(self.num_samples, self.num_clusters, replace=False)\n        self.centroids = [self.data[idx] for idx in initial_sample_indices]\n\n        for _ in range(self.max_iterations):\n            # Step 1: Assign each data point to the closest centroid to form clusters\n            self.clusters = self._assign_to_clusters(self.centroids)\n            if self.show_steps:\n                self._plot_clusters()\n\n            # Step 2: Calculate new centroids by averaging the data points in each cluster\n            old_centroids = self.centroids\n            self.centroids = self._calculate_new_centroids(self.clusters)\n\n            # Step 3: Check for convergence \n            if self._has_converged(old_centroids, self.centroids):\n                break\n            if self.show_steps:\n                self._plot_clusters()\n\n        return self._get_cluster_labels(self.clusters)\n\n    def _assign_to_clusters(self, centroids):\n        \"\"\"\n        Assign each data point to the closest centroid based on Euclidean distance.\n        \"\"\"\n        clusters = [[] for _ in range(self.num_clusters)]\n        for sample_idx, sample in enumerate(self.data):\n            closest_centroid_idx = self._find_closest_centroid(sample, centroids)\n            clusters[closest_centroid_idx].append(sample_idx)\n        return clusters\n\n    def _find_closest_centroid(self, sample, centroids):\n        \"\"\"\n        Find the index of the closest centroid to the given data point (sample).\n        \"\"\"\n        distances = [euclidean_distance(sample, centroid) for centroid in centroids]\n        closest_idx = np.argmin(distances)  # Index of the closest centroid\n        return closest_idx\n\n    def _calculate_new_centroids(self, clusters):\n        \"\"\"\n        Calculate new centroids by averaging the data points in each cluster.\n        \"\"\"\n        centroids = np.zeros((self.num_clusters, self.num_features))\n        for cluster_idx, cluster in enumerate(clusters):\n            cluster_mean = np.mean(self.data[cluster], axis=0)\n            centroids[cluster_idx] = cluster_mean\n        return centroids\n\n    def _has_converged(self, old_centroids, new_centroids):\n        \"\"\"\n        Check if the centroids have converged \n        \"\"\"\n        distances = [euclidean_distance(old_centroids[i], new_centroids[i]) for i in range(self.num_clusters)]\n        return sum(distances) == 0  # If centroids haven't moved, they are converged\n\n    def _get_cluster_labels(self, clusters):\n        \"\"\"\n        Get the cluster labels for each data point based on the final clusters.\n        \"\"\"\n        labels = np.empty(self.num_samples)\n        for cluster_idx, cluster in enumerate(clusters):\n            for sample_idx in cluster:\n                labels[sample_idx] = cluster_idx\n        return labels\n\n    def _plot_clusters(self):\n        \"\"\"\n        Visualize the clusters and centroids in a 2D plot using matplotlib.\n        \"\"\"\n        fig, ax = plt.subplots(figsize=(12, 8))\n        for i, cluster in enumerate(self.clusters):\n            cluster_points = self.data[cluster]\n            ax.scatter(cluster_points[:, 0], cluster_points[:, 1])\n\n        for centroid in self.centroids:\n            ax.scatter(centroid[0], centroid[1], marker=\"x\", color=\"black\", linewidth=2)\n\n        plt.show()\n
  • test_kmeans.py file

```py import unittest import numpy as np from kmeans_scratch import KMeansClustering

class TestKMeansClustering(unittest.TestCase):

def setUp(self):\n    np.random.seed(42)\n    self.X_train = np.vstack([\n        np.random.randn(100, 2) + np.array([5, 5]),\n        np.random.randn(100, 2) + np.array([-5, -5]),\n        np.random.randn(100, 2) + np.array([5, -5]),\n        np.random.randn(100, 2) + np.array([-5, 5])\n    ])\n\ndef test_kmeans(self):\n    \"\"\"Test the basic KMeans clustering functionality\"\"\"\n    kmeans = KMeansClustering(num_clusters=4, max_iterations=100, show_steps=False)\n\n    cluster_labels = kmeans.fit_predict(self.X_train)\n\n    unique_labels = np.unique(cluster_labels)\n    self.assertEqual(len(unique_labels), 4)  \n    self.assertEqual(cluster_labels.shape, (self.X_train.shape[0],))  \n    print(\"Cluster labels for the data points:\")\n    print(cluster_labels)\n

if name == 'main': unittest.main()

"},{"location":"algorithms/machine-learning/unsupervised/dimensionality-reduction/","title":"Dimensionality Reduction \ud83e\udd16","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/natural-language-processing/","title":"Natural Language Processing \ud83d\udde3\ufe0f","text":"Bag Of Words

Representation of text that is based on an unordered collection.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 3 mins

Fast Text

From Facebook AI Research(FAIR) for learning word embeddings and word classifications.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 7 mins

Gloabl Vectors

Unsupervised learning algorithm for obtaining vector representations for words.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

NLP Introduction

Enables computers to comprehend, generate, and manipulate human language.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 3 mins

NLTK Setup

Working with human language data.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 2 mins

Text Pre-Processing Techniques

Cleaning and preparing raw text data for further analysis or model training.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

Term Frequency-Inverse Document Frequency

Measure of importance of a word to a document.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 3 mins

Transformers

Deep neural network architecture.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 4 mins

Word2Vec

Creates vector representations of words.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 3 mins

Word Embeddings

Numeric representations of words in a lower-dimensional space.

\ud83d\udcc5 2025-01-15 | \u23f1\ufe0f 3 mins

"},{"location":"algorithms/natural-language-processing/Bag_Of_Words/","title":"Bag Of Words","text":"
import re\nimport pandas as pd\nfrom sklearn.feature_extraction.text import CountVectorizer,TfidfTransformer\n\n#data collection\ndata = [\n    'Fashion is an art form and expression.',\n    'Style is a way to say who you are without having to speak.',\n    'Fashion is what you buy, style is what you do with it.',\n    'With fashion, you convey a message about yourself without uttering a single word'\n]\n\n#text processing\n\ndef preprocess_text(text):\n    text = text.lower()\n    text = re.sub(r'[^a-zs]',' ',text)\n    return text\n\npreprocessed_data = [preprocess_text(doc) for doc in data]\n\nfor i, doc in enumerate(preprocessed_data, 1):\n    print(f'Data-{i} {doc}')\n\n\n\n# removing words like the, is, are, and as they usually do not carry much useful information for the analysis.\nvectorizer = CountVectorizer(stop_words='english')\nX=vectorizer.fit_transform(preprocessed_data)\nWord=vectorizer.get_feature_names_out()\n\nbow_df = pd.DataFrame(X.toarray(),columns=Word)\nbow_df.index =[f'Data {i}' for i in range(1, len(data) + 1)]\n\ntfidf_transformer = TfidfTransformer()\nX_tfidf=tfidf_transformer.fit_transform(X)\ntfidf_df=pd.DataFrame(X_tfidf.toarray(), columns=Word)\ntfidf_df.index=[f'Data {i}' for i in range(1, len(data) + 1)]\n\n\nprint()\nprint(\"--------------------------------BoW Represention----------------------------\")\nprint(bow_df)\n\nprint()\nprint(\"--------------------------------TF-IDF Value----------------------------\")\nprint(tfidf_df)\n
"},{"location":"algorithms/natural-language-processing/Fast_Text/","title":"Fast Text","text":""},{"location":"algorithms/natural-language-processing/Fast_Text/#introduction","title":"Introduction","text":"

The FastText class implements a word representation and classification tool developed by Facebook's AI Research (FAIR) lab. FastText extends the Word2Vec model by representing each word as a bag of character n-grams. This approach helps capture subword information and improves the handling of rare words.

"},{"location":"algorithms/natural-language-processing/Fast_Text/#explanation","title":"Explanation","text":""},{"location":"algorithms/natural-language-processing/Fast_Text/#initialization","title":"Initialization","text":"
  • vocab_size: Size of the vocabulary.
  • embedding_dim: Dimension of the word embeddings.
  • n_gram_size: Size of character n-grams.
  • learning_rate: Learning rate for updating embeddings.
  • epochs: Number of training epochs.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#building-vocabulary","title":"Building Vocabulary","text":"
  • build_vocab(): Constructs the vocabulary from the input sentences and creates a reverse mapping of words to indices.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#generating-n-grams","title":"Generating N-grams","text":"
  • get_ngrams(): Generates character n-grams for a given word. It pads the word with < and > symbols to handle edge cases effectively.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#training","title":"Training","text":"
  • train(): Updates word and context embeddings using a simple Stochastic Gradient Descent (SGD) approach. The loss is computed as the squared error between the predicted and actual values.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#prediction","title":"Prediction","text":"
  • predict(): Calculates the dot product between the target word and context embeddings to predict word vectors.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#getting-word-vectors","title":"Getting Word Vectors","text":"
  • get_word_vector(): Retrieves the embedding for a specific word from the trained model.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#normalization","title":"Normalization","text":"
  • get_embedding_matrix(): Returns the normalized embedding matrix for better performance and stability.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#advantages","title":"Advantages","text":"
  • Subword Information: FastText captures morphological details by using character n-grams, improving handling of rare and out-of-vocabulary words.
  • Improved Representations: The use of subwords allows for better word representations, especially for languages with rich morphology.
  • Efficiency: FastText is designed to handle large-scale datasets efficiently, with optimizations for both training and inference.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#applications","title":"Applications","text":"
  • Natural Language Processing (NLP): FastText embeddings are used in tasks like text classification, sentiment analysis, and named entity recognition.
  • Information Retrieval: Enhances search engines by providing more nuanced semantic matching between queries and documents.
  • Machine Translation: Improves translation models by leveraging subword information for better handling of rare words and phrases.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#implementation","title":"Implementation","text":""},{"location":"algorithms/natural-language-processing/Fast_Text/#preprocessing","title":"Preprocessing","text":"
  1. Initialization: Set up parameters such as vocabulary size, embedding dimension, n-gram size, learning rate, and number of epochs.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#building-vocabulary_1","title":"Building Vocabulary","text":"
  1. Build Vocabulary: Construct the vocabulary from the input sentences and create a mapping for words.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#generating-n-grams_1","title":"Generating N-grams","text":"
  1. Generate N-grams: Create character n-grams for each word in the vocabulary, handling edge cases with padding.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#training_1","title":"Training","text":"
  1. Train the Model: Use SGD to update word and context embeddings based on the training data.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#prediction_1","title":"Prediction","text":"
  1. Predict Word Vectors: Calculate the dot product between target and context embeddings to predict word vectors.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#getting-word-vectors_1","title":"Getting Word Vectors","text":"
  1. Retrieve Word Vectors: Extract the embedding for a specific word from the trained model.
"},{"location":"algorithms/natural-language-processing/Fast_Text/#normalization_1","title":"Normalization","text":"
  1. Normalize Embeddings: Return the normalized embedding matrix for stability and improved performance.

For more advanced implementations, consider using optimized libraries like the FastText library by Facebook or other frameworks that offer additional features and efficiency improvements.

"},{"location":"algorithms/natural-language-processing/Fast_Text/#code","title":"Code","text":"
  • main.py
from fasttext import FastText\n\n# Example sentences\nsentences = [\n    \"fast text is a library for efficient text classification\",\n    \"word embeddings are useful for NLP tasks\",\n    \"fasttext models can handle out-of-vocabulary words\"\n]\n\n# Initialize and train FastText model\nfasttext_model = FastText(vocab_size=100, embedding_dim=50)\nfasttext_model.build_vocab(sentences)\nfasttext_model.train(sentences)\n\n# Get the vector for a word\nvector = fasttext_model.get_word_vector(\"fast\")\nprint(f\"Vector for 'fast': {vector}\")\n
  • fast_test.py
import numpy as np\nfrom collections import defaultdict\nfrom sklearn.preprocessing import normalize\n\nclass FastText:\n    def __init__(self, vocab_size, embedding_dim, n_gram_size=3, learning_rate=0.01, epochs=10):\n        self.vocab_size = vocab_size\n        self.embedding_dim = embedding_dim\n        self.n_gram_size = n_gram_size\n        self.learning_rate = learning_rate\n        self.epochs = epochs\n        self.word_embeddings = np.random.uniform(-0.1, 0.1, (vocab_size, embedding_dim))\n        self.context_embeddings = np.random.uniform(-0.1, 0.1, (vocab_size, embedding_dim))\n        self.vocab = {}\n        self.rev_vocab = {}\n\n    def build_vocab(self, sentences):\n        \"\"\"\n        Build vocabulary from sentences.\n\n        Args:\n        sentences (list): List of sentences (strings).\n        \"\"\"\n        word_count = defaultdict(int)\n        for sentence in sentences:\n            words = sentence.split()\n            for word in words:\n                word_count[word] += 1\n        self.vocab = {word: idx for idx, (word, _) in enumerate(word_count.items())}\n        self.rev_vocab = {idx: word for word, idx in self.vocab.items()}\n\n    def get_ngrams(self, word):\n        \"\"\"\n        Get n-grams for a given word.\n\n        Args:\n        word (str): Input word.\n\n        Returns:\n        set: Set of n-grams.\n        \"\"\"\n        ngrams = set()\n        word = '<' * (self.n_gram_size - 1) + word + '>' * (self.n_gram_size - 1)\n        for i in range(len(word) - self.n_gram_size + 1):\n            ngrams.add(word[i:i + self.n_gram_size])\n        return ngrams\n\n    def train(self, sentences):\n        \"\"\"\n        Train the FastText model using the given sentences.\n\n        Args:\n        sentences (list): List of sentences (strings).\n        \"\"\"\n        for epoch in range(self.epochs):\n            loss = 0\n            for sentence in sentences:\n                words = sentence.split()\n                for i, word in enumerate(words):\n                    if word not in self.vocab:\n                        continue\n                    word_idx = self.vocab[word]\n                    target_ngrams = self.get_ngrams(word)\n                    for j in range(max(0, i - 1), min(len(words), i + 2)):\n                        if i != j and words[j] in self.vocab:\n                            context_idx = self.vocab[words[j]]\n                            prediction = self.predict(word_idx, context_idx)\n                            error = prediction - 1 if j == i + 1 else prediction\n                            loss += error**2\n                            self.word_embeddings[word_idx] -= self.learning_rate * error * self.context_embeddings[context_idx]\n                            self.context_embeddings[context_idx] -= self.learning_rate * error * self.word_embeddings[word_idx]\n            print(f'Epoch {epoch + 1}/{self.epochs}, Loss: {loss}')\n\n    def predict(self, word_idx, context_idx):\n        \"\"\"\n        Predict the dot product of the word and context embeddings.\n\n        Args:\n        word_idx (int): Index of the word.\n        context_idx (int): Index of the context word.\n\n        Returns:\n        float: Dot product.\n        \"\"\"\n        return np.dot(self.word_embeddings[word_idx], self.context_embeddings[context_idx])\n\n    def get_word_vector(self, word):\n        \"\"\"\n        Get the word vector for the specified word.\n\n        Args:\n        word (str): Input word.\n\n        Returns:\n        np.ndarray: Word vector.\n        \"\"\"\n        if word in self.vocab:\n            return self.word_embeddings[self.vocab[word]]\n        else:\n            raise ValueError(f\"Word '{word}' not found in vocabulary\")\n\n    def get_embedding_matrix(self):\n        \"\"\"\n        Get the normalized embedding matrix.\n\n        Returns:\n        np.ndarray: Normalized word embeddings.\n        \"\"\"\n        return normalize(self.word_embeddings, axis=1)\n
"},{"location":"algorithms/natural-language-processing/Fast_Text/#references","title":"References","text":"
  1. FastText - Facebook AI Research
  2. Understanding FastText
  3. FastText on GitHub
"},{"location":"algorithms/natural-language-processing/GloVe/","title":"GloVe","text":""},{"location":"algorithms/natural-language-processing/GloVe/#introduction","title":"Introduction","text":"

The GloVe class implements the Global Vectors for Word Representation algorithm, developed by Stanford researchers. GloVe generates dense vector representations of words, capturing semantic relationships between them. Unlike traditional one-hot encoding, GloVe produces low-dimensional, continuous vectors that convey meaningful information about words and their contexts.

"},{"location":"algorithms/natural-language-processing/GloVe/#key-concepts","title":"Key Concepts","text":"
  • Co-occurrence Matrix: GloVe starts by creating a co-occurrence matrix from a large corpus of text. This matrix counts how often words appear together within a given context window, capturing the frequency of word pairs.

  • Weighted Least Squares: The main idea behind GloVe is to factorize this co-occurrence matrix to find word vectors that capture the relationships between words. It aims to represent words that frequently appear together in similar contexts with similar vectors.

  • Weighting Function: To ensure that the optimization process doesn't get overwhelmed by very frequent co-occurrences, GloVe uses a weighting function. This function reduces the influence of extremely common word pairs.

  • Training Objective: The goal is to adjust the word vectors so that their dot products align with the observed co-occurrence counts. This helps in capturing the similarity between words based on their contexts.

"},{"location":"algorithms/natural-language-processing/GloVe/#glove-training-objective","title":"GloVe Training Objective","text":"

GloVe\u2019s training involves adjusting word vectors so that their interactions match the observed co-occurrence data. It focuses on ensuring that words appearing together often have similar vector representations.

"},{"location":"algorithms/natural-language-processing/GloVe/#advantages","title":"Advantages","text":"
  • Efficient Training: By using a global co-occurrence matrix, GloVe captures semantic relationships effectively, including long-range dependencies.
  • Meaningful Vectors: The resulting vectors can represent complex relationships between words, such as analogies (e.g., \"king\" - \"man\" + \"woman\" \u2248 \"queen\").
  • Flexibility: GloVe vectors are versatile and can be used in various NLP tasks, including sentiment analysis and machine translation.
"},{"location":"algorithms/natural-language-processing/GloVe/#applications","title":"Applications","text":"
  • Natural Language Processing (NLP): GloVe vectors are used as features in NLP tasks like sentiment analysis, named entity recognition, and question answering.
  • Information Retrieval: Enhance search engines by providing better semantic matching between queries and documents.
  • Machine Translation: Improve translation models by capturing semantic similarities between words in different languages.
"},{"location":"algorithms/natural-language-processing/GloVe/#implementation","title":"Implementation","text":""},{"location":"algorithms/natural-language-processing/GloVe/#preprocessing","title":"Preprocessing","text":"
  1. Clean and Tokenize: Prepare the text data by cleaning and tokenizing it into words.
"},{"location":"algorithms/natural-language-processing/GloVe/#building-vocabulary","title":"Building Vocabulary","text":"
  1. Create Vocabulary: Construct a vocabulary and map words to unique indices.
"},{"location":"algorithms/natural-language-processing/GloVe/#co-occurrence-matrix","title":"Co-occurrence Matrix","text":"
  1. Build Co-occurrence Matrix: Create a matrix that captures how often each word pair appears together within a specified context.
"},{"location":"algorithms/natural-language-processing/GloVe/#glove-model","title":"GloVe Model","text":"
  1. Initialization: Set up the model parameters and hyperparameters.
  2. Weighting Function: Define how to balance the importance of different co-occurrence counts.
  3. Training: Use optimization techniques to adjust the word vectors based on the co-occurrence data.
  4. Get Word Vector: Extract the vector representation for each word from the trained model.

For more advanced implementations, consider using libraries like TensorFlow or PyTorch, which offer enhanced functionalities and optimizations.

"},{"location":"algorithms/natural-language-processing/GloVe/#code","title":"Code","text":"
  • main.py file
import numpy as np\nfrom preprocess import preprocess\nfrom vocab_and_matrix import build_vocab, build_cooccurrence_matrix\nfrom glove_model import GloVe\n\n# Example text corpus\ncorpus = [\"I love NLP\", \"NLP is a fascinating field\", \"Natural language processing with GloVe\"]\n\n# Preprocess the corpus\ntokens = [token for sentence in corpus for token in preprocess(sentence)]\n\n# Build vocabulary and co-occurrence matrix\nword_to_index = build_vocab(tokens)\ncooccurrence_matrix = build_cooccurrence_matrix(tokens, word_to_index)\n\n# Initialize and train the GloVe model\nglove = GloVe(vocab_size=len(word_to_index), embedding_dim=50)\nglove.train(cooccurrence_matrix, epochs=100)\n\n# Get the word vector for 'nlp'\nword_vector = glove.get_word_vector('nlp', word_to_index)\nprint(word_vector)\n
  • glove_model.py file
import numpy as np\n\nclass GloVe:\n    def __init__(self, vocab_size, embedding_dim=50, x_max=100, alpha=0.75):\n        self.vocab_size = vocab_size\n        self.embedding_dim = embedding_dim\n        self.x_max = x_max\n        self.alpha = alpha\n        self.W = np.random.rand(vocab_size, embedding_dim)\n        self.W_tilde = np.random.rand(vocab_size, embedding_dim)\n        self.b = np.random.rand(vocab_size)\n        self.b_tilde = np.random.rand(vocab_size)\n        self.gradsq_W = np.ones((vocab_size, embedding_dim))\n        self.gradsq_W_tilde = np.ones((vocab_size, embedding_dim))\n        self.gradsq_b = np.ones(vocab_size)\n        self.gradsq_b_tilde = np.ones(vocab_size)\n\n    def weighting_function(self, x):\n        if x < self.x_max:\n            return (x / self.x_max) ** self.alpha\n        return 1.0\n\n    def train(self, cooccurrence_matrix, epochs=100, learning_rate=0.05):\n        for epoch in range(epochs):\n            total_cost = 0\n            for i in range(self.vocab_size):\n                for j in range(self.vocab_size):\n                    if cooccurrence_matrix[i, j] == 0:\n                        continue\n                    X_ij = cooccurrence_matrix[i, j]\n                    weight = self.weighting_function(X_ij)\n                    cost = weight * (np.dot(self.W[i], self.W_tilde[j]) + self.b[i] + self.b_tilde[j] - np.log(X_ij)) ** 2\n                    total_cost += cost\n\n                    grad_common = weight * (np.dot(self.W[i], self.W_tilde[j]) + self.b[i] + self.b_tilde[j] - np.log(X_ij))\n                    grad_W = grad_common * self.W_tilde[j]\n                    grad_W_tilde = grad_common * self.W[i]\n                    grad_b = grad_common\n                    grad_b_tilde = grad_common\n\n                    self.W[i] -= learning_rate * grad_W / np.sqrt(self.gradsq_W[i])\n                    self.W_tilde[j] -= learning_rate * grad_W_tilde / np.sqrt(self.gradsq_W_tilde[j])\n                    self.b[i] -= learning_rate * grad_b / np.sqrt(self.gradsq_b[i])\n                    self.b_tilde[j] -= learning_rate * grad_b_tilde / np.sqrt(self.gradsq_b_tilde[j])\n\n                    self.gradsq_W[i] += grad_W ** 2\n                    self.gradsq_W_tilde[j] += grad_W_tilde ** 2\n                    self.gradsq_b[i] += grad_b ** 2\n                    self.gradsq_b_tilde[j] += grad_b_tilde ** 2\n\n            if epoch % 10 == 0:\n                print(f'Epoch: {epoch}, Cost: {total_cost}')\n\n    def get_word_vector(self, word, word_to_index):\n        if word in word_to_index:\n            word_index = word_to_index[word]\n            return self.W[word_index]\n        return None\n
  • preprocess.py file
import string\n\ndef preprocess(text):\n    \"\"\"\n    Preprocess the text by removing punctuation, converting to lowercase, and splitting into words.\n\n    Args:\n    text (str): Input text string.\n\n    Returns:\n    list: List of words (tokens).\n    \"\"\"\n    text = text.translate(str.maketrans('', '', string.punctuation)).lower()\n    tokens = text.split()\n    return tokens\n
  • vocab_and_matrix.py file
import numpy as np\nfrom collections import Counter\n\ndef build_vocab(tokens):\n    \"\"\"\n    Build vocabulary from tokens and create a word-to-index mapping.\n\n    Args:\n    tokens (list): List of words (tokens).\n\n    Returns:\n    dict: Word-to-index mapping.\n    \"\"\"\n    vocab = Counter(tokens)\n    word_to_index = {word: i for i, word in enumerate(vocab)}\n    return word_to_index\n\ndef build_cooccurrence_matrix(tokens, word_to_index, window_size=2):\n    \"\"\"\n    Build the co-occurrence matrix from tokens using a specified window size.\n\n    Args:\n    tokens (list): List of words (tokens).\n    word_to_index (dict): Word-to-index mapping.\n    window_size (int): Context window size.\n\n    Returns:\n    np.ndarray: Co-occurrence matrix.\n    \"\"\"\n    vocab_size = len(word_to_index)\n    cooccurrence_matrix = np.zeros((vocab_size, vocab_size))\n\n    for i, word in enumerate(tokens):\n        word_index = word_to_index[word]\n        context_start = max(0, i - window_size)\n        context_end = min(len(tokens), i + window_size + 1)\n\n        for j in range(context_start, context_end):\n            if i != j:\n                context_word = tokens[j]\n                context_word_index = word_to_index[context_word]\n                cooccurrence_matrix[word_index, context_word_index] += 1\n\n    return cooccurrence_matrix\n
"},{"location":"algorithms/natural-language-processing/GloVe/#references","title":"References","text":"
  1. GloVe - Stanford NLP
  2. Understanding GloVe
  3. GloVe: Global Vectors for Word Representation - Wikipedia
"},{"location":"algorithms/natural-language-processing/NLTK_Setup/","title":"NLTK Setup","text":"

Hello there! \ud83c\udf1f Welcome to your first step into the fascinating world of Natural Language Processing (NLP) with the Natural Language Toolkit (NLTK). This guide is designed to be super beginner-friendly. We\u2019ll cover everything from installation to basic operations with lots of explanations along the way. Let's get started!

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#what-is-nltk","title":"What is NLTK?","text":"

The Natural Language Toolkit (NLTK) is a comprehensive Python library for working with human language data (text). It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning. It also includes wrappers for industrial-strength NLP libraries.

Key Features of NLTK:

1.Corpora and Lexical Resources: NLTK includes access to a variety of text corpora and lexical resources, such as WordNet, the Brown Corpus, the Gutenberg Corpus, and many more.

2.Text Processing Libraries: It provides tools for a wide range of text processing tasks:

Tokenization (splitting text into words, sentences, etc.)

Part-of-Speech (POS) tagging

Named Entity Recognition (NER)

Stemming and Lemmatization

Parsing (syntax analysis)

Semantic reasoning

3.Classification and Machine Learning: NLTK includes various classifiers and machine learning algorithms that can be used for text classification tasks.

4.Visualization and Demonstrations: It offers visualization tools for trees, graphs, and other linguistic structures. It also includes a number of interactive demonstrations and sample data.

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#installation","title":"Installation","text":"

First, we need to install NLTK. Make sure you have Python installed on your system. If not, you can download it from python.org. Once you have Python, open your command prompt (or terminal) and type the following command:

pip install nltk\n
To verify that NLTK is installed correctly, open a Python shell and import the library:

import nltk If no errors occur, NLTK is successfully installed. NLTK requires additional data packages for various functionalities. To download all the data packages, open a python shell and run :

import nltk\nnltk.download ('all')\n
Alternatively you can download specific data packages using :

nltk.download ('punkt') # Tokenizer for splitting sentences into words nltk.download ('averaged_perceptron_tagger') # Part-of-speech tagger for tagging words with their parts of speech nltk.download ('maxent_ne_chunker') # Named entity chunker for recognizing named entities in text nltk.download ('words') # Corpus of English words required for many NLTK functions Now that we have everything set up, let\u2019s dive into some basic NLP operations with NLTK.

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#tokenization","title":"Tokenization","text":"

Tokenization is the process of breaking down text into smaller pieces, like words or sentences. It's like cutting a big cake into smaller slices.

from nltk.tokenize import word_tokenize, sent_tokenize\n\n#Sample text to work with\ntext = \"Natural Language Processing with NLTK is fun and educational.\"\n\n#Tokenize into words\nwords = word_tokenize(text)\nprint(\"Word Tokenization:\", words)\n\n#Tokenize into sentences\nsentences = sent_tokenize(text)\nprint(\"Sentence Tokenization:\", sentences)\n
Word Tokenization: ['Natural', 'Language', 'Processing', 'with', 'NLTK', 'is', 'fun', 'and', 'educational', '.']

Sentence Tokenization: ['Natural Language Processing with NLTK is fun and educational.']

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#stopwords-removal","title":"Stopwords Removal","text":"

Stopwords are common words that don\u2019t carry much meaning on their own. In many NLP tasks, we remove these words to focus on the important ones.

from nltk.corpus import stopwords\n\n# Get the list of stopwords in English\nstop_words = set(stopwords.words('english'))\n\n# Remove stopwords from our list of words\nfiltered_words = [word for word in words if word.lower() not in stop_words]\n\nprint(\"Filtered Words:\", filtered_words)\n
Filtered Words: ['Natural', 'Language', 'Processing', 'NLTK', 'fun', 'educational', '.']

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#explanation","title":"Explanation:","text":"

stopwords.words('english'): This gives us a list of common English stopwords.

[word for word in words if word.lower() not in stop_words]: This is a list comprehension that filters out the stopwords from our list of words.

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#stemming","title":"Stemming","text":"

Stemming is the process of reducing words to their root form. It\u2019s like finding the 'stem' of a word.

from nltk.stem import PorterStemmer\n\n# Create a PorterStemmer object\nps = PorterStemmer()\n\n# Stem each word in our list of words\nstemmed_words = [ps.stem(word) for word in words]\n\nprint(\"Stemmed Words:\", stemmed_words)\n
Stemmed Words: ['natur', 'languag', 'process', 'with', 'nltk', 'is', 'fun', 'and', 'educ', '.']

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#explanation_1","title":"Explanation:","text":"

PorterStemmer(): This creates a PorterStemmer object, which is a popular stemming algorithm.

[ps.stem(word) for word in words]: This applies the stemming algorithm to each word in our list.

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#lemmatization","title":"Lemmatization","text":"

Lemmatization is similar to stemming but it uses a dictionary to find the base form of a word. It\u2019s more accurate than stemming.

from nltk.stem import WordNetLemmatizer\n\n# Create a WordNetLemmatizer object\nlemmatizer = WordNetLemmatizer()\n\n# Lemmatize each word in our list of words\nlemmatized_words = [lemmatizer.lemmatize(word) for word in words]\n\nprint(\"Lemmatized Words:\", lemmatized_words)\n
Lemmatized Words: ['Natural', 'Language', 'Processing', 'with', 'NLTK', 'is', 'fun', 'and', 'educational', '.']

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#explanation_2","title":"Explanation:","text":"

WordNetLemmatizer(): This creates a lemmatizer object.

[lemmatizer.lemmatize(word) for word in words]: This applies the lemmatization process to each word in our list.

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#part-of-speech-tagging","title":"Part-of-speech tagging","text":"

Part-of-speech tagging is the process of labeling each word in a sentence with its corresponding part of speech, such as noun, verb, adjective, etc. NLTK provides functionality to perform POS tagging easily.

# Import the word_tokenize function from nltk.tokenize module\n# Import the pos_tag function from nltk module\nfrom nltk.tokenize import word_tokenize\nfrom nltk import pos_tag\n\n# Sample text to work with\ntext = \"NLTK is a powerful tool for natural language processing.\"\n\n# Tokenize the text into individual words\n# The word_tokenize function splits the text into a list of words\nwords = word_tokenize(text)\n\n# Perform Part-of-Speech (POS) tagging\n# The pos_tag function takes a list of words and assigns a part-of-speech tag to each word\npos_tags = pos_tag(words)\n\n# Print the part-of-speech tags\nprint(\"Part-of-speech tags:\")\nprint(pos_tags)\n
Part-of-speech tags: [('NLTK', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('powerful', 'JJ'), ('tool', 'NN'), ('for', 'IN'), ('natural', 'JJ'), ('language', 'NN'), ('processing', 'NN'), ('.', '.')]

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#explanation_3","title":"Explanation:","text":"

pos_tags = pos_tag(words): The pos_tag function takes the list of words and assigns a part-of-speech tag to each word. For example, it might tag 'NLTK' as a proper noun (NNP), 'is' as a verb (VBZ), and so on.

Here is a list of common POS tags used in the Penn Treebank tag set, along with explanations and examples:

"},{"location":"algorithms/natural-language-processing/NLTK_Setup/#common-pos-tags","title":"Common POS Tags:","text":"

CC: Coordinating conjunction (e.g., and, but, or)

CD: Cardinal number (e.g., one, two)

DT: Determiner (e.g., the, a, an)

EX: Existential there (e.g., there is)

FW: Foreign word (e.g., en route)

IN: Preposition or subordinating conjunction (e.g., in, of, like)

JJ: Adjective (e.g., big, blue, fast)

JJR: Adjective, comparative (e.g., bigger, faster)

JJS: Adjective, superlative (e.g., biggest, fastest)

LS: List item marker (e.g., 1, 2, One)

MD: Modal (e.g., can, will, must)

NN: Noun, singular or mass (e.g., dog, city, music)

NNS: Noun, plural (e.g., dogs, cities)

NNP: Proper noun, singular (e.g., John, London)

NNPS: Proper noun, plural (e.g., Americans, Sundays)

PDT: Predeterminer (e.g., all, both, half)

POS: Possessive ending (e.g., 's, s')

PRP: Personal pronoun (e.g., I, you, he)

PRP$: Possessive pronoun (e.g., my, your, his)

RB: Adverb (e.g., quickly, softly)

RBR: Adverb, comparative (e.g., faster, harder)

RBS: Adverb, superlative (e.g., fastest, hardest)

RP: Particle (e.g., up, off)

SYM: Symbol (e.g., $, %, &)

TO: to (e.g., to go, to read)

UH: Interjection (e.g., uh, well, wow)

VB: Verb, base form (e.g., run, eat)

VBD: Verb, past tense (e.g., ran, ate)

VBG: Verb, gerund or present participle (e.g., running, eating)

VBN: Verb, past participle (e.g., run, eaten)

VBP: Verb, non-3rd person singular present (e.g., run, eat)

VBZ: Verb, 3rd person singular present (e.g., runs, eats)

WDT: Wh-determiner (e.g., which, that)

WP: Wh-pronoun (e.g., who, what)

WP$: Possessive wh-pronoun (e.g., whose)

WRB: Wh-adverb (e.g., where, when)

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/","title":"NLP Introduction","text":""},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#what-is-nlp","title":"What is NLP? \ud83e\udd16","text":"

Natural Language Processing (NLP) is a field of artificial intelligence that enables computers to understand, interpret, and respond to human language. It bridges the gap between human communication and machine understanding, making it possible for computers to interact with us in a natural and intuitive way.

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#importance-of-nlp","title":"Importance of NLP \ud83c\udf10","text":"

NLP is essential for various applications that we use daily, such as:

  • Voice Assistants (e.g., Siri, Alexa) \ud83d\udde3\ufe0f
  • Chatbots for customer service \ud83d\udcac
  • Language Translation services (e.g., Google Translate) \ud83c\udf0d
  • Sentiment Analysis in social media monitoring \ud83d\udcca
  • Text Summarization for news articles \ud83d\udcf0
"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#history-and-evolution-of-nlp","title":"History and Evolution of NLP \ud83d\udcdc","text":"
  1. 1950s: Alan Turing's seminal paper \"Computing Machinery and Intelligence\" proposes the Turing Test.
  2. 1960s-1970s: Development of early NLP systems like ELIZA and SHRDLU.
  3. 1980s: Introduction of machine learning algorithms and statistical models.
  4. 1990s: Emergence of more sophisticated algorithms and large annotated datasets.
  5. 2000s: Advent of deep learning, leading to significant breakthroughs in NLP.
  6. 2010s-Present: Development of powerful models like BERT and GPT, revolutionizing the field.
"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#key-concepts-and-terminology","title":"Key Concepts and Terminology \ud83d\udcda","text":"
  • Tokens: The smallest units of text, such as words or punctuation marks. Example: \"Hello, world!\" becomes [\"Hello\", \",\", \"world\", \"!\"].
  • Corpus: A large collection of text used for training NLP models. Example: The Wikipedia Corpus.
  • Stopwords: Commonly used words (e.g., \"the\", \"is\", \"in\") that are often removed from text during preprocessing.
  • Stemming: Reducing words to their base or root form. Example: \"running\" becomes \"run\".
  • Lemmatization: Similar to stemming, but it reduces words to their dictionary form. Example: \"better\" becomes \"good\".
"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#real-world-use-cases","title":"Real-World Use Cases \ud83c\udf1f","text":""},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#voice-assistants","title":"Voice Assistants \ud83d\udde3\ufe0f","text":"

Voice assistants like Siri and Alexa use NLP to understand and respond to user commands. For example, when you ask, \"What's the weather today?\", NLP helps the assistant interpret your query and provide the relevant weather information.

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#customer-service-chatbots","title":"Customer Service Chatbots \ud83d\udcac","text":"

Many companies use chatbots to handle customer inquiries. NLP enables these bots to understand customer questions and provide accurate responses, improving customer satisfaction and reducing response time.

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#language-translation","title":"Language Translation \ud83c\udf0d","text":"

NLP powers translation services like Google Translate, which can translate text from one language to another. This helps break down language barriers and facilitates global communication.

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#sentiment-analysis","title":"Sentiment Analysis \ud83d\udcca","text":"

Businesses use sentiment analysis to monitor social media and understand public opinion about their products or services. NLP analyzes text to determine whether the sentiment expressed is positive, negative, or neutral.

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#text-summarization","title":"Text Summarization \ud83d\udcf0","text":"

NLP algorithms can summarize long articles into concise summaries, making it easier for readers to grasp the main points quickly. This is particularly useful for news articles and research papers.

"},{"location":"algorithms/natural-language-processing/N_L_P_Introduction/#conclusion","title":"Conclusion \ud83c\udf1f","text":"

NLP is a dynamic and rapidly evolving field that plays a crucial role in how we interact with technology. By understanding its basics and key concepts, you can start exploring the fascinating world of language and machines.

This README provides a brief introduction to NLP. For a deeper dive, explore more resources and start building your own NLP projects!

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/","title":"Text Preprocessing Techniques","text":""},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#welcome-to-this-comprehensive-guide-on-text-preprocessing-with-nltk-natural-language-toolkit-this-notebook-will-walk-you-through-various-essential-text-preprocessing-techniques-all-explained-in-simple-terms-with-easy-to-follow-code-examples-whether-youre-just-starting-out-in-nlp-natural-language-processing-or-looking-to-brush-up-on-your-skills-youre-in-the-right-place","title":"Welcome to this comprehensive guide on text preprocessing with NLTK (Natural Language Toolkit)! This notebook will walk you through various essential text preprocessing techniques, all explained in simple terms with easy-to-follow code examples. Whether you're just starting out in NLP (Natural Language Processing) or looking to brush up on your skills, you're in the right place! \ud83d\ude80","text":""},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#nltk-provides-a-comprehensive-suite-of-tools-for-processing-and-analyzing-unstructured-text-data","title":"NLTK provides a comprehensive suite of tools for processing and analyzing unstructured text data.","text":""},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#1-tokenization","title":"1. Tokenization","text":"

Tokenization is the process of splitting text into individual words or sentences.

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#sentence-tokenization","title":"Sentence Tokenization","text":"
import nltk\nnltk.download('punkt')\nfrom nltk.tokenize import sent_tokenize\n\ntext = \"Hello World. This is NLTK. It is great for text processing.\"\nsentences = sent_tokenize(text)\nprint(sentences)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#word-tokenization","title":"Word Tokenization","text":"
from nltk.tokenize import word_tokenize\n\nwords = word_tokenize(text)\nprint(words)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#2-removing-stop-words","title":"2. Removing Stop Words","text":"

Stop words are common words that may not be useful for text analysis (e.g., \"is\", \"the\", \"and\").

from nltk.corpus import stopwords\nnltk.download('stopwords')\n\nstop_words = set(stopwords.words('english'))\nfiltered_words = [word for word in words if word.lower() not in stop_words]\nprint(filtered_words)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#3-stemming","title":"3. Stemming","text":"

Stemming reduces words to their root form by chopping off the ends.

from nltk.stem import PorterStemmer\nstemmer = PorterStemmer()\nstemmed_words = [stemmer.stem(word) for word in filtered_words]\nprint(stemmed_words)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#4-lemmatization","title":"4. Lemmatization","text":"

Lemmatization reduces words to their base form (lemma), taking into account the meaning of the word.

from nltk.stem import WordNetLemmatizer\nnltk.download('wordnet')\n\nlemmatizer = WordNetLemmatizer()\nlemmatized_words = [lemmatizer.lemmatize(word) for word in filtered_words]\nprint(lemmatized_words)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#5-part-of-speech-tagging","title":"5. Part-of-Speech Tagging","text":"

Tagging words with their parts of speech (POS) helps understand the grammatical structure.

The complete POS tag list can be accessed from the Installation and set-up notebook.

nltk.download('averaged_perceptron_tagger')\n\npos_tags = nltk.pos_tag(lemmatized_words)\nprint(pos_tags)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#6-named-entity-recognition","title":"6. Named Entity Recognition","text":"

Identify named entities such as names of people, organizations, locations, etc.

# Numpy is required to run this\n%pip install numpy\n\nnltk.download('maxent_ne_chunker')\nnltk.download('words')\nfrom nltk.chunk import ne_chunk\n\nnamed_entities = ne_chunk(pos_tags)\nprint(named_entities)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#7-word-frequency-distribution","title":"7. Word Frequency Distribution","text":"

Count the frequency of each word in the text.

from nltk.probability import FreqDist\n\nfreq_dist = FreqDist(lemmatized_words)\nprint(freq_dist.most_common(5))\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#8-removing-punctuation","title":"8. Removing Punctuation","text":"

Remove punctuation from the text.

import string\n\nno_punct = [word for word in lemmatized_words if word not in string.punctuation]\nprint(no_punct)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#9-lowercasing","title":"9. Lowercasing","text":"

Convert all words to lowercase.

lowercased = [word.lower() for word in no_punct]\nprint(lowercased)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#10-spelling-correction","title":"10. Spelling Correction","text":"

Correct the spelling of words.

%pip install pyspellchecker\n\nfrom nltk.corpus import wordnet\nfrom spellchecker import SpellChecker\n\nspell = SpellChecker()\n\ndef correct_spelling(word):\n    if not wordnet.synsets(word):\n        return spell.correction(word)\n    return word\n\nlemmatized_words = ['hello', 'world', '.', 'klown', 'taxt', 'procass', '.']\nwords_with_corrected_spelling = [correct_spelling(word) for word in lemmatized_words]\nprint(words_with_corrected_spelling)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#11-removing-numbers","title":"11. Removing Numbers","text":"

Remove numerical values from the text.

lemmatized_words = ['hello', 'world', '88', 'text', 'process', '.']\n\nno_numbers = [word for word in lemmatized_words if not word.isdigit()]\nprint(no_numbers)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#12-word-replacement","title":"12. Word Replacement","text":"

Replace specific words with other words (e.g., replacing slang with formal words).

lemmatized_words = ['hello', 'world', 'gr8', 'text', 'NLTK', '.']\nreplacements = {'NLTK': 'Natural Language Toolkit', 'gr8' : 'great'}\n\nreplaced_words = [replacements.get(word, word) for word in lemmatized_words]\nprint(replaced_words)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#13-synonym-replacement","title":"13. Synonym Replacement","text":"

Replace words with their synonyms.

from nltk.corpus import wordnet\nlemmatized_words = ['hello', 'world', 'awesome', 'text', 'great', '.']\n\ndef get_synonym(word):\n    synonyms = wordnet.synsets(word)\n    if synonyms:\n        return synonyms[0].lemmas()[0].name()\n    return word\n\nsynonym_replaced = [get_synonym(word) for word in lemmatized_words]\nprint(synonym_replaced)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#14-extracting-bigrams-and-trigrams","title":"14. Extracting Bigrams and Trigrams","text":"

Extract bigrams (pairs of consecutive words) and trigrams (triplets of consecutive words).

from nltk import bigrams\n\nbigrams_list = list(bigrams(lemmatized_words))\nprint(bigrams_list)\n\nfrom nltk import trigrams\n\ntrigrams_list = list(trigrams(lemmatized_words))\nprint(trigrams_list)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#15-sentence-segmentation","title":"15. Sentence Segmentation","text":"

Split text into sentences while considering abbreviations and other punctuation complexities.

import nltk.data\n\ntext = 'Hello World. This is NLTK. It is great for text preprocessing.'\n\n# Load the sentence tokenizer\ntokenizer = nltk.data.load('tokenizers/punkt/english.pickle')\n\n# Tokenize the text into sentences\nsentences = tokenizer.tokenize(text)\n\n# Print the tokenized sentences\nprint(sentences)\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#16-identifying-word-frequencies","title":"16. Identifying Word Frequencies","text":"

Identify and display the frequency of words in a text.

from nltk.probability import FreqDist\n\nlemmatized_words = ['hello', 'hello', 'awesome', 'text', 'great', '.', '.', '.']\n\n\nword_freq = FreqDist(lemmatized_words)\nfor word, freq in word_freq.items():\n    print(f\"{word}: {freq}\")\n
"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#17-removing-html-tags","title":"17. Removing HTML tags","text":"

Remove HTML tags from the text.

%pip install bs4\n\nfrom bs4 import BeautifulSoup\n\nhtml_text = \"<p>Hello World. This is NLTK.</p>\"\nsoup = BeautifulSoup(html_text, \"html.parser\")\ncleaned_text = soup.get_text()\nprint(cleaned_text)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#18-detecting-language","title":"18. Detecting Language","text":"

Detect the language of the text.

%pip install langdetect\n\nfrom langdetect import detect\n\nlanguage = detect(text)\nprint(language) #`en` (for English)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#19-tokenizing-by-regular-expressions","title":"19. Tokenizing by Regular Expressions","text":"

Use Regular Expressions to tokenize text.

text = 'Hello World. This is NLTK. It is great for text preprocessing.'\n\nfrom nltk.tokenize import regexp_tokenize\n\npattern = r'\\w+'\nregex_tokens = regexp_tokenize(text, pattern)\nprint(regex_tokens)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#20-remove-frequent-words","title":"20. Remove Frequent Words","text":"

Removes frequent words (also known as \u201chigh-frequency words\u201d) from a list of tokens using NLTK, you can use the nltk.FreqDist() function to calculate the frequency of each word and filter out the most common ones.

import nltk\n\n# input text\ntext = \"Natural language processing is a field of AI. I love AI.\"\n\n# tokenize the text\ntokens = nltk.word_tokenize(text)\n\n# calculate the frequency of each word\nfdist = nltk.FreqDist(tokens)\n\n# remove the most common words (e.g., the top 10% of words by frequency)\nfiltered_tokens = [token for token in tokens if fdist[token] < fdist.N() * 0.1]\n\nprint(\"Tokens without frequent words:\", filtered_tokens)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#21-remove-extra-whitespace","title":"21. Remove extra whitespace","text":"

Tokenizes the input string into individual sentences and remove any leading or trailing whitespace from each sentence.

import nltk.data\n\n# Text data\ntext = 'Hello World. This is NLTK. It is great for text preprocessing.'\n\n# Load the sentence tokenizer\ntokenizer = nltk.data.load('tokenizers/punkt/english.pickle')\n\n# Tokenize the text into sentences\nsentences = tokenizer.tokenize(text)\n\n# Remove extra whitespace from each sentence\nsentences = [sentence.strip() for sentence in sentences]\n\n# Print the tokenized sentences\nprint(sentences)\n

"},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#conclusion","title":"Conclusion \ud83c\udf89","text":""},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#text-preprocessing-is-a-crucial-step-in-natural-language-processing-nlp-and-can-significantly-impact-the-performance-of-your-models-and-applications-with-nltk-we-have-a-powerful-toolset-that-simplifies-and-streamlines-these-tasks","title":"Text preprocessing is a crucial step in natural language processing (NLP) and can significantly impact the performance of your models and applications. With NLTK, we have a powerful toolset that simplifies and streamlines these tasks.","text":""},{"location":"algorithms/natural-language-processing/Text_PreProcessing_Techniques/#i-hope-this-guide-has-provided-you-with-a-solid-foundation-for-text-preprocessing-with-nltk-as-you-continue-your-journey-in-nlp-remember-that-preprocessing-is-just-the-beginning-there-are-many-more-exciting-and-advanced-techniques-to-explore-and-apply-in-your-projects","title":"I hope this guide has provided you with a solid foundation for text preprocessing with NLTK. As you continue your journey in NLP, remember that preprocessing is just the beginning. There are many more exciting and advanced techniques to explore and apply in your projects.","text":""},{"location":"algorithms/natural-language-processing/Tf_Idf/","title":"Tf-Idf","text":""},{"location":"algorithms/natural-language-processing/Tf_Idf/#introduction","title":"Introduction","text":"

The TFIDF class converts a collection of documents into their respective TF-IDF (Term Frequency-Inverse Document Frequency) representations. TF-IDF is a statistical measure used to evaluate the importance of a word in a document relative to a collection of documents (corpus).

"},{"location":"algorithms/natural-language-processing/Tf_Idf/#attributes","title":"Attributes","text":"

The TFIDF class is initialized with two main attributes:

  • self.vocabulary: A dictionary that maps words to their indices in the TF-IDF matrix.
  • self.idf_values: A dictionary that stores the IDF (Inverse Document Frequency) values for each word.
"},{"location":"algorithms/natural-language-processing/Tf_Idf/#methods","title":"Methods","text":""},{"location":"algorithms/natural-language-processing/Tf_Idf/#fit-method","title":"fit Method","text":""},{"location":"algorithms/natural-language-processing/Tf_Idf/#input","title":"Input","text":"
  • documents (list of str): List of documents where each document is a string.
"},{"location":"algorithms/natural-language-processing/Tf_Idf/#purpose","title":"Purpose","text":"

Calculate the IDF values for all unique words in the corpus.

"},{"location":"algorithms/natural-language-processing/Tf_Idf/#steps","title":"Steps","text":"
  1. Count Document Occurrences: Determine how many documents contain each word.
  2. Compute IDF: Calculate the importance of each word across all documents. Higher values indicate the word is more unique to fewer documents.
  3. Build Vocabulary: Create a mapping of words to unique indexes.
"},{"location":"algorithms/natural-language-processing/Tf_Idf/#transform-method","title":"transform Method","text":""},{"location":"algorithms/natural-language-processing/Tf_Idf/#input_1","title":"Input","text":"
  • documents (list of str): A list where each entry is a document in the form of a string.
"},{"location":"algorithms/natural-language-processing/Tf_Idf/#purpose_1","title":"Purpose","text":"

Convert each document into a numerical representation that shows the importance of each word.

"},{"location":"algorithms/natural-language-processing/Tf_Idf/#steps_1","title":"Steps","text":"
  1. Compute Term Frequency (TF): Determine how often each word appears in a document relative to the total number of words in that document.
  2. Compute TF-IDF: Multiply the term frequency of each word by its IDF to get a measure of its relevance in each document.
  3. Store Values: Save these numerical values in a matrix where each row represents a document.
"},{"location":"algorithms/natural-language-processing/Tf_Idf/#fit_transform-method","title":"fit_transform Method","text":""},{"location":"algorithms/natural-language-processing/Tf_Idf/#purpose_2","title":"Purpose","text":"

Perform both fitting (computing IDF values) and transforming (converting documents to TF-IDF representation) in one step.

"},{"location":"algorithms/natural-language-processing/Tf_Idf/#explanation-of-the-code","title":"Explanation of the Code","text":"

The TFIDF class includes methods for fitting the model to the data, transforming new data into the TF-IDF representation, and combining these steps. Here's a breakdown of the primary methods:

  1. fit Method: Calculates IDF values for all unique words in the corpus. It counts the number of documents containing each word and computes the IDF. The vocabulary is built with a word-to-index mapping.

  2. transform Method: Converts each document into a TF-IDF representation. It computes Term Frequency (TF) for each word in the document, calculates TF-IDF by multiplying TF with IDF, and stores these values in a matrix where each row corresponds to a document.

  3. fit_transform Method: Combines the fitting and transforming steps into a single method for efficient processing of documents.

"},{"location":"algorithms/natural-language-processing/Tf_Idf/#code","title":"Code","text":"
  • main.py file
import math\nfrom collections import Counter\n\nclass TFIDF:\n    def __init__(self):\n        self.vocabulary = {}  # Vocabulary to store word indices\n        self.idf_values = {}  # IDF values for words\n\n    def fit(self, documents):\n        \"\"\"\n        Compute IDF values based on the provided documents.\n\n        Args:\n            documents (list of str): List of documents where each document is a string.\n        \"\"\"\n        doc_count = len(documents)\n        term_doc_count = Counter()  # To count the number of documents containing each word\n\n        # Count occurrences of words in documents\n        for doc in documents:\n            words = set(doc.split())  # Unique words in the current document\n            for word in words:\n                term_doc_count[word] += 1\n\n        # Compute IDF values\n        self.idf_values = {\n            word: math.log(doc_count / (count + 1))  # +1 to avoid division by zero\n            for word, count in term_doc_count.items()\n        }\n\n        # Build vocabulary\n        self.vocabulary = {word: idx for idx, word in enumerate(self.idf_values.keys())}\n\n    def transform(self, documents):\n        \"\"\"\n        Transform documents into TF-IDF representation.\n\n        Args:\n            documents (list of str): List of documents where each document is a string.\n\n        Returns:\n            list of list of float: TF-IDF matrix where each row corresponds to a document.\n        \"\"\"\n        rows = []\n        for doc in documents:\n            words = doc.split()\n            word_count = Counter(words)\n            doc_length = len(words)\n            row = [0] * len(self.vocabulary)\n\n            for word, count in word_count.items():\n                if word in self.vocabulary:\n                    tf = count / doc_length\n                    idf = self.idf_values[word]\n                    index = self.vocabulary[word]\n                    row[index] = tf * idf\n            rows.append(row)\n        return rows\n\n    def fit_transform(self, documents):\n        \"\"\"\n        Compute IDF values and transform documents into TF-IDF representation.\n\n        Args:\n            documents (list of str): List of documents where each document is a string.\n\n        Returns:\n            list of list of float: TF-IDF matrix where each row corresponds to a document.\n        \"\"\"\n        self.fit(documents)\n        return self.transform(documents)\n# Example usage\nif __name__ == \"__main__\":\n    documents = [\n        \"the cat sat on the mat\",\n        \"the dog ate my homework\",\n        \"the cat ate the dog food\",\n        \"I love programming in Python\",\n        \"Machine learning is fun\",\n        \"Python is a versatile language\",\n        \"Learning new skills is always beneficial\"\n    ]\n\n    # Initialize the TF-IDF model\n    tfidf = TFIDF()\n\n    # Fit the model and transform the documents\n    tfidf_matrix = tfidf.fit_transform(documents)\n\n    # Print the vocabulary\n    print(\"Vocabulary:\", tfidf.vocabulary)\n\n    # Print the TF-IDF representation\n    print(\"TF-IDF Representation:\")\n    for i, vector in enumerate(tfidf_matrix):\n        print(f\"Document {i + 1}: {vector}\")\n\n    # More example documents with mixed content\n    more_documents = [\n        \"the quick brown fox jumps over the lazy dog\",\n        \"a journey of a thousand miles begins with a single step\",\n        \"to be or not to be that is the question\",\n        \"the rain in Spain stays mainly in the plain\",\n        \"all human beings are born free and equal in dignity and rights\"\n    ]\n\n    # Fit the model and transform the new set of documents\n    tfidf_more = TFIDF()\n    tfidf_matrix_more = tfidf_more.fit_transform(more_documents)\n\n    # Print the vocabulary for the new documents\n    print(\"\\nVocabulary for new documents:\", tfidf_more.vocabulary)\n\n    # Print the TF-IDF representation for the new documents\n    print(\"TF-IDF Representation for new documents:\")\n    for i, vector in enumerate(tfidf_matrix_more):\n        print(f\"Document {i + 1}: {vector}\")\n
"},{"location":"algorithms/natural-language-processing/Tf_Idf/#references","title":"References","text":"
  1. TF-IDF - Wikipedia
  2. Understanding TF-IDF
  3. Scikit-learn: TF-IDF
"},{"location":"algorithms/natural-language-processing/Transformers/","title":"Transformers","text":"

Welcome to the official documentation for the Transformers library! \ud83d\ude80 This library, developed by Hugging Face, is designed to provide state-of-the-art natural language processing (NLP) models and tools. It's widely used for a variety of NLP tasks, including text classification, translation, summarization, and more.

"},{"location":"algorithms/natural-language-processing/Transformers/#overview","title":"\ud83d\udd0d Overview","text":"

Transformers are a type of deep learning model that excel in handling sequential data, like text. They rely on mechanisms such as attention to process and generate text in a way that captures long-range dependencies and contextual information.

"},{"location":"algorithms/natural-language-processing/Transformers/#key-features","title":"Key Features","text":"
  • State-of-the-art Models: Access pre-trained models like BERT, GPT, T5, and many more. \ud83c\udfc6
  • Easy-to-use Interface: Simplify the process of using and fine-tuning models with a user-friendly API. \ud83c\udfaf
  • Tokenization Tools: Tokenize and preprocess text efficiently for model input. \ud83e\udde9
  • Multi-Framework Support: Compatible with PyTorch and TensorFlow, giving you flexibility in your deep learning environment. \u2699\ufe0f
  • Extensive Documentation: Detailed guides and tutorials to help you get started and master the library. \ud83d\udcd6
"},{"location":"algorithms/natural-language-processing/Transformers/#installation","title":"\ud83d\udd27 Installation","text":"

To get started with the Transformers library, you need to install it via pip:

pip install transformers\n
"},{"location":"algorithms/natural-language-processing/Transformers/#system-requirements","title":"System Requirements","text":"
  • Python: Version 3.6 or later.
  • PyTorch or TensorFlow: Depending on your preferred framework. Visit the official documentation for compatibility details.
"},{"location":"algorithms/natural-language-processing/Transformers/#quick-start","title":"\ud83d\ude80 Quick Start","text":"

Here's a basic example to demonstrate how to use the library for sentiment classification:

from transformers import pipeline\n\n# Initialize the pipeline for sentiment analysis\nclassifier = pipeline('sentiment-analysis')\n\n# Analyze sentiment of a sample text\nresult = classifier(\"Transformers are amazing for NLP tasks! \ud83c\udf1f\")\n\nprint(result)\n
"},{"location":"algorithms/natural-language-processing/Transformers/#common-pipelines","title":"Common Pipelines","text":"
  • Text Classification: Classify text into predefined categories.
  • Named Entity Recognition (NER): Identify entities like names, dates, and locations.
  • Text Generation: Generate text based on a prompt.
  • Question Answering: Answer questions based on a given context.
  • Translation: Translate text between different languages.
"},{"location":"algorithms/natural-language-processing/Transformers/#documentation","title":"\ud83d\udcda Documentation","text":"

For comprehensive guides, tutorials, and API references, check out the following resources:

  • Transformers Documentation: The official site with detailed information on using and customizing the library.
  • Model Hub: Explore a wide range of pre-trained models available for different NLP tasks.
  • API Reference: Detailed descriptions of classes and functions in the library.
"},{"location":"algorithms/natural-language-processing/Transformers/#community-and-support","title":"\ud83d\udee0\ufe0f Community and Support","text":"

Join the vibrant community of Transformers users and contributors to get support, share your work, and stay updated:

  • Hugging Face Forums: Engage with other users and experts. Ask questions, share your projects, and participate in discussions.
  • GitHub Repository: Browse the source code, report issues, and contribute to the project. Check out the issues for ongoing conversations.
"},{"location":"algorithms/natural-language-processing/Transformers/#additional-resources","title":"\ud83d\udd17 Additional Resources","text":"
  • Research Papers: Read the research papers behind the models and techniques used in the library.
  • Blog Posts: Discover insights, tutorials, and updates from the Hugging Face team.
  • Webinars and Talks: Watch recorded talks and webinars on the latest developments and applications of Transformers.
"},{"location":"algorithms/natural-language-processing/Transformers/#faq","title":"\u2753 FAQ","text":"

Q: What are the main differences between BERT and GPT?

A: BERT (Bidirectional Encoder Representations from Transformers) is designed for understanding the context of words in both directions (left and right). GPT (Generative Pre-trained Transformer), on the other hand, is designed for generating text and understanding context in a left-to-right manner.

Q: Can I fine-tune a model on my own data?

A: Yes, the Transformers library provides tools for fine-tuning pre-trained models on your custom datasets. Check out the fine-tuning guide for more details.

Happy Transforming! \ud83c\udf1f

"},{"location":"algorithms/natural-language-processing/Word_2_Vec/","title":"Word 2 Vec","text":""},{"location":"algorithms/natural-language-processing/Word_2_Vec/#introduction","title":"Introduction","text":"

Word2Vec is a technique to learn word embeddings using neural networks. The primary goal is to represent words in a continuous vector space where semantically similar words are mapped to nearby points. Word2Vec can be implemented using two main architectures:

  1. Continuous Bag of Words (CBOW): Predicts the target word based on the context words (surrounding words).
  2. Skip-gram: Predicts the context words based on a given target word.

In this example, we focus on the Skip-gram approach, which is more commonly used in practice. The Skip-gram model tries to maximize the probability of context words given a target word.

"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#installation","title":"Installation","text":"

Ensure you have Python installed. You can install the necessary dependencies using pip:

pip install numpy\n
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#usage","title":"Usage","text":""},{"location":"algorithms/natural-language-processing/Word_2_Vec/#initialization","title":"Initialization","text":"

Define the parameters for the Word2Vec model:

  • window_size: Defines the size of the context window around the target word.
  • embedding_dim: Dimension of the word vectors (embedding space).
  • learning_rate: Rate at which weights are updated.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#tokenization","title":"Tokenization","text":"

The tokenize method creates a vocabulary from the documents and builds mappings between words and their indices.

"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#generate-training-data","title":"Generate Training Data","text":"

The generate_training_data method creates pairs of target words and context words based on the window size.

"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#training","title":"Training","text":"

The train method initializes the weight matrices and updates them using gradient descent.

For each word-context pair, it computes the hidden layer representation, predicts context probabilities, calculates the error, and updates the weights.

"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#retrieve-word-vector","title":"Retrieve Word Vector","text":"

The get_word_vector method retrieves the embedding of a specific word.

"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#explanation-of-the-code","title":"Explanation of the Code","text":""},{"location":"algorithms/natural-language-processing/Word_2_Vec/#initialization_1","title":"Initialization","text":"
  • Parameters:
  • window_size: Size of the context window around the target word.
  • embedding_dim: Dimension of the word vectors (embedding space).
  • learning_rate: Rate at which weights are updated.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#tokenization_1","title":"Tokenization","text":"
  • The tokenize method creates a vocabulary from the documents.
  • Builds mappings between words and their indices.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#generate-training-data_1","title":"Generate Training Data","text":"
  • The generate_training_data method creates pairs of target words and context words based on the window size.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#training_1","title":"Training","text":"
  • The train method initializes the weight matrices.
  • Updates the weights using gradient descent.
  • For each word-context pair:
  • Computes the hidden layer representation.
  • Predicts context probabilities.
  • Calculates the error.
  • Updates the weights.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#softmax-function","title":"Softmax Function","text":"
  • The softmax function converts the output layer scores into probabilities.
  • Used to compute the error and update the weights.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#retrieve-word-vector_1","title":"Retrieve Word Vector","text":"
  • The get_word_vector method retrieves the embedding of a specific word.
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#code","title":"Code","text":"
  • word2vec.py file
import numpy as np\n\nclass Word2Vec:\n    def __init__(self, window_size=2, embedding_dim=10, learning_rate=0.01):\n        # Initialize parameters\n        self.window_size = window_size\n        self.embedding_dim = embedding_dim\n        self.learning_rate = learning_rate\n        self.vocabulary = {}\n        self.word_index = {}\n        self.index_word = {}\n        self.W1 = None\n        self.W2 = None\n\n    def tokenize(self, documents):\n        # Tokenize documents and build vocabulary\n        vocabulary = set()\n        for doc in documents:\n            words = doc.split()\n            vocabulary.update(words)\n\n        self.vocabulary = list(vocabulary)\n        self.word_index = {word: idx for idx, word in enumerate(self.vocabulary)}\n        self.index_word = {idx: word for idx, word in enumerate(self.vocabulary)}\n\n    def generate_training_data(self, documents):\n        # Generate training data for the Skip-gram model\n        training_data = []\n        for doc in documents:\n            words = doc.split()\n            for idx, word in enumerate(words):\n                target_word = self.word_index[word]\n                context = [self.word_index[words[i]] for i in range(max(0, idx - self.window_size), min(len(words), idx + self.window_size + 1)) if i != idx]\n                for context_word in context:\n                    training_data.append((target_word, context_word))\n        return training_data\n\n    def train(self, documents, epochs=1000):\n        # Tokenize the documents and generate training data\n        self.tokenize(documents)\n        training_data = self.generate_training_data(documents)\n\n        # Initialize weight matrices with random values\n        vocab_size = len(self.vocabulary)\n        self.W1 = np.random.uniform(-1, 1, (vocab_size, self.embedding_dim))\n        self.W2 = np.random.uniform(-1, 1, (self.embedding_dim, vocab_size))\n\n        for epoch in range(epochs):\n            loss = 0\n            for target_word, context_word in training_data:\n                # Forward pass\n                h = self.W1[target_word]  # Hidden layer representation of the target word\n                u = np.dot(h, self.W2)    # Output layer scores\n                y_pred = self.softmax(u) # Predicted probabilities\n\n                # Calculate error\n                e = np.zeros(vocab_size)\n                e[context_word] = 1\n                error = y_pred - e\n\n                # Backpropagation\n                self.W1[target_word] -= self.learning_rate * np.dot(self.W2, error)\n                self.W2 -= self.learning_rate * np.outer(h, error)\n\n                # Calculate loss (cross-entropy)\n                loss -= np.log(y_pred[context_word])\n\n            if (epoch + 1) % 100 == 0:\n                print(f'Epoch {epoch + 1}, Loss: {loss}')\n\n    def softmax(self, x):\n        # Softmax function to convert scores into probabilities\n        e_x = np.exp(x - np.max(x))\n        return e_x / e_x.sum(axis=0)\n\n    def get_word_vector(self, word):\n        # Retrieve the vector representation of a word\n        return self.W1[self.word_index[word]]\n\n    def get_vocabulary(self):\n        # Retrieve the vocabulary\n        return self.vocabulary\n# Example usage\nif __name__ == \"__main__\":\n    # Basic example usage    \n    documents = [\n        \"the cat sat on the mat\",\n        \"the dog ate my homework\",\n        \"the cat ate the dog food\",\n        \"I love programming in Python\",\n        \"Machine learning is fun\",\n        \"Python is a versatile language\",\n        \"Learning new skills is always beneficial\"\n    ]\n\n    # Initialize and train the Word2Vec model\n    word2vec = Word2Vec()\n    word2vec.train(documents)\n\n    # Print the vocabulary\n    print(\"Vocabulary:\", word2vec.get_vocabulary())\n\n    # Print the word vectors for each word in the vocabulary\n    print(\"Word Vectors:\")\n    for word in word2vec.get_vocabulary():\n        vector = word2vec.get_word_vector(word)\n        print(f\"Vector for '{word}':\", vector)\n\n    # More example documents with mixed content\n    more_documents = [\n        \"the quick brown fox jumps over the lazy dog\",\n        \"a journey of a thousand miles begins with a single step\",\n        \"to be or not to be that is the question\",\n        \"the rain in Spain stays mainly in the plain\",\n        \"all human beings are born free and equal in dignity and rights\"\n    ]\n\n    # Initialize and train the Word2Vec model on new documents\n    word2vec_more = Word2Vec()\n    word2vec_more.train(more_documents)\n\n    # Print the word vectors for selected words\n    print(\"\\nWord Vectors for new documents:\")\n    for word in ['quick', 'journey', 'be', 'rain', 'human']:\n        vector = word2vec_more.get_word_vector(word)\n        print(f\"Vector for '{word}':\", vector)\n
"},{"location":"algorithms/natural-language-processing/Word_2_Vec/#references","title":"References","text":"
  1. Word2Vec - Google
  2. Efficient Estimation of Word Representations in Vector Space
  3. Distributed Representations of Words and Phrases and their Compositionality
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/","title":"Word Embeddings","text":""},{"location":"algorithms/natural-language-processing/Word_Embeddings/#introduction","title":"Introduction","text":"

Word embeddings are a type of word representation that allows words to be represented as vectors in a continuous vector space. These embeddings capture semantic meanings of words based on their context and usage in a given corpus. Word embeddings have become a fundamental concept in Natural Language Processing (NLP) and are widely used for various NLP tasks such as sentiment analysis, machine translation, and more.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#why-use-word-embeddings","title":"Why Use Word Embeddings?","text":"

Traditional word representations, such as one-hot encoding, have limitations: - High Dimensionality: One-hot encoding results in very high-dimensional vectors, which are sparse (mostly zeros). - Lack of Semantics: One-hot vectors do not capture any semantic relationships between words.

Word embeddings address these issues by: - Dimensionality Reduction: Representing words in a lower-dimensional space. - Capturing Semantics: Encoding words such that similar words are closer together in the vector space.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#types-of-word-embeddings","title":"Types of Word Embeddings","text":""},{"location":"algorithms/natural-language-processing/Word_Embeddings/#1-word2vec","title":"1. Word2Vec","text":"

Developed by Mikolov et al., Word2Vec generates word embeddings using neural networks. There are two main models:

  • Continuous Bag of Words (CBOW): Predicts a target word based on its surrounding context words.
  • Skip-gram: Predicts surrounding context words given a target word.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#advantages","title":"Advantages","text":"
  • Efficient and scalable.
  • Captures semantic similarity.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#limitations","title":"Limitations","text":"
  • Context window size and other parameters need tuning.
  • Does not consider word order beyond the fixed context window.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#2-glove-global-vectors-for-word-representation","title":"2. GloVe (Global Vectors for Word Representation)","text":"

Developed by Pennington et al., GloVe generates embeddings by factorizing the word co-occurrence matrix. The key idea is to use the global statistical information of the corpus.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#advantages_1","title":"Advantages","text":"
  • Captures global statistical information.
  • Produces high-quality embeddings.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#limitations_1","title":"Limitations","text":"
  • Computationally intensive.
  • Fixed window size and parameters.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#3-fasttext","title":"3. FastText","text":"

Developed by Facebook's AI Research (FAIR) lab, FastText is an extension of Word2Vec that represents words as bags of character n-grams. This allows FastText to generate embeddings for out-of-vocabulary words.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#advantages_2","title":"Advantages","text":"
  • Handles morphologically rich languages better.
  • Generates embeddings for out-of-vocabulary words.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#limitations_2","title":"Limitations","text":"
  • Slightly more complex to train than Word2Vec.
  • Increased training time.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#4-elmo-embeddings-from-language-models","title":"4. ELMo (Embeddings from Language Models)","text":"

Developed by Peters et al., ELMo generates embeddings using deep contextualized word representations based on a bidirectional LSTM.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#advantages_3","title":"Advantages","text":"
  • Contextual embeddings.
  • Captures polysemy (different meanings of a word).
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#limitations_3","title":"Limitations","text":"
  • Computationally expensive.
  • Not as interpretable as static embeddings.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#5-bert-bidirectional-encoder-representations-from-transformers","title":"5. BERT (Bidirectional Encoder Representations from Transformers)","text":"

Developed by Devlin et al., BERT uses transformers to generate embeddings. BERT's embeddings are contextual and capture bidirectional context.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#advantages_4","title":"Advantages","text":"
  • State-of-the-art performance in many NLP tasks.
  • Contextual embeddings capture richer semantics.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#limitations_4","title":"Limitations","text":"
  • Requires significant computational resources.
  • Complexity in implementation.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#applications-of-word-embeddings","title":"Applications of Word Embeddings","text":"
  • Sentiment Analysis: Understanding the sentiment of a text by analyzing word embeddings.
  • Machine Translation: Translating text from one language to another using embeddings.
  • Text Classification: Categorizing text into predefined categories.
  • Named Entity Recognition: Identifying and classifying entities in text.
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#example-code","title":"Example Code","text":"

Here's an example of using Word2Vec with the gensim library in Python:

from gensim.models import Word2Vec\nfrom nltk.tokenize import word_tokenize\nimport nltk\n\nnltk.download('punkt')\n\n# Sample corpus\ncorpus = [\n    \"This is a sample sentence\",\n    \"Word embeddings are useful in NLP\",\n    \"Natural Language Processing with embeddings\"\n]\n\n# Tokenize the corpus\ntokenized_corpus = [word_tokenize(sentence.lower()) for sentence in corpus]\n\n# Train Word2Vec model\nmodel = Word2Vec(sentences=tokenized_corpus, vector_size=50, window=3, min_count=1, sg=1)\n\n# Get the embedding for the word 'word'\nword_embedding = model.wv['word']\nprint(\"Embedding for 'word':\", word_embedding)\n
"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#conclusion","title":"Conclusion","text":"

Word embeddings are a powerful technique in NLP that provide a way to represent words in a dense, continuous vector space. By capturing semantic relationships and reducing dimensionality, embeddings improve the performance of various NLP tasks and models.

"},{"location":"algorithms/natural-language-processing/Word_Embeddings/#references","title":"References","text":"
  • Word2Vec Explained
  • GloVe: Global Vectors for Word Representation
  • FastText
  • ELMo: Deep Contextualized Word Representations
  • BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
"},{"location":"algorithms/statistics/","title":"Statistics \ud83d\udcc3","text":"Descriptive

Describe our data in some meaningful manner

Inferential

Uses sample data to draw conclusions about a larger population.

Metrics and Losses

Quantify how well your model matches the actual data

Probabilty

Possibility of the outcome of any random event.

"},{"location":"algorithms/statistics/descriptive/","title":"Descriptive Statstics \ud83d\udcc3","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/statistics/inferential/","title":"Inferential Statstics \ud83d\udcc3","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"algorithms/statistics/metrics-and-losses/","title":"Metrics and Losses \ud83d\udcc3","text":""},{"location":"algorithms/statistics/metrics-and-losses/errors/Mean_Absolute_Error/","title":"Mean Absolute Error","text":"
import numpy as np\n\ndef mean_absolute_error(y_true, y_pred):\n    \"\"\"\n    Calculate the mean absolute error between true and predicted values.\n\n    Parameters:\n    - y_true: True target values (numpy array).\n    - y_pred: Predicted values (numpy array).\n\n    Returns:\n    - Mean absolute error (float).\n    \"\"\"    \n    return (np.absolute(y_true - y_pred)).mean()\n
"},{"location":"algorithms/statistics/metrics-and-losses/errors/Mean_Squared_Error/","title":"Mean Squared Error","text":"
import numpy as np\n\ndef mean_squared_error(y_true, y_pred):\n    \"\"\"\n    Calculate the mean squared error between true and predicted values.\n\n    Parameters:\n    - y_true: True target values (numpy array).\n    - y_pred: Predicted values (numpy array).\n\n    Returns:\n    - Mean squared error (float).\n    \"\"\"\n    return np.mean((y_true - y_pred) ** 2)\n
"},{"location":"algorithms/statistics/metrics-and-losses/errors/R2_Squared_Error/","title":"R2 Squared Error","text":"
import numpy as np\n\ndef r_squared(y_true, y_pred):\n    \"\"\"\n    Calculate the R-squared value between true and predicted values.\n\n    Parameters:\n    - y_true: True target values (numpy array).\n    - y_pred: Predicted values (numpy array).\n\n    Returns:\n    - R-squared value (float).\n    \"\"\"\n    total_variance = np.sum((y_true - np.mean(y_true)) ** 2)\n    explained_variance = np.sum((y_pred - np.mean(y_true)) ** 2)\n    r2 = 1 - (explained_variance / total_variance)\n    return r2\n
"},{"location":"algorithms/statistics/metrics-and-losses/errors/Root_Mean_Squared_Error/","title":"Root Mean Squared Error","text":"
import numpy as np\nimport math as mt\n\ndef root_mean_squared_error(y_true,y_pred):\n    \"\"\"\n    Calculate the root mean squared error between true and predicted values.\n\n    Parameters:\n    - y_true: True target values (numpy array).\n    - y_pred: Predicted values (numpy array).\n\n    Returns:\n    - Root Mean squared error (float).\n    \"\"\"\n    return mt.sqrt(np.mean((y_true - y_pred) ** 2))\n
"},{"location":"algorithms/statistics/metrics-and-losses/loss-functions/Cross_Entropy_Loss/","title":"Cross Entropy Loss","text":"
import numpy as np\n\ndef binary_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:\n    \"\"\"\n    Calculate the binary cross entropy loss between true and predicted values.\n    It measures the difference between the predicted probability distribution and the actual binary label distribution.\n    The formula for binary cross-entropy loss is as follows:\n\n    L(y, \u0177) = -[y * log(\u0177) + (1 \u2014 y) * log(1 \u2014 \u0177)]\n\n    where y is the true binary label (0 or 1), \u0177 is the predicted probability (ranging from 0 to 1), and log is the natural logarithm.\n\n    Parameters:\n    - y_true: True target values (numpy array).\n    - y_pred: Predicted values (numpy array).\n\n    Returns:\n    - Binary cross entropy loss (float).\n    \"\"\"\n    if (y_true is not None) and (y_pred is not None):\n        if type(y_true) == list:\n            y_true = np.asarray(y_true)\n        if type(y_pred) == list:\n            y_pred = np.asarray(y_pred)\n        assert y_true.shape == y_pred.shape, f\"Shape of y_true ({y_true.shape}) does not match y_pred ({y_pred.shape})\"\n        # calculate the binary cross-entropy loss\n        loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)).mean()\n        return loss\n    else:\n        return None\n\ndef weighted_binary_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list, w_pos: float, w_neg: float) -> float:\n    \"\"\"\n    Calculates the weighted binary cross entropy loss between true and predicted values.\n    Weighted Binary Cross-Entropy loss is a variation of the binary cross-entropy loss that allows for assigning different weights to positive and negative examples. This can be useful when dealing with imbalanced datasets, where one class is significantly underrepresented compared to the other.\n    The formula for weighted binary cross-entropy loss is as follows:\n\n    L(y, \u0177) = -[w_pos * y * log(\u0177) + w_neg * (1 \u2014 y) * log(1 \u2014 \u0177)]\n\n    where y is the true binary label (0 or 1), \u0177 is the predicted probability (ranging from 0 to 1), log is the natural logarithm, and w_pos and w_neg are the positive and negative weights, respectively.\n\n    Parameters:\n    - y_true: True target values (numpy array).\n    - y_pred: Predicted values (numpy array).\n\n    Returns:\n    - Weighted binary cross entropy loss (float).\n    \"\"\"\n    if (y_true is not None) and (y_pred is not None):\n        assert w_pos != 0.0, f\"Weight w_pos = {w_pos}\"\n        assert w_neg != 0.0, f\"Weight w_neg = {w_neg}\"\n        if type(y_true) == list:\n            y_true = np.asarray(y_true)\n        if type(y_pred) == list:\n            y_pred = np.asarray(y_pred)\n        assert y_true.shape == y_pred.shape, f\"Shape of y_true ({y_true.shape}) does not match y_pred ({y_pred.shape})\"\n        # calculate the binary cross-entropy loss\n        loss = -(w_pos * y_true * np.log(y_pred) + w_neg * (1 - y_true) * np.log(1 - y_pred)).mean()\n        return loss\n    else:\n        return None\n\n\ndef categorical_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:\n    \"\"\"\n    Calculate the categorical cross entropy loss between true and predicted values.\n    It measures the difference between the predicted probability distribution and the actual one-hot encoded label distribution.\n    The formula for categorical cross-entropy loss is as follows:\n\n    L(y, \u0177) = -1/N * \u03a3[\u03a3{y * log(\u0177)}]\n\n    where y is the true one-hot encoded label vector, \u0177 is the predicted probability distribution, and log is the natural logarithm.\n\n    Parameters:\n    - y_true: True target values (numpy array) (one-hot encoded).\n    - y_pred: Predicted values (numpy array) (probabilities).\n\n    Returns:\n    - Categorical cross entropy loss (float).\n    \"\"\"\n    if (y_true is not None) and (y_pred is not None):\n        if type(y_true) == list:\n            y_true = np.asarray(y_true)\n        if type(y_pred) == list:\n            y_pred = np.asarray(y_pred)\n        assert y_pred.ndim == 2, f\"Shape of y_pred should be (N, C), got {y_pred.shape}\"\n        assert y_true.shape == y_pred.shape, f\"Shape of y_true ({y_true.shape}) does not match y_pred ({y_pred.shape})\"\n\n        # Ensure numerical stability\n        y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)\n\n        # calculate the categorical cross-entropy loss\n        loss = -1/len(y_true) * np.sum(np.sum(y_true * np.log(y_pred)))\n        return loss.mean()\n    else:\n        return None\n\ndef sparse_categorical_cross_entropy_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:\n    \"\"\"\n    Calculate the sparse categorical cross entropy loss between true and predicted values.\n    It measures the difference between the predicted probability distribution and the actual class indices.\n    The formula for sparse categorical cross-entropy loss is as follows:\n\n    L(y, \u0177) = -\u03a3[log(\u0177[range(N), y])]\n\n    where y is the true class indices, \u0177 is the predicted probability distribution, and log is the natural logarithm.\n\n    Parameters:\n    - y_true: True target values (numpy array) (class indices).\n    - y_pred: Predicted values (numpy array) (probabilities).\n\n    Returns:\n    - Sparse categorical cross entropy loss (float).\n    \"\"\"\n    if (y_true is not None) and (y_pred is not None):\n        if type(y_true) == list:\n            y_true = np.asarray(y_true)\n        if type(y_pred) == list:\n            y_pred = np.asarray(y_pred)\n        assert y_true.shape[0] == y_pred.shape[0], f\"Batch size of y_true ({y_true.shape[0]}) does not match y_pred ({y_pred.shape[0]})\"\n\n        # convert true labels to one-hot encoding\n        y_true_onehot = np.zeros_like(y_pred)\n        y_true_onehot[np.arange(len(y_true)), y_true] = 1\n\n        # Ensure numerical stability\n        y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)\n\n        # calculate loss\n        loss = -np.mean(np.sum(y_true_onehot * np.log(y_pred), axis=-1))\n        return loss\n    else:\n        return None\n\n\nif __name__ == \"__main__\":\n    # define true labels and predicted probabilities\n    y_true = np.array([0, 1, 1, 0])\n    y_pred = np.array([0.1, 0.9, 0.8, 0.3])\n\n    print(\"\\nTesting Binary Cross Entropy Loss\")\n    print(\"Y_True: \", y_true)\n    print(\"Y_Pred:\", y_pred)\n    print(\"Binary Cross Entropy Loss: \", binary_cross_entropy_loss(y_true, y_pred))\n\n    positive_weight = 0.7\n    negative_weight = 0.3\n\n    print(\"\\nTesting Weighted Binary Cross Entropy Loss\")\n    print(\"Y_True: \", y_true)\n    print(\"Y_Pred:\", y_pred)\n    print(\"Weighted Binary Cross Entropy Loss: \", weighted_binary_cross_entropy_loss(y_true, y_pred, positive_weight, negative_weight))\n\n    y_true = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])\n    y_pred = np.array([[0.8, 0.1, 0.1], [0.2, 0.3, 0.5], [0.1, 0.6, 0.3]])\n    print(\"\\nTesting Categorical Cross Entropy Loss\")\n    print(\"Y_True: \", y_true)\n    print(\"Y_Pred:\", y_pred)\n    print(\"Categorical Cross Entropy Loss: \", categorical_cross_entropy_loss(y_true, y_pred))\n\n    y_true = np.array([1, 2, 0])\n    y_pred = np.array([[0.1, 0.8, 0.1], [0.3, 0.2, 0.5], [0.4, 0.3, 0.3]])\n    print(\"\\nTesting Sparse Categorical Cross Entropy Loss\")\n    print(\"Y_True: \", y_true)\n    print(\"Y_Pred:\", y_pred)\n    print(\"Sparse Categorical Cross Entropy Loss: \", sparse_categorical_cross_entropy_loss(y_true, y_pred))\n
"},{"location":"algorithms/statistics/metrics-and-losses/loss-functions/Hinge_Loss/","title":"Hinge Loss","text":"
import numpy as np\n\ndef hinge_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list)-> float:\n    \"\"\"\n    Calculates the hinge loss between true and predicted values.\n\n    The formula for hinge loss is as follows:\n\n    L(y, \u0177) = max(0, 1 - y * \u0177)\n\n    \"\"\"\n    if (y_true is not None) and (y_pred is not None):\n        if type(y_true) == list:\n            y_true = np.asarray(y_true)\n        if type(y_pred) == list:\n            y_pred = np.asarray(y_pred)\n        assert y_true.shape[0] == y_pred.shape[0], f\"Batch size of y_true ({y_true.shape[0]}) does not match y_pred ({y_pred.shape[0]})\"\n\n    # replacing 0 values to -1\n    y_pred = np.where(y_pred == 0, -1, 1)\n    y_true = np.where(y_true == 0, -1, 1)\n\n     # Calculate loss\n    loss = np.maximum(0, 1 - y_true * y_pred).mean()\n    return loss\n\nif __name__ == \"__main__\":\n    # define true labels and predicted probabilities\n    actual = np.array([1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1])\n    predicted = np.array([0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1])\n\n    print(\"\\nTesting Hinge Loss\")\n    print(\"Y_True: \", actual)\n    print(\"Y_Pred:\", predicted)\n    print(\"Hinge Loss: \", hinge_loss(actual, predicted))\n
"},{"location":"algorithms/statistics/metrics-and-losses/loss-functions/Kullback_Leibler_Divergence_Loss/","title":"KL Divergence Loss","text":"
Kullback Leibler Divergence Loss\n
import numpy as np\n\ndef kl_divergence_loss(y_true: np.ndarray | list, y_pred: np.ndarray | list) -> float:\n    \"\"\"\n    Calculate the Kullback-Leibler (KL) divergence between two probability distributions.\n    KL divergence measures how one probability distribution diverges from another reference probability distribution.\n\n    The formula for KL divergence is:\n    D_KL(P || Q) = \u03a3 P(x) * log(P(x) / Q(x))\n\n    where P is the true probability distribution and Q is the predicted probability distribution.\n\n    Parameters:\n    - y_true: True probability distribution (numpy array or list).\n    - y_pred: Predicted probability distribution (numpy array or list).\n\n    Returns:\n    - KL divergence loss (float).\n    \"\"\"\n    if (y_true is not None) and (y_pred is not None):\n        if type(y_true) == list:\n            y_true = np.asarray(y_true)\n        if type(y_pred) == list:\n            y_pred = np.asarray(y_pred)\n        assert y_true.shape == y_pred.shape, f\"Shape of p_true ({y_true.shape}) does not match q_pred ({y_pred.shape})\"\n\n        # Ensure numerical stability by clipping the probabilities\n        y_true = np.clip(y_true, 1e-15, 1)\n        y_pred = np.clip(y_pred, 1e-15, 1)\n\n        # Normalize the distributions\n        y_true /= y_true.sum(axis=-1, keepdims=True)\n        y_pred /= y_pred.sum(axis=-1, keepdims=True)\n\n        # Calculate KL divergence\n        kl_div = np.sum(y_true * np.log(y_true / y_pred), axis=-1)\n        return kl_div.mean()\n    else:\n        return None\n\nif __name__ == \"__main__\":\n    y_true = np.array([[0.2, 0.5, 0.3], [0.1, 0.7, 0.2]]) # True probability distributions\n    y_pred = np.array([[0.1, 0.6, 0.3], [0.2, 0.5, 0.3]]) # Predicted probability distributions\n\n    print(\"\\nTesting Kullback Leibler Divergence Loss\")\n    print(\"Y_True: \", y_true)\n    print(\"Y_Pred:\", y_pred)\n    print(\"Kullback Leibler Divergence Loss: \", kl_divergence_loss(y_true, y_pred))\n
"},{"location":"algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/","title":"Ranking Losses","text":""},{"location":"algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/#pair-wise-ranking-loss","title":"Pair Wise Ranking Loss","text":"
import tensorflow as tf\nfrom typing import Tuple\n\ndef pairwise_ranking_loss(y_true: tf.Tensor, y_pred: tf.Tensor, margin: float = 1.0) -> tf.Tensor:\n    \"\"\"\n    Computes the pairwise ranking loss for a batch of pairs.\n\n    Args:\n        y_true: Tensor of true labels (0 for negative pairs, 1 for positive pairs).\n        y_pred: Tensor of predicted similarities/distances, expected to be a tensor of shape (batch_size, 2, embedding_dim) where \n                y_pred[:, 0] is the anchor and y_pred[:, 1] is the positive/negative.\n        margin: Margin parameter for the pairwise ranking loss.\n\n    Returns:\n        loss: Computed pairwise ranking loss as a scalar tensor.\n    \"\"\"\n    anchor, positive_or_negative = y_pred[:, 0], y_pred[:, 1]\n\n    distances = tf.reduce_sum(tf.square(anchor - positive_or_negative), axis=-1)\n    positive_loss = y_true * distances\n    negative_loss = (1 - y_true) * tf.maximum(margin - distances, 0.0)\n\n    loss = positive_loss + negative_loss\n    return tf.reduce_mean(loss)\n\n# Example usage:\n# model.compile(optimizer='adam', loss=pairwise_ranking_loss)\n
"},{"location":"algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/#triplet-loss","title":"Triplet Loss","text":"
import tensorflow as tf\nfrom typing import Tuple\n\ndef triplet_loss_func(y_true: tf.Tensor, y_pred: tf.Tensor, alpha: float = 0.3) -> tf.Tensor:\n    \"\"\"\n    Computes the triplet loss for a batch of triplets.\n\n    Args:\n        y_true: True values of classification (unused in this implementation, typically required for compatibility with Keras).\n        y_pred: Predicted values, expected to be a tensor of shape (batch_size, 3, embedding_dim) where \n                y_pred[:, 0] is the anchor, y_pred[:, 1] is the positive, and y_pred[:, 2] is the negative.\n        alpha: Margin parameter for the triplet loss.\n\n    Returns:\n        loss: Computed triplet loss as a scalar tensor.\n    \"\"\"\n    anchor, positive, negative = y_pred[:, 0], y_pred[:, 1], y_pred[:, 2]\n\n    positive_dist = tf.reduce_sum(tf.square(anchor - positive), axis=-1)\n    negative_dist = tf.reduce_sum(tf.square(anchor - negative), axis=-1)\n\n    loss = tf.maximum(positive_dist - negative_dist + alpha, 0.0)\n    return tf.reduce_mean(loss)\n\n# Example usage:\n# model.compile(optimizer='adam', loss=triplet_loss_func)\n
"},{"location":"algorithms/statistics/probability/","title":"Probability \ud83d\udcc3","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"projects/","title":"Projects \ud83c\udf89","text":"Statistics

Understanding data through statistical analysis and inference methods.

Machine Learning

Dive into the world of algorithms and models in Machine Learning.

Deep Learning

Explore the fascinating world of deep learning.

Computer Vision

Learn computer vision with OpenCV for real-time image processing applications.

Natural Language Processing

Dive into how machines understand and generate human language.

Generative Adversarial Networks

Learn about the power of Generative Adversarial Networks for creative AI solutions.

Large Language Models

Explore the cutting-edge techniques behind large language models like GPT and BERT.

Artificial Intelligence

Explore the fundamentals and advanced concepts of Artificial Intelligence.

"},{"location":"projects/artificial-intelligence/","title":"Artificial Intelligence \ud83d\udca1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"projects/computer-vision/","title":"Computer Vision \ud83c\udfa5","text":"Face Detection Model

Detecting faces in images using OpenCV's powerful Haar cascades.

\ud83d\udcc5 2025-01-16 | \u23f1\ufe0f 10 mins

Counting Bicep Reps

Real-time tracking and counting of bicep curls with Mediapipe's Pose module and OpenCV.

\ud83d\udcc5 2025-01-18 | \u23f1\ufe0f 15 mins

Brightness Control

Adjust screen brightness dynamically using MediaPipe's Hand Tracking and OpenCV.

\ud83d\udcc5 2025-01-24 | \u23f1\ufe0f 10 mins

"},{"location":"projects/computer-vision/black_and_white_image_colorizer/","title":"Black and White Image Colorizer","text":""},{"location":"projects/computer-vision/black_and_white_image_colorizer/#aim","title":"AIM","text":"

Colorization of Black and White Images using OpenCV and pre-trained caffe models.

"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#pre-trained-models","title":"PRE-TRAINED MODELS","text":"

colorization_deploy_v2.prototxt - colorization_release_v2.caffemodel - pts_in_hull.npy

"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#notebook-link","title":"NOTEBOOK LINK","text":"

Colab Notebook

"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • numpy
  • cv2
"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • The project aims to perform colorization of black and white images.
  • It involves in showcase the capabilities of OpenCV's DNN module and caffe models.
  • It is done by processing given image using openCV and use Lab Color space model to hallucinate an approximation of how colorized version of the image \"might look like\".
Why is it necessary?
  • It helps preserving historical black-and-white photos.
  • It can be used adding color to grayscale images for creative industries.
  • It acts an advancing computer vision applications in artistic and research fields.
How is it beneficial and used?
  • Personal use : It helps in restoring old family photographs.
  • Cultural and Political : it also enhances grayscale photographs of important historic events for modern displays.
  • Creativity and Art : it improves AI-based creative tools for artists and designers.
How did you start approaching this project? (Initial thoughts and planning)
  • Initial approach : reading various research papers and analyze different approaches on how to deal with this project.
  • Identified Richzhang research paper on the title : Colorful Image colorization.
  • Did some research on pre-trained models for image colorization.
  • Understood OpenCV's DNN module and its implementation.
  • Experimented with sample images to test model outputs.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Richzhang's Colorful Image Colorization
  • Lab Color space
  • openCV Documentation
"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#explanation","title":"EXPLANATION","text":""},{"location":"projects/computer-vision/black_and_white_image_colorizer/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4

Initial data exploration and understanding:

  • Load the grayscale input image.
  • Load pre-trained caffe models using openCV dnn module.

Data cleaning and preprocessing:

  • Preprocess image to normalize and convert to LAB color space.
  • Resize image for the network.
  • Split L channel and perform mean subtraction.
  • Predict ab channel from the input of L channel.

Feature engineering and selection:

  • Resize predicted ab channel's volume to same dimension as our image.
  • Join L and predicted ab channel.
  • Convert image from Lab back to RGB.

Result :

  • Resize and Show the Original and Colorized image.
"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Computational efficiency vs. color accuracy.
  • Solution : Used optimized preprocessing pipelines to reduce runtime.
  • Pre-trained model generalization vs. custom training.
  • Solution : Choose the pre-trained model for faster implementation and reliable results.
"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#screenshots","title":"SCREENSHOTS","text":"

Project structure or tree diagram

  graph LR  \n    A[Load Grayscale Image] --> B[Preprocess Image];  \n    B --> C[Load Pre-trained Model];  \n    C --> D[Predict A and B Channels];  \n    D --> E[Combine with L Channel];  \n    E --> F[Convert to RGB];  \n    F --> G[Display/Save Colorized Image];
Visualizations of results Original ImageColorized ImageResult

"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/computer-vision/black_and_white_image_colorizer/#key-learnings","title":"KEY LEARNINGS","text":"

Insights gained from the data

  • Color Space : LAB color space facilitates colorization tasks.
  • Pre-trained Models : Pre-trained models can generalize across various grayscale images.
Improvements in understanding machine learning concepts
  • OpenCV : Enhanced knowledge of OpenCV's DNN module.
  • Caffe Models : Usage of pre-trained models.
  • Image Dimensionality : Understanding how Image can be manipulated.
Challenges faced and how they were overcome
  • Color Space Conversion : Initial difficulties with LAB to RGB conversion; resolved using OpenCV documentation.
"},{"location":"projects/computer-vision/black_and_white_image_colorizer/#use-cases","title":"USE CASES","text":"Application 1Application 2

Image Restoration

  • Restoring old family photographs to vivid colors.

Creative Industries

  • Colorizing artistic grayscale sketches for concept designs.
"},{"location":"projects/computer-vision/brightness-control/","title":"\ud83d\udcdc Brightness control","text":""},{"location":"projects/computer-vision/brightness-control/#aim","title":"\ud83c\udfaf AIM","text":"

To develop a real-time brightness control system using hand gestures, leveraging OpenCV and MediaPipe for hand detection and brightness adjustment.

"},{"location":"projects/computer-vision/brightness-control/#dataset-link","title":"\ud83d\udcca DATASET LINK","text":"

No dataset used

"},{"location":"projects/computer-vision/brightness-control/#notebook-link","title":"\ud83d\udcd3 NOTEBOOK LINK","text":"

https://drive.google.com/file/d/1q7kraajGykfc2Kb6-84dCOjkrDGhIQcy/view?usp=sharing

"},{"location":"projects/computer-vision/brightness-control/#tech-stack","title":"\u2699\ufe0f TECH STACK","text":"Category Technologies Languages Python Libraries/Frameworks OpenCV, NumPy, MediaPipe, cvzone Tools Jupyter Notebook, Local Python IDE"},{"location":"projects/computer-vision/brightness-control/#description","title":"\ud83d\udcdd DESCRIPTION","text":"

What is the requirement of the project?

  • The project requires a webcam to capture real-time video and detect hand gestures for brightness control.
How is it beneficial and used?
  • Allows users to control screen brightness without physical touch, making it useful for touchless interfaces.
  • Ideal for applications in smart home systems and assistive technologies.
How did you start approaching this project? (Initial thoughts and planning)
  • Identified the need for a touchless brightness control system.
  • Selected OpenCV for video processing and MediaPipe for efficient hand tracking.
  • Developed a prototype to calculate brightness based on hand distance.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • OpenCV documentation for video processing.
  • MediaPipe's official guide for hand tracking.
"},{"location":"projects/computer-vision/brightness-control/#explanation","title":"\ud83d\udd0d EXPLANATION","text":""},{"location":"projects/computer-vision/brightness-control/#details-of-the-different-features","title":"\ud83e\udde9 DETAILS OF THE DIFFERENT FEATURES","text":""},{"location":"projects/computer-vision/brightness-control/#developed-features","title":"\ud83d\udee0 Developed Features","text":"Feature Name Description Reason Hand Detection Detects hand gestures in real-time To control brightness with gestures Distance Calculation Calculates distance between fingers To adjust brightness dynamically Brightness Mapping Maps hand distance to brightness levels Ensures smooth adjustment of brightness"},{"location":"projects/computer-vision/brightness-control/#project-workflow","title":"\ud83d\udee4 PROJECT WORKFLOW","text":"

Project workflow

  graph LR\nA[Start] --> B[Initialize Webcam];\nB --> C[Detect Hand Gestures];\nC --> D[Calculate Distance];\nD --> E[Adjust Brightness];\nE --> F[Display Output];
Step 1
  • Initialize the webcam using OpenCV.
Step 2
  • Use MediaPipe to detect hands in the video feed.
Step 3
  • Calculate the distance between two fingers (e.g., thumb and index).
Step 4
  • Map the distance to a brightness range.
Step 5
  • Display the adjusted brightness on the video feed.
"},{"location":"projects/computer-vision/brightness-control/#code-explanation","title":"\ud83d\udda5 CODE EXPLANATION","text":"Section 1: Webcam Initialization
  • The program begins by setting up the webcam to capture frames with a resolution of 640x480 pixels. This ensures consistent processing and visualization of the video stream.

    cap = cv2.VideoCapture(0)  \ncap.set(3, 640)  # Set width  \ncap.set(4, 480)  # Set height \n
Section 2: Hand Detection and Brightness Control
  • Using the HandDetector from cvzone, the program tracks one hand (maxHands=1). The brightness of the video frame is dynamically adjusted based on the distance between the thumb and index finger.

    detector = HandDetector(detectionCon=0.8, maxHands=1)  \nbrightness = 1.0  # Default brightness level  \n
  • The HandDetector detects hand landmarks in each frame with a confidence threshold of 0.8. The initial brightness is set to 1.0 (normal).

    hands, img = detector.findHands(frame, flipType=False)  \n\nif hands:  \n    hand = hands[0]  \n    lm_list = hand['lmList']  \n    if len(lm_list) > 8:  \n        thumb_tip = lm_list[4]  \n        index_tip = lm_list[8]  \n        distance = int(((thumb_tip[0] - index_tip[0]) ** 2 + (thumb_tip[1] - index_tip[1]) ** 2) ** 0.5)  \n        brightness = np.interp(distance, [20, 200], [0, 1])  \n
  • The program calculates the distance between the thumb tip (lmList[4]) and index finger tip (lmList[8]). This distance is mapped to a brightness range of 0 to 1 using np.interp.

Section 3: Brightness Adjustment and Display
  • The captured frame's brightness is modified by scaling the value (V) channel in the HSV color space according to the calculated brightness level.

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  \nhsv[..., 2] = np.clip(hsv[..., 2] * brightness, 0, 255).astype(np.uint8)  \nframe_bright = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)  \ncv2.imshow(\"Brightness Controller\", frame_bright)  \n
  • This technique ensures smooth, real-time brightness adjustments based on the user's hand gestures. The output frame is displayed with the adjusted brightness level.

"},{"location":"projects/computer-vision/brightness-control/#project-trade-offs-and-solutions","title":"\u2696\ufe0f PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Real-time processing vs. computational efficiency: Optimized hand detection by limiting the maximum number of detectable hands to 1.
  • Precision in brightness control vs. usability: Adjusted mapping function to ensure smooth transitions.
"},{"location":"projects/computer-vision/brightness-control/#screenshots","title":"\ud83d\uddbc SCREENSHOTS","text":"Working of the model Image Topic"},{"location":"projects/computer-vision/brightness-control/#conclusion","title":"\u2705 CONCLUSION","text":""},{"location":"projects/computer-vision/brightness-control/#key-learnings","title":"\ud83d\udd11 KEY LEARNINGS","text":"

Insights gained from the data

  • Improved understanding of real-time video processing.
  • Learned to integrate gesture detection with hardware functionalities.
Improvements in understanding machine learning concepts
  • Gained insights into MediaPipe's efficient hand detection algorithms.
"},{"location":"projects/computer-vision/brightness-control/#use-cases","title":"\ud83c\udf0d USE CASES","text":"Smart Homes
  • Touchless brightness control for smart home displays.
Assistive Technologies
  • Brightness adjustment for users with limited mobility.
"},{"location":"projects/computer-vision/counting-bicep-reps/","title":"Counting Bicep Reps","text":""},{"location":"projects/computer-vision/counting-bicep-reps/#aim","title":"AIM","text":"

To track and count bicep curls in real time using computer vision techniques with OpenCV and Mediapipe's Pose module.

"},{"location":"projects/computer-vision/counting-bicep-reps/#dataset-link","title":"DATASET LINK","text":"

This project does not use a specific dataset as it works with real-time video from a webcam.

"},{"location":"projects/computer-vision/counting-bicep-reps/#notebook-link","title":"NOTEBOOK LINK","text":"

https://drive.google.com/file/d/13Omm8Zy0lmtjmdHgfQbraBu3NJf3wknw/view?usp=sharing

"},{"location":"projects/computer-vision/counting-bicep-reps/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • OpenCV
  • Mediapipe
  • NumPy
"},{"location":"projects/computer-vision/counting-bicep-reps/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • The project aims to provide a computer vision-based solution for tracking fitness exercises like bicep curls without the need for wearable devices or sensors.
Why is it necessary?
  • Helps fitness enthusiasts monitor their workouts in real time.
  • Provides an affordable and accessible alternative to wearable fitness trackers.
How is it beneficial and used?
  • Real-time feedback on workout form and repetition count.
  • Can be extended to other fitness exercises and integrated into fitness apps
How did you start approaching this project? (Initial thoughts and planning)
  • Explored Mediapipe's Pose module for pose landmark detection.
  • Integrated OpenCV for video frame processing and real-time feedback.
  • Planned the logic for detecting curls based on elbow angle thresholds.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Mediapipe official documentation.
  • OpenCV tutorials on video processing.
"},{"location":"projects/computer-vision/counting-bicep-reps/#explanation","title":"EXPLANATION","text":""},{"location":"projects/computer-vision/counting-bicep-reps/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"
- Pose Estimation: Utilized Mediapipe's Pose module to detect key landmarks on the human body.\n- Angle Calculation: Calculated angles at the elbow joints to determine curl movement.\n- Rep Tracking: Incremented rep count when alternating between full curl and relaxed positions.\n- Real-Time Feedback: Displayed the remaining curl count on the video feed.\n
"},{"location":"projects/computer-vision/counting-bicep-reps/#project-workflow","title":"PROJECT WORKFLOW","text":"Step 1

Initial setup:

  • Installed OpenCV and Mediapipe.
  • Set up a webcam feed for video capture.
Step 2

Pose detection:

  • Used Mediapipe's Pose module to identify body landmarks.
Step 3

Angle calculation:

  • Implemented a function to calculate the angle between shoulder, elbow, and wrist.
Step 4

Rep detection:

  • Monitored elbow angles to track upward and downward movements.
Step 5

Real-time feedback:

  • Displayed the remaining number of curls on the video feed using OpenCV.
Step 6

Completion:

  • Stopped the program when the target reps were completed or on manual exit.
"},{"location":"projects/computer-vision/counting-bicep-reps/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Accuracy vs. Simplicity:
    • Using elbow angles alone may not handle all body postures but ensures simplicity.
    • Solution: Fine-tuned angle thresholds and added tracking for alternating arms.
  • Real-Time Performance vs. Model Complexity:
    • Mediapipe's lightweight solution ensured smooth processing over heavier models.
"},{"location":"projects/computer-vision/counting-bicep-reps/#screenshots","title":"SCREENSHOTS","text":"
  1. Entering no of reps you want to perform

  2. Performing reps

Project workflow

graph LR  \nA[Webcam Feed] --> F[Enter No of Biceps Reps]\nF --> B[Mediapipe Pose Detection]  \nB --> C[Elbow Angle Calculation]  \nC --> D[Rep Count Decrement]  \nD --> E[Real-Time Update on Frsame]  
"},{"location":"projects/computer-vision/counting-bicep-reps/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/computer-vision/counting-bicep-reps/#key-learnings","title":"KEY LEARNINGS","text":"

Insights gained from the data

  • Real-time video processing using OpenCV.
  • Pose detection and landmark analysis with Mediapipe.
Improvements in understanding machine learning concepts
  • Understanding geometric computations in pose analysis.
  • Effective use of pre-trained models like Mediapipe Pose.
Challenges faced and how they were overcome
  • Challenge: Handling incorrect postures.
    • Solution: Fine-tuning angle thresholds.
"},{"location":"projects/computer-vision/counting-bicep-reps/#use-cases","title":"USE CASES","text":"Application 1Application 2

Personal Fitness Tracker - Helps users track their workouts without additional equipment.

Fitness App Integration - Can be integrated into fitness apps for real-time exercise tracking.

"},{"location":"projects/computer-vision/face-detection/","title":"Face Detection","text":""},{"location":"projects/computer-vision/face-detection/#aim","title":"AIM","text":"

The goal of this project is to build a face detection system using OpenCV, which identifies faces in static images using Haar Cascades.

"},{"location":"projects/computer-vision/face-detection/#dataset-link","title":"DATASET LINK","text":"

For this project we are going to use the pretrained Haar Cascade XML file for face detection from OpenCV's Github repository.

https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml

"},{"location":"projects/computer-vision/face-detection/#notebook-link","title":"NOTEBOOK LINK","text":"

https://colab.research.google.com/drive/1upcl9sa5cL5fUuVLBG5IVuU0xPYs3Nwf#scrollTo=94ggAdg5AnUk

"},{"location":"projects/computer-vision/face-detection/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • OpenCV
  • Random
  • Matplotlib
"},{"location":"projects/computer-vision/face-detection/#description","title":"DESCRIPTION","text":"

This project involves building a face detection model using OpenCV's pre-trained Haar Cascade Classifiers to detect faces in images.

What is the requirement of the project?

  • A face detection system is needed for various applications such as security, attendance tracking, and facial recognition systems.
  • This project demonstrates a basic use of computer vision techniques for detecting faces in static images.
Why is it necessary?
  • Face detection is the first crucial step in many computer vision applications such as face recognition and emotion analysis.
  • It is an essential component in systems that require human identification or verification.
How is it beneficial and used?
  • Face detection can be used in automation systems, for example, in attendance tracking, photo tagging, and security surveillance.
  • It enables various applications in user experience enhancement and biometric systems.
How did you start approaching this project? (Initial thoughts and planning)
  • I began by exploring OpenCV documentation, focusing on how to implement Haar Cascade for face detection.
  • Initially, I focused on static image detection, planning to extend the project to video-based detection in the future.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • OpenCV documentation
  • Book: \"Learning OpenCV 3\" by Adrian Kaehler and Gary Bradski
"},{"location":"projects/computer-vision/face-detection/#explanation","title":"EXPLANATION","text":""},{"location":"projects/computer-vision/face-detection/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"
- Haar Cascade Classifier: A machine learning-based approach for detecting objects in images or video. It works by training on a large set of positive and negative images of faces.\n- Cascade Classifier*: The classifier works through a series of stages, each aimed at increasing detection accuracy.\n- Face Detection: The primary feature of this project is detecting human faces in static images, which is the first step in many facial recognition systems.\n
"},{"location":"projects/computer-vision/face-detection/#project-workflow","title":"PROJECT WORKFLOW","text":"Step 1

Initial data exploration and understanding:

  • Research the Haar Cascade method for face detection in OpenCV.
  • Collect sample images for testing the model's performance.
Step 2

Data cleaning and preprocessing:

  • Ensure all input images are properly formatted (e.g., grayscale images for face detection).
  • Resize or crop images to ensure optimal processing speed.
Step 3Step 4Step 5

Feature engineering and selection: - Use pre-trained Haar Cascade classifiers for detecting faces. - Select the appropriate classifier based on face orientation and conditions (e.g., frontal face, profile).

Model training and evaluation: - Use OpenCV's pre-trained Haar Cascade models. - Test the detection accuracy on various sample images.

Model optimization and fine-tuning:

  • Adjust parameters such as scale factor and minNeighbors to enhance accuracy.
  • Experiment with different input image sizes to balance speed and accuracy.
Step 6

Validation and testing:

  • Validate the model's effectiveness on different test images, ensuring robust detection.
  • Evaluate the face detection accuracy based on diverse lighting and image conditions.
"},{"location":"projects/computer-vision/face-detection/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Accuracy vs. computational efficiency.
    • Solution: Fine-tuned classifier parameters to ensure a balance between accuracy and speed.
  • Detection performance vs. image resolution.
    • Solution: Optimized input image resolution and processing flow to ensure both fast processing and accurate detection.
"},{"location":"projects/computer-vision/face-detection/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\nA[Start] --> B{Face Detected?}\nB -->|Yes| C[Mark Face]\nC --> D[Display Result]\nB -->|No| F[Idle/Do Nothing]
"},{"location":"projects/computer-vision/face-detection/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/computer-vision/face-detection/#key-learnings","title":"KEY LEARNINGS","text":"

Insights gained from the data

  • Gained an understanding of face detection using Haar Cascades.
  • Improved ability to optimize computer vision models for accuracy and speed.
Improvements in understanding machine learning concepts
  • Learned how to handle trade-offs between accuracy and speed in real-time applications.
  • Gained hands-on experience with the implementation of object detection algorithms.
Challenges faced and how they were overcome
  • Challenge: Low detection accuracy in poor lighting conditions.
  • Solution: Adjusted classifier parameters and added preprocessing steps to improve accuracy.
"},{"location":"projects/computer-vision/face-detection/#use-cases","title":"USE CASES","text":"Application 1Application 2

Security Surveillance Systems

  • Used for identifying individuals or monitoring for intruders in secure areas.

Attendance Systems

  • Used to automate attendance tracking by detecting the faces of students or employees.
"},{"location":"projects/computer-vision/music_genre_classification_model/","title":"Music Genre Classification Model","text":""},{"location":"projects/computer-vision/music_genre_classification_model/#aim","title":"AIM","text":"

To develop a precise and effective music genre classification model using Convolutional Neural Networks (CNN), Support Vector Machines (SVM), Random Forest and XGBoost Classifier algorithms for the Kaggle GTZAN Dataset Music Genre Classification.

"},{"location":"projects/computer-vision/music_genre_classification_model/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/andradaolteanu/gtzan-dataset-music-genre-classification/data

"},{"location":"projects/computer-vision/music_genre_classification_model/#my-notebook-link","title":"MY NOTEBOOK LINK","text":"

https://colab.research.google.com/drive/1j8RZccP2ee5XlWEFSkTyJ98lFyNrezHS?usp=sharing

"},{"location":"projects/computer-vision/music_genre_classification_model/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • librosa
  • matplotlib
  • pandas
  • sklearn
  • seaborn
  • numpy
  • scipy
  • xgboost
"},{"location":"projects/computer-vision/music_genre_classification_model/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • The objective of this research is to develop a precise and effective music genre classification model using Convolutional Neural Networks (CNN), Support Vector Machines (SVM), Random Forest and XGBoost algorithms for the Kaggle GTZAN Dataset Music Genre Classification.
Why is it necessary?
  • Music genre classification has several real-world applications, including music recommendation, content-based music retrieval, and personalized music services. However, the task of music genre classification is challenging due to the subjective nature of music and the complexity of audio signals.
How is it beneficial and used?
  • For User: Provides more personalised music
  • For Developers: A recommendation system for songs that are of interest to the user
  • For Business: Able to charge premium for the more personalised and recommendation services provided
How did you start approaching this project? (Initial thoughts and planning)
  • Initially how the different sounds are structured.
  • Learned how to represent sound signal in 2D format on graphs using the librosa library.
  • Came to know about the various features of sound like
    • Mel-frequency cepstral coefficients (MFCC)
    • Chromagram
    • Spectral Centroid
    • Zero-crossing rate
    • BPM - Beats Per Minute
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • https://scholarworks.calstate.edu/downloads/73666b68n
  • https://www.kaggle.com/datasets/andradaolteanu/gtzan-dataset-music-genre-classification/data
  • https://towardsdatascience.com/music-genre-classification-with-python-c714d032f0d8
"},{"location":"projects/computer-vision/music_genre_classification_model/#explanation","title":"EXPLANATION","text":""},{"location":"projects/computer-vision/music_genre_classification_model/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"
There are 3 different types of the datasets.\n\n- genres_original\n- images_original\n- features_3_sec.csv\n- feature_30_sec.csv\n
  • The features in genres_original

    ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock'] Each and every genre has 100 WAV files

  • The features in genres_original

    ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock'] Each and every genre has 100 PNG files

  • There are 60 features in features_3_sec.csv

  • There are 60 features in features_30_sec.csv

"},{"location":"projects/computer-vision/music_genre_classification_model/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5Step 6Step 7
  • Created data visual reprsentation of the data to help understand the data
  • Found strong relationships between independent features and dependent feature using correlation.
  • Performed Exploratory Data Analysis on data.
  • Used different Classification techniques like SVM, Random Forest,
  • Compared various models and used best performance model to make predictions.
  • Used Mean Squared Error and R2 Score for evaluating model's performance.
  • Visualized best model's performance using matplotlib and seaborn library.
"},{"location":"projects/computer-vision/music_genre_classification_model/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2Trade Off 3Trade Off 4

How do you visualize audio signal

  • Solution:

  • librosa: It is the mother of all audio file libraries

  • Plotting Graphs: As I have the necessary libraries to visualize the data. I started plotting the audio signals
  • Spectogram:A spectrogram is a visual representation of the spectrum of frequencies of a signal as it varies with time. When applied to an audio signal, spectrograms are sometimes called sonographs, voiceprints, or voicegrams. Here we convert the frequency axis to a logarithmic one.

Features that help classify the data

  • Solution:

  • Feature Engineering: What are the features present in audio signals

  • Spectral Centroid: Indicates where the \u201dcentre of mass\u201d for a sound is located and is calculated as the weighted mean of the frequencies present in the sound.
  • Mel-Frequency Cepstral Coefficients: The Mel frequency cepstral coefficients (MFCCs) of a signal are a small set of features (usually about 10\u201320) which concisely describe the overall shape of a spectral envelope. It models the characteristics of the human voice.
  • Chroma Frequencies: Chroma features are an interesting and powerful representation for music audio in which the entire spectrum is projected onto 12 bins representing the 12 distinct semitones (or chroma) of the musical octave.

Performing EDA on the CSV files

  • Solution:

  • Tool Selection: Used the correlation matrix on the features_30_sec.csv dataset to extract most related datasets

  • Visualization Best Practices: Followed best practices such as using appropriate chart types (e.g., box plots for BPM data, PCA plots for correlations), adding labels and titles, and ensuring readability.
  • Iterative Refinement: Iteratively refined visualizations based on feedback and self-review to enhance clarity and informativeness.

Implementing Machine Learning Models

  • Solution:

  • Cross-validation: Used cross-validation techniques to ensure the reliability and accuracy of the analysis results.

  • Collaboration with Experts: Engaged with Music experts and enthusiasts to validate the findings and gain additional perspectives.
  • Contextual Understanding: Interpreted results within the context of the music, considering factors such as mood of the users, surrounding, and specific events to provide meaningful and actionable insights.
"},{"location":"projects/computer-vision/music_genre_classification_model/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Error?};\n    B -->|Yes| C[Hmm...];\n    C --> D[Debug];\n    D --> B;\n    B ---->|No| E[Yay!];
Visualizations and EDA of different features Harm PercSound WaveSTFTPop Mel-SpecBlues Mel-SpecSpec CentSpec RolloffMFCCChromogramCorr HeatmapBPM BoxplotPCA Scatter PlotConfusion Matrix

"},{"location":"projects/computer-vision/music_genre_classification_model/#models-used-and-their-accuracies","title":"MODELS USED AND THEIR ACCURACIES","text":"Model Accuracy KNN 0.80581 Random Forest 0.81415 Cross Gradient Booster 0.90123 SVM 0.75409"},{"location":"projects/computer-vision/music_genre_classification_model/#models-comparison-graphs","title":"MODELS COMPARISON GRAPHS","text":"

Models Comparison Graphs

ACC Plot

"},{"location":"projects/computer-vision/music_genre_classification_model/#conclusion","title":"CONCLUSION","text":"
We can see that Accuracy plots of the different models.\nXGB Classifier can predict most accurate results for predicting the Genre of the music.\n
"},{"location":"projects/computer-vision/music_genre_classification_model/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  • Discovered a new library that help visualize audio signal
  • Discovered new features related to audio like STFT, MFCC, Spectral Centroid, Spectral Rolloff
  • Gained a deeper understanding of the features of different genres of music
Improvements in understanding machine learning concepts
  • Enhanced knowledge of data cleaning and preprocessing techniques to handle real-world datasets.
  • Improved skills in exploratory data analysis (EDA) to extract meaningful insights from raw data.
  • Learned how to use visualization tools to effectively communicate data-driven findings.
"},{"location":"projects/computer-vision/music_genre_classification_model/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1Application 2

User Personalisation

  • It can be used to provide more personalised music recommendation for users based on their taste in music or the various genres they listen to. This personalisation experience can be used to develop 'Premium' based business models.

Compatability Between Users

  • Based on the musical taste and the genres they listen we can identify the user behaviour and pattern come with similar users who can be friends with. This increases social interaction within the app.
"},{"location":"projects/computer-vision/music_genre_classification_model/#features-planned-but-not-implemented","title":"FEATURES PLANNED BUT NOT IMPLEMENTED","text":"Feature 1Feature 1
  • Real-time Compatability Tracking

  • Implementing a real-time tracking system to view compatability between users.

  • Predictive Analytics

  • Using advanced machine learning algorithms to predict the next song the users is likely to listen to.

"},{"location":"projects/deep-learning/","title":"Deep Learning \u2728","text":"Brain Tumor Detection Model

Deep learning algorithm for image and video recognition.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 10 mins

"},{"location":"projects/deep-learning/brain-tumor-detection-model/","title":"Brain Tumor Detectioon","text":""},{"location":"projects/deep-learning/brain-tumor-detection-model/#aim","title":"AIM","text":"

To predict the Brain Tumor using Convolutional Neural Network

"},{"location":"projects/deep-learning/brain-tumor-detection-model/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/primus11/brain-tumor-mri

"},{"location":"projects/deep-learning/brain-tumor-detection-model/#my-notebook-link","title":"MY NOTEBOOK LINK","text":"

https://colab.research.google.com/github/11PRIMUS/ALOK/blob/main/Tumor3.ipynb

"},{"location":"projects/deep-learning/brain-tumor-detection-model/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn (>=1.5.0 for TunedThresholdClassifierCV)
  • matplotlib
  • seaborn
  • streamlit
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • This project aims to predict early stage brain tumor it uses Convolutional Neural Network to classify wheter tumor is present or not.
Why is it necessary?
  • Brain Tumor is leading case of deaths on world and most of the cases can be solved by detecting the cancer in its initial stages so one can take medication according to that without having further risks.
How is it beneficial and used?
  • Doctors can use it to detect cancer and the region affected by that using MRI scans an help patient to overcom ethat with right and proper guidance. It also acts as a fallback mechanism in rare cases where the diagnosis is not obvious.
  • People (patients in particular) can check simply by using MRI scans to detect Tumor and take necessary medication and precautions
How did you start approaching this project? (Initial thoughts and planning)
  • Going through previous research and articles related to the problem.
  • Data exploration to understand the features.
  • Identifying key metrics for the problem based on ratio of target classes.
  • Feature engineering and selection based on EDA.
  • Setting up a framework for easier testing of multiple models even for peoples.
  • Analysing results of models simply using MRI scans
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Research paper: Review of MRI-based Brain Tumor Image Segmentation Using Deep Learning Methods
  • Public notebook: Brain Tumor Classification
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#model-architecture","title":"Model Architecture","text":"
- The CNN architecture is designed to perform binary classification. The key layers used in the architecture are:\n- Convolutional Layers: For feature extraction from images. MaxPooling Layers: To downsample the image features. Dense Layers: To perform the classification. Dropout: For regularization to prevent overfitting.\n
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#model-structure","title":"Model Structure","text":"
- Input Layer 224*224 pixels.\n- Convolutionla layer followed by MaxPooling layers.\n- Flattern layer to convert feature into 1D vector\n- Fully connected layer for Classification.\n- Output Layer: Sigmoid activation for binary classification (tumor/no tumor)\n
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5

Exploratory Data Analysis

  • Summary statistics
  • Data visualization for numerical feature distributions
  • Splitting of data (70% for training, 15% for validation and 15% for testing)

Data cleaning and Preprocessing

  • Data preperation using Image Generator
  • Categorical feature encoding
  • Image resized to 224*224 pixels

Feature engineering and selection

  • Combining original features based on domain knowledge
  • Using MobileNet to process input

Modeling

  • Convolutional layer followed by MaxPooling layer
  • Flattering the layer to convert features into 1D vector
  • Sigmoid function to actiavte binary classification
  • Holdout dataset created or model testing
  • Using VGG16 and ResNet for future improvement

Result analysis

  • Hosted on Streamlit to ensure one can easily upload MRI scans and detect wether canncer is present or not.
  • Early stopping to ensure better accuracy is achieved.
  • Experiment with differnt agumentation techniques to improve model's robustness.
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1

Accuracy vs Validation_accuracy: The training accuracy is much higher than the validation accuracy after epoch 2, suggesting that the model may be overfitting the training data.

  • Solution: It might be better to stop training around epoch 2 or 3 to avoid overfitting and ensure better generalization.
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/deep-learning/brain-tumor-detection-model/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  • Early detection of cancer can lead to easily reduce the leading death of person and take proper medication on that basis.
Improvements in understanding machine learning concepts
  • Learned and implemented the concept of predicting probability and tuning the prediction threshold for more accurate results, compared to directly predicting with the default thresold for models.
Challenges faced and how they were overcome
  • Resigning the RGB image to grayscale and reducing the pixel by 225 * 225 was big challenge.
  • Lesser dataset so we reached out to some hospitals which helped us collecting the MRI scans.
"},{"location":"projects/deep-learning/brain-tumor-detection-model/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1Application 2
  • Doctors can identify the cancer stage and type accurately, allowing for tailored treatment approaches.
  • Treatments at early stages are often less invasive and have fewer side effects compared to late-stage therapies
"},{"location":"projects/generative-adversarial-networks/","title":"Generative Adversarial Networks \ud83d\udcb1","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"projects/large-language-models/","title":"Large Language Models \ud83e\udd2a","text":"No Items Found

There are no items available at this time. Check back again later.

"},{"location":"projects/machine-learning/","title":"Machine Learning \ud83e\udd16","text":"Air Quality Prediction Model

Breathing Insights: Predicting Air Quality with Precision, One Sensor at a Time!

\ud83d\udcc5 2025-01-26 | \u23f1\ufe0f 9 mins

"},{"location":"projects/machine-learning/air-quality-prediction/","title":"Air Quality Prediction Model","text":""},{"location":"projects/machine-learning/air-quality-prediction/#aim","title":"\ud83c\udfaf AIM","text":"

To predict air quality levels based on various features such as CO (Carbon Monoxide), NO (Nitrogen Oxides), NO2 (Nitrogen Dioxide), O3 (Ozone), and other environmental factors. By applying machine learning models, this project explores how different algorithms perform in predicting air quality and understanding the key factors that influence it.

"},{"location":"projects/machine-learning/air-quality-prediction/#dataset-link","title":"\ud83d\udcca DATASET LINK","text":"

https://www.kaggle.com/datasets/fedesoriano/air-quality-data-set

"},{"location":"projects/machine-learning/air-quality-prediction/#notebook","title":"\ud83d\udcd3 NOTEBOOK","text":"

https://www.kaggle.com/code/disha520/air-quality-predictor

Kaggle Notebook

"},{"location":"projects/machine-learning/air-quality-prediction/#tech-stack","title":"\u2699\ufe0f TECH STACK","text":"Category Technologies Languages Python Libraries/Frameworks Pandas, Numpy, Matplotlib, Seaborn, Scikit-learn Tools Git, Jupyter, VS Code"},{"location":"projects/machine-learning/air-quality-prediction/#description","title":"\ud83d\udcdd DESCRIPTION","text":"

The project focuses on predicting air quality levels based on the features of air pollutants and environmental parameters. The objective is to test various regression models to see which one gives the best predictions for CO (Carbon Monoxide) levels.

What is the requirement of the project?

  • Air quality is a critical issue for human health, and accurate forecasting models can provide insights to policymakers and the public.
  • To accurately predict the CO levels based on environmental data.
How is it beneficial and used?
  • Predicting air quality can help in early detection of air pollution and assist in controlling environmental factors effectively.
  • This model can be used by environmental agencies, city planners, and policymakers to predict and manage air pollution in urban areas, contributing to better public health outcomes.
How did you start approaching this project? (Initial thoughts and planning)
  • Began by cleaning the dataset, handling missing data, and converting categorical features into numerical data.
  • After preparing the data, various machine learning models were trained and evaluated to identify the best-performing model.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Kaggle kernels and documentation for additional dataset understanding.
  • Tutorials on machine learning regression techniques, particularly for Random Forest, SVR, and Decision Trees.
"},{"location":"projects/machine-learning/air-quality-prediction/#explanation","title":"\ud83d\udd0d EXPLANATION","text":""},{"location":"projects/machine-learning/air-quality-prediction/#details-of-the-different-features","title":"\ud83e\udde9 DETAILS OF THE DIFFERENT FEATURES","text":""},{"location":"projects/machine-learning/air-quality-prediction/#airqualitycsv","title":"\ud83d\udcc2 AirQuality.csv","text":"Feature Name Description CO(GT) Carbon monoxide in the air Date & Time Record of data collection time PT08.S1(CO), PT08.S2(NMHC), PT08.S3(NOX), PT08.S4(NO2), PT08.S5(O3) These are sensor readings for different gas pollutants T, RH, AH Temperature, Humidity, and Absolute Humidity respectively, recorded as environmental factors"},{"location":"projects/machine-learning/air-quality-prediction/#project-workflow","title":"\ud83d\udee4 PROJECT WORKFLOW","text":"
  graph LR\n    A[Start] --> B{Is data clean?};\n    B -->|Yes| C[Explore Data];\n    C --> D[Data Preprocessing];\n    D --> E[Feature Selection & Engineering];\n    E --> F[Split Data into Training & Test Sets];\n    F --> G[Define Models];\n    G --> H[Train and Evaluate Models];\n    H --> I[Visualize Evaluation Metrics];\n    I --> J[Model Testing];\n    J --> K[Conclusion and Observations];\n    B ---->|No| L[Clean Data];
Import Necessary LibrariesLoad DatasetData Cleaning ProcessVisualizing Correlations Between FeaturesData Preparation - Features (X) and Target (y)Split the Data into Training and Test SetsDefine ModelsTrain and Evaluate Each ModelVisualizing Model Evaluation MetricsConclusion and Observations
  • First, we import all the essential libraries needed for handling, analyzing, and modeling the dataset.
  • This includes libraries like Pandas for data manipulation, Numpy for numerical computations, Matplotlib and Seaborn for data visualization, and Scikit-learn for machine learning models, evaluation, and data preprocessing.
  • These libraries will enable us to perform all required tasks efficiently.
  • We load the dataset using Pandas read_csv() function. The dataset contains air quality data, which is loaded with a semicolon delimiter.
  • After loading, we inspect the first few rows to understand the structure of the data and ensure that the dataset is correctly loaded.

Data cleaning is a crucial step in any project. In this step:

  • We remove unnamed columns that aren't useful for analysis (such as 'Unnamed: 15', 'Unnamed: 16').
  • We correct data consistency issues, specifically replacing commas with periods in numeric columns to ensure the correct parsing of values.
  • Missing values in numeric columns are replaced with the mean of that respective column.
  • We eliminate rows that consist entirely of missing values (NaN).
  • A new datetime feature is created by combining the 'Date' and 'Time' columns.
  • Additional temporal features such as month, day, weekday, and hour are derived from the new datetime feature.
  • The original Date and Time columns are dropped as they are no longer needed.
  • To understand relationships among the features, a heatmap is used to visualize correlations between all numeric columns.
  • The heatmap highlights how features are correlated with each other, helping to identify possible redundancies or important predictors for the target variable.

After cleaning the data, we separate the dataset into features (X) and the target variable (y):

  • Features (X): These are the columns used to predict the target value. We exclude the target variable column \u2018CO(GT)\u2019 and include all other columns as features.
  • Target (y): This is the variable we want to predict. We extract the 'CO(GT)' column and ensure all values are numeric.

To prepare the data for machine learning, any non-numeric columns in the features (X) are encoded using LabelEncoder.

  • We split the dataset into training and testing sets, allocating 80% of the data for training and the remaining 20% for testing.
  • This split allows us to evaluate model performance on unseen data and validate the effectiveness of the model.

We define multiple regression models to train and evaluate on the dataset:

  • RandomForestRegressor: A robust ensemble method that performs well on non-linear datasets.
  • LinearRegression: A fundamental regression model, useful for establishing linear relationships.
  • SVR (Support Vector Regression): A regression model based on Support Vector Machines, useful for complex, non-linear relationships.
  • DecisionTreeRegressor: A decision tree-based model, capturing non-linear patterns and interactions.

Each model is trained on the training data and used to make predictions on the testing set. The performance is evaluated using two metrics:

  • Mean Absolute Error (MAE): Measures the average error between predicted and actual values.
  • R2 Score: Represents the proportion of the variance in the target variable that is predictable from the features.

The evaluation metrics for each model are stored for comparison.

We visualize the evaluation results for all models to get a comparative view of their performances. Two plots are generated:

  • Mean Absolute Error (MAE) for each model, showing how much deviation there is between predicted and actual values.
  • R2 Score, depicting the models' ability to explain the variability in the target variable. Higher R2 values indicate a better fit.

These visualizations make it easy to compare model performances and understand which model is performing the best.

  • In this final step, we summarize the results and draw conclusions based on the evaluation metrics. We discuss which model achieved the best performance in terms of both MAE and R2 Score, along with insights from the data cleaning and feature engineering steps.
  • Key observations include the importance of feature selection, the efficacy of different models for regression tasks, and which model has the most accurate predictions based on the dataset at hand.
"},{"location":"projects/machine-learning/air-quality-prediction/#code-explanation","title":"\ud83d\udda5 CODE EXPLANATION","text":""},{"location":"projects/machine-learning/air-quality-prediction/#project-trade-offs-and-solutions","title":"\u2696\ufe0f PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Trade-off: Choosing between model accuracy and training time.
  • Solution: Random Forest was chosen due to its balance between accuracy and efficiency, with SVR considered for its powerful predictive power despite longer training time.
  • Trade-off: Model interpretability vs complexity.
  • Solution: Decision trees were avoided in favor of Random Forest, which tends to be more robust in dealing with complex data and prevents overfitting.
"},{"location":"projects/machine-learning/air-quality-prediction/#screenshots","title":"\ud83d\uddbc SCREENSHOTS","text":"

Visualizations and EDA of different features

HeatMapModel Comparison

"},{"location":"projects/machine-learning/air-quality-prediction/#models-used-and-their-evaluation-metrics","title":"\ud83d\udcc9 MODELS USED AND THEIR EVALUATION METRICS","text":"Model Mean Absolute Error (MAE) R2 Score Random Forest Regressor 1.2391 0.885 Linear Regression 1.4592 0.82 SVR 1.3210 0.843 Decision Tree Regressor 1.5138 0.755"},{"location":"projects/machine-learning/air-quality-prediction/#conclusion","title":"\u2705 CONCLUSION","text":""},{"location":"projects/machine-learning/air-quality-prediction/#key-learnings","title":"\ud83d\udd11 KEY LEARNINGS","text":"

Insights gained from the data

  • Learned how different machine learning models perform on real-world data and gained insights into their strengths and weaknesses.
  • Understood the significance of feature engineering and preprocessing to achieve better model performance.
  • Data had missing values that required filling.
  • Feature creation from datetime led to better prediction accuracy.
Improvements in understanding machine learning concepts
  • Learned how to effectively implement and optimize machine learning models using libraries like scikit-learn.
"},{"location":"projects/machine-learning/air-quality-prediction/#use-cases","title":"\ud83c\udf0d USE CASES","text":"Application 1Application 2

Predicting Air Quality in Urban Areas

  • Local governments can use this model to predict air pollution levels and take early actions to reduce pollution in cities.

Predicting Seasonal Air Pollution Levels

  • The model can help forecast air quality during different times of the year, assisting in long-term policy planning.
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/","title":"Cardiovascular Disease Prediction","text":""},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#aim","title":"AIM","text":"

To predict the risk of cardiovascular disease based on lifestyle factors.

"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/alphiree/cardiovascular-diseases-risk-prediction-dataset

"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#my-notebook-link","title":"MY NOTEBOOK LINK","text":"

https://www.kaggle.com/code/sid4ds/cardiovascular-disease-risk-prediction

"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn (>=1.5.0 for TunedThresholdClassifierCV)
  • matplotlib
  • seaborn
  • joblib
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • This project aims to predict the risk of cardivascular diseases (CVD) based on data provided by people about their lifestyle factors. Predicting the risk in advance can minimize cases which reach a terminal stage.
Why is it necessary?
  • CVD is one of the leading causes of death globally. Using machine learning models to predict risk of CVD can be an important tool in helping the people affected by it.
How is it beneficial and used?
  • Doctors can use it as a second opinion to support their diagnosis. It also acts as a fallback mechanism in rare cases where the diagnosis is not obvious.
  • People (patients in particular) can track their risk of CVD based on their own lifestyle and schedule an appointment with a doctor in advance to mitigate the risk.
How did you start approaching this project? (Initial thoughts and planning)
  • Going through previous research and articles related to the problem.
  • Data exploration to understand the features. Using data visualization to check their distributions.
  • Identifying key metrics for the problem based on ratio of target classes.
  • Feature engineering and selection based on EDA.
  • Setting up a framework for easier testing of multiple models.
  • Analysing results of models using confusion matrix.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Research paper: Integrated Machine Learning Model for Comprehensive Heart Disease Risk Assessment Based on Multi-Dimensional Health Factors
  • Public notebook: Cardiovascular-Diseases-Risk-Prediction
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#explanation","title":"EXPLANATION","text":""},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"Feature Name Description Type Values/Range General_Health \"Would you say that in general your health is\u2014\" Categorical [Poor, Fair, Good, Very Good, Excellent] Checkup \"About how long has it been since you last visited a doctor for a routine checkup?\" Categorical [Never, 5 or more years ago, Within last 5 years, Within last 2 years, Within the last year] Exercise \"Did you participate in any physical activities like running, walking, or gardening?\" Categorical [Yes, No] Skin_Cancer Respondents that reported having skin cancer Categorical [Yes, No] Other_Cancer Respondents that reported having any other types of cancer Categorical [Yes, No] Depression Respondents that reported having a depressive disorder Categorical [Yes, No] Diabetes Respondents that reported having diabetes. If yes, specify the type. Categorical [Yes, No, No pre-diabetes or borderline diabetes, Yes but female told only during pregnancy] Arthritis Respondents that reported having arthritis Categorical [Yes, No] Sex Respondent's gender Categorical [Yes, No] Age_Category Respondent's age range Categorical ['18-24', '25-34', '35-44', '45-54', '55-64', '65-74', '75-80', '80+'] Height_(cm) Respondent's height in cm Numerical Measured in cm Weight_(kg) Respondent's weight in kg Numerical Measured in kg BMI Respondent's Body Mass Index in kg/cm\u00b2 Numerical Measured in kg/cm\u00b2 Smoking_History Respondent's smoking history Categorical [Yes, No] Alcohol_Consumption Number of days of alcohol consumption in a month Numerical Integer values Fruit_Consumption Number of servings of fruit consumed in a month Numerical Integer values Green_Vegetables_Consumption Number of servings of green vegetables consumed in a month Numerical Integer values FriedPotato_Consumption Number of servings of fried potato consumed in a month Numerical Integer values"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5

Exploratory Data Analysis

  • Summary statistics
  • Data visualization for numerical feature distributions
  • Target splits for categorical features

Data cleaning and Preprocessing

  • Regrouping rare categories
  • Categorical feature encoding
  • Outlier clipping for numerical features

Feature engineering and selection

  • Combining original features based on domain knowledge
  • Discretizing numerical features

Modeling

  • Holdout dataset created or model testing
  • Models trained: Logistic Regression, Decision Tree, Random Forest, AdaBoost, HistGradient Boosting, Multi-Layer Perceptron
  • Class imbalance handled through:
  • Class weights, when supported by model architecture
  • Threshold tuning using TunedThresholdClassifierCV
  • Metric for model-tuning: F2-score (harmonic weighted mean of precision and recall, with twice the weightage for recall)

Result analysis

  • Confusion matrix using predictions made on holdout test set
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1

Accuracy vs Recall: Data is extremely imbalanced, with only ~8% representing the positive class. This makes accuracy unsuitable as a metric for our problem. It is critical to correctly predict all the positive samples, due to which we must focus on recall. However, this lowers the overall accuracy since some negative samples may be predicted as positive.

  • Solution: Prediction threshold for models is tuned using F2-score to create a balance between precision and recall, with more importance given to recall. This maintains overall accuracy at an acceptable level while boosting recall.
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Error?};\n    B -->|Yes| C[Hmm...];\n    C --> D[Debug];\n    D --> B;\n    B ---->|No| E[Yay!];
Numerical feature distributions Height_(cm)Weight_(kg)BMIAlcoholFruitVegetableFried Patato

Correlations PearsonSpearman's RankKendall-Tau

"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#models-used-and-their-accuracies","title":"MODELS USED AND THEIR ACCURACIES","text":"Model + Feature set Accuracy (%) Recall (%) Logistic Regression + Original 76.29 74.21 Logistic Regression + Extended 76.27 74.41 Logistic Regression + Selected 72.66 78.09 Decision Tree + Original 72.76 78.61 Decision Tree + Extended 74.09 76.69 Decision Tree + Selected 75.52 73.61 Random Forest + Original 73.97 77.33 Random Forest + Extended 74.10 76.61 Random Forest + Selected 74.80 74.05 AdaBoost + Original 76.03 74.49 AdaBoost + Extended 74.99 76.25 AdaBoost + Selected 74.76 75.33 Multi-Layer Perceptron + Original 76.91 72.81 Multi-Layer Perceptron + Extended 73.26 79.01 Multi-Layer Perceptron + Selected 74.86 75.05 Hist-Gradient Boosting + Original 75.98 73.49 Hist-Gradient Boosting + Extended 75.63 74.73 Hist-Gradient Boosting + Selected 74.40 75.85"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#models-comparison-graphs","title":"MODELS COMPARISON GRAPHS","text":"

Logistic Regression

LR OriginalLR ExtendedLR Selected

Decision Tree DT OriginalDT ExtendedDT Selected

Random Forest RF OriginalRF ExtendedRF Selected

Ada Boost AB OriginalAB ExtendedAB Selected

Multi-Layer Perceptron MLP OriginalMLP ExtendedMLP Selected

Hist-Gradient Boosting HGB OriginalHGB ExtendedHGB Selected

"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  • General Health, Age and Co-morbities (such as Diabetes & Arthritis) are the most indicative features for CVD risk.
Improvements in understanding machine learning concepts
  • Learned and implemented the concept of predicting probability and tuning the prediction threshold for more accurate results, compared to directly predicting with the default thresold for models.
Challenges faced and how they were overcome
  • Deciding the correct metric for evaluation of models due to imbalanced nature of the dataset. Since positive class is more important, Recall was used as the final metric for ranking models.
  • F2-score was used to tune the threshold for models to maintain a balance between precision and recall, thereby maintaining overall accuracy.
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1Application 2
  • Doctors can use it as a second opinion when assessing a new patient. Model trained on cases from previous patients can be used to predict the risk.
  • People (patients in particular) can use this tool to track the risk of CVD based on their own lifestyle factors and take preventive measures when the risk is high.
"},{"location":"projects/machine-learning/cardiovascular-disease-prediction/#features-planned-but-not-implemented","title":"FEATURES PLANNED BUT NOT IMPLEMENTED","text":"Feature 1
  • Different implementations of gradient-boosting models such as XGBoost, CatBoost, LightGBM, etc. were not implemented since none of the tree ensemble models such as Random Forest, AdaBoost or Hist-Gradient Boosting were among the best performers. Hence, avoid additional dependencies based on such models.
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/","title":"Health Insurance Cross-Sell Prediction","text":""},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#aim","title":"AIM","text":"

To predict whether a Health Insurance customer would be interested in buying Vehicle Insurance.

"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/anmolkumar/health-insurance-cross-sell-prediction

"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#my-notebook-link","title":"MY NOTEBOOK LINK","text":"

https://www.kaggle.com/code/sid4ds/insurance-cross-sell-prediction-eda-modeling

"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn (>=1.5.0 for TunedThresholdClassifierCV)
  • xgboost
  • catboost
  • lightgbm
  • matplotlib
  • seaborn
  • joblib
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#description","title":"DESCRIPTION","text":"

Why is it necessary?

  • This project aims to predict the chances of cross-selling Vehicle insurance to existing Health insurance customers. This would be extremely helpful for companies because they can then accordingly plan communication strategy to reach out to those customers and optimise their business model and revenue.
How did you start approaching this project? (Initial thoughts and planning)
  • Going through previous research and articles related to the problem.
  • Data exploration to understand the features. Using data visualization to check their distributions.
  • Identifying key metrics for the problem based on ratio of target classes - ROC-AUC & Matthew's Correlation Coefficient (MCC) instead of Accuracy.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Feature Engineering: Tutorial notebook
  • Public notebook: Vehicle Insurance EDA and boosting models
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#explanation","title":"EXPLANATION","text":""},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"Feature Name Description Type Values/Range id Unique ID for the customer Numerical Unique numerical values Gender Binary gender of the customer Binary [0: Male, 1: Female] (or other binary representations as applicable) Age Numerical age of the customer Numerical Measured in years Driving_License Indicates if the customer has a Driving License Binary [0: No, 1: Yes] Region_Code Unique code for the customer's region Numerical Unique numerical values Previously_Insured Indicates if the customer already has Vehicle Insurance Binary [0: No, 1: Yes] Vehicle_Age Age of the vehicle categorized as ordinal values Categorical [< 1 year, 1-2 years, > 2 years] Vehicle_Damage Indicates if the vehicle was damaged in the past Binary [0: No, 1: Yes] Annual_Premium Amount to be paid as premium over the year Numerical Measured in currency Policy_Sales_Channel Anonymized code for the channel of customer outreach Numerical Unique numerical codes representing various channels Vintage Number of days the customer has been associated with the company Numerical Measured in days"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5

Exploratory Data Analysis

  • Summary statistics
  • Data visualization for numerical feature distributions
  • Target splits for categorical features

Data cleaning and Preprocessing

  • Removing duplicates
  • Categorical feature encoding

Feature engineering and selection

  • Discretizing numerical features
  • Feature selection based on model-based feature importances and statistical tests.

Modeling

  • Holdout dataset created or model testing
  • Setting up a framework for easier testing of multiple models.
  • Models trained: Logistic Regression, Linear Discriminant Analysis, Quadratic Discriminant Analysis, Gaussian Naive-Bayes, Decision Tree, Random Forest, AdaBoost, Multi-Layer Perceptron, XGBoost, CatBoost, LightGBM
  • Class imbalance handled through:
  • Class weights, when supported by model architecture
  • Threshold tuning using TunedThresholdClassifierCV
  • Metric for model-tuning: F1-score (harmonic weighted mean of precision and recall)

Result analysis

  • Predictions made on holdout test set
  • Models compared based on classification report and chosen metrics: ROC-AUC and MCC.
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1

Accuracy vs Recall & Precision

Data is heavily imbalanced, with only ~12% representing the positive class. This makes accuracy unsuitable as a metric for our problem. Our goal is to correctly predict all the positive samples, due to which we must focus on recall. However, this lowers the overall accuracy since some negative samples may be predicted as positive.

  • Solution: Prediction threshold for models is tuned using F1-score to create a balance between precision and recall. This maintains overall accuracy at an acceptable level while boosting recall.
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Error?};\n    B -->|Yes| C[Hmm...];\n    C --> D[Debug];\n    D --> B;\n    B ---->|No| E[Yay!];
Feature distributions (Univariate Analysis) AgeLicenseRegion CodePreviously InsuredVehical AgeVehical DamageAnnual PremiumPolicy ChannelVintage

Engineered Features Age GroupPolicy Group

Feature Distributions (Bivariate Analysis) Pair PlotsSpearman-Rank CorrelationPoint-Biserial CorrelationTetrachoric Correlation

Feature Selection Point-Biserial CorrelationANOVA F-TestTetrachoric CorrelationChi-Squared Test of IndependenceMutual InformationXGBoost Feature ImportancesExtra Trees Feature Importances

"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#models-used-and-their-performance","title":"MODELS USED AND THEIR PERFORMANCE","text":"
Best threshold after threshold tuning is also mentioned.\n
Model + Feature set ROC-AUC MCC Best threshold Logistic Regression + Original 0.8336 0.3671 0.65 Logistic Regression + Extended 0.8456 0.3821 0.66 Logistic Regression + Reduced 0.8455 0.3792 0.67 Logistic Regression + Minimal 0.8177 0.3507 0.60 Linear DA + Original 0.8326 0.3584 0.19 Linear DA + Extended 0.8423 0.3785 0.18 Linear DA + Reduced 0.8421 0.3768 0.18 Linear DA + Minimal 0.8185 0.3473 0.15 Quadratic DA + Original 0.8353 0.3779 0.45 Quadratic DA + Extended 0.8418 0.3793 0.54 Quadratic DA + Reduced 0.8422 0.3807 0.44 Quadratic DA + Minimal 0.8212 0.3587 0.28 Gaussian Naive Bayes + Original 0.8230 0.3879 0.78 Gaussian Naive Bayes + Extended 0.8242 0.3914 0.13 Gaussian Naive Bayes + Reduced 0.8240 0.3908 0.08 Gaussian Naive Bayes + Minimal 0.8055 0.3605 0.15 K-Nearest Neighbors + Original 0.7819 0.3461 0.09 K-Nearest Neighbors + Extended 0.7825 0.3469 0.09 K-Nearest Neighbors + Reduced 0.7710 0.3405 0.01 K-Nearest Neighbors + Minimal 0.7561 0.3201 0.01 Decision Tree + Original 0.8420 0.3925 0.67 Decision Tree + Extended 0.8420 0.3925 0.67 Decision Tree + Reduced 0.8419 0.3925 0.67 Decision Tree + Minimal 0.8262 0.3683 0.63 Random Forest + Original 0.8505 0.3824 0.70 Random Forest + Extended 0.8508 0.3832 0.70 Random Forest + Reduced 0.8508 0.3820 0.71 Random Forest + Minimal 0.8375 0.3721 0.66 Extra-Trees + Original 0.8459 0.3770 0.70 Extra-Trees + Extended 0.8504 0.3847 0.71 Extra-Trees + Reduced 0.8515 0.3836 0.72 Extra-Trees + Minimal 0.8337 0.3682 0.67 AdaBoost + Original 0.8394 0.3894 0.83 AdaBoost + Extended 0.8394 0.3894 0.83 AdaBoost + Reduced 0.8404 0.3839 0.84 AdaBoost + Minimal 0.8269 0.3643 0.86 Multi-Layer Perceptron + Original 0.8512 0.3899 0.22 Multi-Layer Perceptron + Extended 0.8528 0.3865 0.23 Multi-Layer Perceptron + Reduced 0.8517 0.3892 0.23 Multi-Layer Perceptron + Minimal 0.8365 0.3663 0.21 XGBoost + Original 0.8585 0.3980 0.68 XGBoost + Extended 0.8585 0.3980 0.68 XGBoost + Reduced 0.8584 0.3967 0.68 XGBoost + Minimal 0.8459 0.3765 0.66 CatBoost + Original 0.8579 0.3951 0.46 CatBoost + Extended 0.8578 0.3981 0.45 CatBoost + Reduced 0.8577 0.3975 0.45 CatBoost + Minimal 0.8449 0.3781 0.42 LightGBM + Original 0.8587 0.3978 0.67 LightGBM + Extended 0.8587 0.3976 0.67 LightGBM + Reduced 0.8587 0.3983 0.67 LightGBM + Minimal 0.8462 0.3753 0.66"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  1. Previously_Insured, Vehicle_Damage, Policy_Sales_Channel and Age are the most informative features for predicting cross-sell probability.
  2. Vintage and Driving_License have no predictive power. They are not included in the best model.
Improvements in understanding machine learning concepts
  1. Implemented threshold-tuning for more accurate results.
  2. Researched and utilized statistical tests for feature selection.
Challenges faced and how they were overcome
  1. Shortlisting the apropriate statistical test for bivariate analysis and feature selection.
  2. Deciding the correct metric for evaluation of models due to imbalanced nature of the dataset.
  3. F1-score was used for threshold-tuning. ROC-AUC score and MCC were used for model comparison.
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1
  • Companies can use customer data to predict which customers to target for cross-sell marketing. This saves cost and effort for the company, and protects uninterested customers from unnecessary marketing calls.
"},{"location":"projects/machine-learning/health-insurance-cross-sell-prediction/#features-planned-but-not-implemented","title":"FEATURES PLANNED BUT NOT IMPLEMENTED","text":"Feature 1
  • Complex model-ensembling through stacking or hill-climbing was not implemented due to significantly longer training time.
"},{"location":"projects/machine-learning/heart-disease-detection-model/","title":"Heart Disease Detection Model","text":""},{"location":"projects/machine-learning/heart-disease-detection-model/#aim","title":"AIM","text":"

The aim of this project is to develop a reliable and efficient machine learning-based system for the early detection and diagnosis of heart disease. By leveraging advanced algorithms, the system seeks to analyze patient data, identify significant patterns, and predict the likelihood of heart disease, thereby assisting healthcare professionals in making informed decisions.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#dataset-link","title":"DATASET LINK","text":"

This project uses a publicly available heart disease dataset from UCI Machine Learning Repository

"},{"location":"projects/machine-learning/heart-disease-detection-model/#notebook-link","title":"NOTEBOOK LINK","text":"

This is notebook of the following project Kaggle

"},{"location":"projects/machine-learning/heart-disease-detection-model/#libraries-needed","title":"LIBRARIES NEEDED","text":"
- pandas\n- numpy\n- scikit-learn\n- matplotlib\n- seaborn\n
"},{"location":"projects/machine-learning/heart-disease-detection-model/#description","title":"DESCRIPTION","text":"

what is the requirement of the project?, The project requires a dataset containing patient health records, including attributes like age, cholesterol levels, blood pressure, and medical history. Additionally, it needs machine learning tools and frameworks (e.g., Python, scikit-learn) for building and evaluating predictive models.

why is it necessary?, Early detection of heart disease is crucial to prevent severe complications and reduce mortality rates. A machine learning-based system provides accurate, fast, and cost-effective predictions, aiding timely medical intervention and improved patient outcomes.

how is it beneficial and used?, This system benefits healthcare by improving diagnostic accuracy and reducing reliance on invasive procedures. It can be used by doctors for decision support, by patients for risk assessment, and in hospitals for proactive healthcare management.

how did you start approaching this project?, The project begins by collecting and preprocessing a heart disease dataset, ensuring it is clean and ready for analysis. Next, machine learning models are selected, trained, and evaluated to identify the most accurate algorithm for predicting heart disease.

Any additional resources used like blogs reading, books reading (mention the name of book along with the pages you have read)? Kaggle kernels and documentation for additional dataset understanding. Tutorials on machine learning regression techniques, particularly for Random Forest, SVR, and Decision Trees.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#explanation","title":"EXPLANATION","text":""},{"location":"projects/machine-learning/heart-disease-detection-model/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"

Age: Patient's age in years.

Sex: Gender of the patient (1 = male; 0 = female).

Chest Pain Type (cp): Categorized as:

0: Typical angina 1: Atypical angina 2: Non-anginal pain 3: Asymptomatic Resting Blood Pressure (trestbps): Measured in mm Hg upon hospital admission.

Serum Cholesterol (chol): Measured in mg/dL.

Fasting Blood Sugar (fbs): Indicates if fasting blood sugar > 120 mg/dL (1 = true; 0 = false).

Resting Electrocardiographic Results (restecg):

0: Normal 1: Having ST-T wave abnormality (e.g., T wave inversions and/or ST elevation or depression > 0.05 mV) 2: Showing probable or definite left ventricular hypertrophy by Estes' criteria Maximum Heart Rate Achieved (thalach): Peak heart rate during exercise.

Exercise-Induced Angina (exang): Presence of angina induced by exercise (1 = yes; 0 = no).

Oldpeak: ST depression induced by exercise relative to rest.

Slope of the Peak Exercise ST Segment (slope):

0: Upsloping 1: Flat 2: Downsloping Number of Major Vessels Colored by Fluoroscopy (ca): Ranges from 0 to 3.

Thalassemia (thal):

1: Normal 2: Fixed defect 3: Reversible defect Target: Diagnosis of heart disease (0 = no disease; 1 = disease).

"},{"location":"projects/machine-learning/heart-disease-detection-model/#project-workflow","title":"PROJECT WORKFLOW","text":""},{"location":"projects/machine-learning/heart-disease-detection-model/#1problem-definition","title":"1.Problem Definition","text":"

Identify the objective: To predict the presence or absence of heart disease based on patient data. Define the outcome variable (target) and input features.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#2data-collection","title":"2.Data Collection","text":"

Gather a reliable dataset, such as the Cleveland Heart Disease dataset, which includes features relevant to heart disease prediction.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#3data-preprocessing","title":"3.Data Preprocessing","text":"

Handle missing values: Fill or remove records with missing data. Normalize/standardize data to ensure all features have comparable scales. Encode categorical variables like sex, cp, and thal using techniques like one-hot encoding or label encoding.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#4exploratory-data-analysis-eda","title":"4.Exploratory Data Analysis (EDA)","text":"

Visualize data distributions using histograms, boxplots, or density plots. Identify relationships between features using correlation matrices and scatterplots. Detect and handle outliers to improve model performance.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#5feature-selection","title":"5.Feature Selection","text":"

Use statistical methods or feature importance metrics to identify the most relevant features for prediction. Remove redundant or less significant features.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#6data-splitting","title":"6.Data Splitting","text":"

Divide the dataset into training, validation, and testing sets (e.g., 70%-15%-15%). Ensure a balanced distribution of the target variable in all splits.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#7model-selection","title":"7.Model Selection","text":"

Experiment with multiple machine learning algorithms such as Logistic Regression, Random Forest, Decision Trees, Support Vector Machines (SVM), and Neural Networks. Select models based on the complexity and nature of the dataset.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#8model-training","title":"8.Model Training","text":"

Train the chosen models using the training dataset. Tune hyperparameters using grid search or random search techniques.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#9model-evaluation","title":"9.Model Evaluation","text":"

Assess models on validation and testing datasets using metrics such as: Accuracy Precision, Recall, and F1-score Receiver Operating Characteristic (ROC) curve and Area Under the Curve (AUC). Compare models to identify the best-performing one.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#10deployment-and-prediction","title":"10.##Deployment and Prediction","text":"

Save the trained model using frameworks like joblib or pickle. Develop a user interface (UI) or API for end-users to input data and receive predictions.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#11iterative-improvement","title":"11.Iterative Improvement","text":"

Continuously refine the model using new data or advanced algorithms. Address feedback and optimize the system based on real-world performance.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Accuracy vs. Interpretability
  • Complex models like Random Forests or Neural Networks offer higher accuracy but are less interpretable compared to simpler models like Logistic Regression.
  • Overfitting vs. Generalization
  • Models with high complexity may overfit the training data, leading to poor generalization on unseen data.
"},{"location":"projects/machine-learning/heart-disease-detection-model/#models-used-and-their-evaluation-metrics","title":"MODELS USED AND THEIR EVALUATION METRICS","text":"Model Score Logistic regression 88% K-Nearest Classifier 68% Random Forest Classifier 86%"},{"location":"projects/machine-learning/heart-disease-detection-model/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/machine-learning/heart-disease-detection-model/#key-learnings","title":"KEY LEARNINGS","text":"
  1. Data Insights Understanding Healthcare Data: Learned how medical attributes (e.g., age, cholesterol, chest pain type) influence heart disease risk. Data Imbalance: Recognized the challenges posed by imbalanced datasets and explored techniques like SMOTE and class weighting to address them. Importance of Preprocessing: Gained expertise in handling missing values, scaling data, and encoding categorical variables, which are crucial for model performance.

  2. Techniques Mastered Exploratory Data Analysis (EDA): Applied visualization tools (e.g., histograms, boxplots, heatmaps) to uncover patterns and correlations in data. Feature Engineering: Identified and prioritized key features using statistical methods and feature importance metrics. Modeling: Implemented various machine learning algorithms, including Logistic Regression, Random Forest, Gradient Boosting, and Support Vector Machines. Evaluation Metrics: Learned to evaluate models using metrics like Precision, Recall, F1-score, and ROC-AUC to optimize for healthcare-specific goals. Hyperparameter Tuning: Used grid search and random search to optimize model parameters and improve performance. Interpretability Tools: Utilized SHAP and feature importance analysis to explain model predictions.

  3. Skills Developed Problem-Solving: Addressed trade-offs such as accuracy vs. interpretability, and overfitting vs. generalization. Critical Thinking: Improved decision-making on model selection, preprocessing methods, and evaluation strategies. Programming: Strengthened Python programming skills, including the use of libraries like scikit-learn, pandas, matplotlib, and TensorFlow. Collaboration: Enhanced communication and teamwork when discussing medical insights and technical challenges with domain experts. Time Management: Balanced experimentation with computational efficiency, focusing on techniques that maximized impact. Ethical Considerations: Gained awareness of ethical issues like ensuring fairness in predictions and minimizing false negatives, which are critical in healthcare applications.

  4. Broader Understanding Interdisciplinary Knowledge: Combined expertise from data science, healthcare, and statistics to create a meaningful application. Real-World Challenges: Understood the complexities of translating machine learning models into practical tools for healthcare. Continuous Learning: Learned that model development is iterative, requiring continuous refinement based on feedback and new data.

"},{"location":"projects/machine-learning/heart-disease-detection-model/#use-cases","title":"USE CASES","text":"Application 1Application 2

Clinical Decision Support Systems (CDSS)

  • ML models can be integrated into Electronic Health Record (EHR) systems to assist doctors in diagnosing heart disease. The model can provide predictions based on patient data, helping clinicians make faster and more accurate decisions.

Early Screening and Risk Assessment

  • Patients can undergo routine screening using a heart disease detection system to assess their risk level. The system can predict whether a patient is at high, moderate, or low risk, prompting early interventions or lifestyle changes.
"},{"location":"projects/machine-learning/poker-hand-prediction/","title":"Poker Hand Prediction","text":""},{"location":"projects/machine-learning/poker-hand-prediction/#aim","title":"AIM","text":"

The aim of this project is to develop a machine learning model using a Multi-Layer Perceptron (MLP) classifier to accurately classify different types of poker hands based on the suit and rank of five cards.

"},{"location":"projects/machine-learning/poker-hand-prediction/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/dysphoria/poker-hand-classification

"},{"location":"projects/machine-learning/poker-hand-prediction/#notebook-link","title":"NOTEBOOK LINK","text":"

https://www.kaggle.com/code/supratikbhowal/poker-hand-prediction-model

"},{"location":"projects/machine-learning/poker-hand-prediction/#libraries-needed","title":"LIBRARIES NEEDED","text":"
  • pandas
  • numpy
  • matplotlib
  • seaborn
  • scikit-learn
"},{"location":"projects/machine-learning/poker-hand-prediction/#description","title":"DESCRIPTION","text":"

This project involves building a classification model to predict poker hands using a Multi-Layer Perceptron (MLP) classifier. The dataset consists of features representing the suit and rank of five cards, with the target variable being the type of poker hand (e.g., one pair, two pair, royal flush). The model is trained on a standardized dataset, with class weights computed to address class imbalance. Performance is evaluated using metrics such as accuracy, classification report, confusion matrix, prediction error, ROC curve, and AUC, providing a comprehensive analysis of the model's effectiveness in classifying poker hands.

What is the requirement of the project? - To accurately predict the Poker Hand type.

Why is it necessary? - The project demonstrates how machine learning can solve structured data problems, bridging the gap between theoretical knowledge and practical implementation.

How is it beneficial and used? - The project automates the classification of poker hands, enabling players to quickly and accurately identify the type of hand they have, such as a straight, flush, or royal flush, without manual effort. - By understanding the probabilities and patterns of certain hands appearing, players can make informed decisions on whether to bet, raise, or fold, improving their gameplay strategy.

How did you start approaching this project? - Analyzed the poker hand classification problem, reviewed the dataset structure (suits, ranks, and hand types), and identified the multi-class nature of the target variable. Studied the class imbalance issue and planned data preprocessing steps, including scaling and class weight computation. - Chose the Multi-Layer Perceptron (MLP) Classifier for its capability to handle complex patterns in data. Defined model hyperparameters, trained the model using standardized features, and evaluated its performance using metrics like accuracy, ROC-AUC, and confusion matrix

Mention any additional resources used: - Kaggle kernels and documentation for additional dataset understanding. - Tutorials on machine learning regression techniques, particularly for MLP

"},{"location":"projects/machine-learning/poker-hand-prediction/#explanation","title":"EXPLANATION","text":""},{"location":"projects/machine-learning/poker-hand-prediction/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"Feature Name Description Type Values/Range S1 Suit of card #1 Ordinal (1-4) representing C1 Rank of card #1 Numerical (1-13) representing (Ace, 2, 3, \u2026 , Queen, King) S2 Suit of card #2 Ordinal (1-4) representing C2 Rank of card #2 Numerical (1-13) representing (Ace, 2, 3, \u2026 , Queen, King) S3 Suit of card #3 Ordinal (1-4) representing C3 Rank of card #3 Numerical (1-13) representing (Ace, 2, 3, \u2026 , Queen, King) S4 Suit of card #4 Ordinal (1-4) representing C4 Rank of card #4 Numerical (1-13) representing (Ace, 2, 3, \u2026 , Queen, King) S5 Suit of card #5 Ordinal (1-4) representing C5 Rank of card #5 Numerical (1-13) representing (Ace, 2, 3, \u2026 , Queen, King) Poker Hand Type of Card in Hand Ordinal (0-9) types*

Poker Hands 0: Nothing in hand, not a recognized poker hand 1: One pair, one pair of equal ranks within five cards 2: Two pairs, two pairs of equal ranks within five cards 3: Three of a kind, three equal ranks within five cards 4: Straight, five cards, sequentially ranked with no gaps 5: Flush, five cards with the same suit 6: Full house, pair + different rank three of a kind 7: Four of a kind, four equal ranks within five cards 8: Straight flush, straight + flush 9: Royal flush, {Ace, King, Queen, Jack, Ten} + flush

"},{"location":"projects/machine-learning/poker-hand-prediction/#project-workflow","title":"PROJECT WORKFLOW","text":""},{"location":"projects/machine-learning/poker-hand-prediction/#1-dataset-loading-and-inspection","title":"1. Dataset Loading and Inspection","text":"

The dataset is loaded into the environment, and its structure, features, and target classes are analyzed. Column names are assigned to enhance clarity and understanding.

"},{"location":"projects/machine-learning/poker-hand-prediction/#2-data-preprocessing","title":"2. Data Preprocessing","text":"

Target variables are mapped to descriptive hand labels, features and target variables are separated, and data is standardized using 'StandardScaler' for uniform scaling.

"},{"location":"projects/machine-learning/poker-hand-prediction/#3-handling-class-imbalance","title":"3. Handling Class Imbalance","text":"

Class weights are computed using 'compute_class_weight' to address the imbalance in target classes, ensuring fair training for all hand types.

"},{"location":"projects/machine-learning/poker-hand-prediction/#4-model-selection-and-training","title":"4. Model Selection and Training","text":"

The MLPClassifier is selected for its capability to handle multi-class problems, configured with suitable hyperparameters, and trained on the preprocessed training data.

"},{"location":"projects/machine-learning/poker-hand-prediction/#5-model-evaluation","title":"5. Model Evaluation","text":"

The model's performance is assessed using metrics such as accuracy, classification reports, and confusion matrices. Predictions are generated for the test dataset.

"},{"location":"projects/machine-learning/poker-hand-prediction/#6-visualization-and-error-analysis","title":"6. Visualization and Error Analysis","text":"

Class prediction errors are visualized using bar charts, and ROC curves with AUC values are generated for each hand type to evaluate classification effectiveness.

"},{"location":"projects/machine-learning/poker-hand-prediction/#7-insights-and-interpretation","title":"7. Insights and Interpretation","text":"

Strengths and weaknesses of the model are identified through error analysis, and the findings are presented using visual tools like heatmaps for better understanding.

"},{"location":"projects/machine-learning/poker-hand-prediction/#8-conclusion","title":"8. Conclusion","text":"

The project outcomes are summarized, practical applications are discussed, and suggestions for further research or improvements are proposed.

"},{"location":"projects/machine-learning/poker-hand-prediction/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1
  • Trade-off: Accuracy vs. Complexity.
    • Solution: Using a Multi-Layer Perceptron (MLP) introduces complexity due to its hidden layers and numerous parameters. While this improves accuracy, it requires more computational resources and training time compared to simpler models like decision trees or logistic regression.
Trade Off 2
  • Trade-off: Bias vs. Variance
    • Solution: The project's hyperparameter tuning (e.g., learning rate, number of hidden layers) aims to reduce bias and variance. However, achieving a perfect balance is a trade-off, as overly complex models may increase variance (overfitting), while overly simplified models may increase bias (underfitting).
Trade Off 3
  • Trade-off: Generalization vs. Overfitting
    • Solution:The model's flexibility with complex hyperparameters (e.g., hidden layers, activation functions) risks overfitting, especially on small or imbalanced datasets. Regularization techniques like adjusting 'alpha' help mitigate this but may compromise some accuracy.
"},{"location":"projects/machine-learning/poker-hand-prediction/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Is data clean?};\n    B -->|Yes| C[Explore Data];\n    C --> D[Data Preprocessing];\n    D --> E[Define Models];\n    E --> F[Train the Model];\n    F --> G[Evaluate Performance];\n    G --> H[Visualize Evaluation Metrics];\n    H --> I[Model Testing];\n    I --> J[Conclusion and Observations];\n    B ---->|No| K[Clean Data];\n    K --> C;
Model Evaluation Metrics Model vs AccuracyROC Curve & AUC ScoreClassification Report Heatmap

"},{"location":"projects/machine-learning/poker-hand-prediction/#models-evaluation-metrics","title":"MODELS EVALUATION METRICS","text":"Poker Hand precision recall f1-score support flush 0.52 0.07 0.12 1996 four_of_a_kind 0.00 0.00 0.00 230 full_house 0.77 0.34 0.47 1424 one_pair 1.00 1.00 1.00 422498 royal_flush 0.00 0.00 0.00 3 straight 0.96 0.44 0.60 3885 straight_flush 0.00 0.00 0.00 12 three_of_a_kind 0.95 0.00 0.00 21121 two_pair 1.00 1.00 1.00 47622 zilch 0.99 1.00 1.00 501209 Model Accuracy Random Forest Regression 0.6190 Gradient Boosting Regression 0.3204 MLP Classifier 0.9924 StackingClassifier 0.9382"},{"location":"projects/machine-learning/poker-hand-prediction/#-","title":"---","text":""},{"location":"projects/machine-learning/poker-hand-prediction/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/machine-learning/poker-hand-prediction/#key-learnings","title":"KEY LEARNINGS","text":"
  • Learned how different machine learning models perform on real-world data and gained insights into their strengths and weaknesses.
  • Realized how important feature engineering and data preprocessing are for enhancing model performance.

Insights gained from the data:

  • Class imbalance can bias the model, requiring class weighting for fair performance.
  • Suits and ranks of cards are crucial features, necessitating preprocessing for better model input.

Improvements in understanding machine learning concepts:

  • Learned how to effectively implement and optimize machine learning models using libraries like scikit-learn.
  • Learned how to effectively use visualizations (e.g., ROC curves, confusion matrices, and heatmaps) to interpret and communicate model performance.

Challenges faced and how they were overcome:

  • Some features had different scales, which could affect model performance. This was overcome by applying standardization to the features, ensuring they were on a similar scale for better convergence during training.
  • Some rare hands, such as \"royal_flush\" and \"straight_flush,\" had very low prediction accuracy. This was mitigated by analyzing misclassification patterns and considering potential improvements like generating synthetic samples or using other models.
"},{"location":"projects/machine-learning/poker-hand-prediction/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1

Poker Strategy Development and Simulation -> Developers or researchers studying poker strategies can use this model to simulate various hand combinations and evaluate strategic decisions. The model's classification can help assess the strength of different hands and optimize strategies.

Application 2

Real-time Poker Hand Evaluation for Mobile Apps -> Mobile apps that allow users to practice or play poker could incorporate this model to provide real-time hand evaluation, helping users understand the strength of their hands during gameplay.

"},{"location":"projects/machine-learning/sleep-quality-prediction/","title":"Sleep quality prediction","text":"Sleep Quality Prediction AIM

To predict sleep quality based on lifestyle and health factors.

DATASET LINK

Sleep Health and Lifestyle Dataset

DESCRIPTION What is the requirement of the project?
  • This project aims to predict the quality of sleep using various health and lifestyle metrics. Predicting sleep quality helps individuals and healthcare professionals address potential sleep-related health issues early.
Why is it necessary?
  • Sleep quality significantly impacts physical and mental health. Early predictions can prevent chronic conditions linked to poor sleep, such as obesity, heart disease, and cognitive impairment.
How is it beneficial and used?
  • Individuals: Assess their sleep health and make lifestyle changes to improve sleep quality.
  • Healthcare Professionals: Use the model as an auxiliary diagnostic tool to recommend personalized interventions.
How did you start approaching this project? (Initial thoughts and planning)
  • Researching sleep health factors and existing literature.
  • Exploring and analyzing the dataset to understand feature distributions.
  • Preprocessing data for effective feature representation.
  • Iterating over machine learning models to find the optimal balance between accuracy and interpretability.
Mention any additional resources used
  • Research Paper: Analyzing Sleep Patterns Using AI
  • Public Notebook: Sleep Quality Prediction with 96% Accuracy
LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn
  • matplotlib
  • seaborn
  • joblib
  • flask
EXPLANATION DETAILS OF THE DIFFERENT FEATURES Feature Name Description Type Values/Range Gender Respondent's gender Categorical [Male, Female] Age Respondent's age Numerical Measured in years Sleep Duration (hours) Hours of sleep per day Numerical Measured in hours Physical Activity Level Daily physical activity in minutes Numerical Measured in minutes Stress Level Stress level on a scale Numerical 1 to 5 (low to high) BMI Category Body Mass Index category Categorical [Underweight, Normal, Overweight, Obese] Systolic Blood Pressure Systolic blood pressure Numerical Measured in mmHg Diastolic Blood Pressure Diastolic blood pressure Numerical Measured in mmHg Heart Rate (bpm) Resting heart rate Numerical Beats per minute Daily Steps Average number of steps per day Numerical Measured in steps Sleep Disorder Reported sleep disorder Categorical [Yes, No] WHAT I HAVE DONE Step 1: Exploratory Data Analysis
  • Summary statistics
  • Data visualization for numerical feature distributions
  • Target splits for categorical features
Step 2: Data Cleaning and Preprocessing
  • Handling missing values
  • Label encoding categorical features
  • Standardizing numerical features
Step 3: Feature Engineering and Selection
  • Merging features based on domain knowledge
  • Creating derived features such as \"Activity-to-Sleep Ratio\"
Step 4: Modeling
  • Model trained: Decision Tree
  • Class imbalance handled using SMOTE
  • Metric for optimization: F1-score
Step 5: Result Analysis
  • Visualized results using confusion matrices and classification reports
  • Interpreted feature importance for tree-based models
MODELS USED AND THEIR ACCURACIES Model Accuracy (%) F1-Score (%) Precision (%) Recall (%) Decision Tree 74.50 75.20 73.00 77.50 CONCLUSION WHAT YOU HAVE LEARNED Insights gained from the data
  • Sleep Duration, Stress Level, and Physical Activity are the most indicative features for predicting sleep quality.
Improvements in understanding machine learning concepts
  • Learned and implemented preprocessing techniques like encoding categorical variables and handling imbalanced datasets.
  • Gained insights into deploying a machine learning model using Flask for real-world use cases.
Challenges faced and how they were overcome
  • Managing imbalanced classes: Overcame this by using SMOTE for oversampling the minority class.
  • Choosing a simple yet effective model: Selected Decision Tree for its interpretability and ease of deployment.
USE CASES OF THIS MODEL Application 1

A health tracker app can integrate this model to assess and suggest improvements in sleep quality based on user inputs.

Application 2

Healthcare providers can use this tool to make preliminary assessments of patients' sleep health, enabling timely interventions.

FEATURES PLANNED BUT NOT IMPLEMENTED Feature 1

Advanced models such as Random Forest, AdaBoost, and Gradient Boosting were not implemented due to the project's focus on simplicity and interpretability.

Feature 2

Integration with wearable device data for real-time predictions was not explored but remains a potential enhancement for future work.

"},{"location":"projects/machine-learning/used-cars-price-prediction/","title":"Used Cars Price Prediction","text":""},{"location":"projects/machine-learning/used-cars-price-prediction/#aim","title":"AIM","text":"

Predicting the prices of used cars based on their configuration and previous usage.

"},{"location":"projects/machine-learning/used-cars-price-prediction/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/avikasliwal/used-cars-price-prediction

"},{"location":"projects/machine-learning/used-cars-price-prediction/#my-notebook-link","title":"MY NOTEBOOK LINK","text":"

https://www.kaggle.com/code/sid4ds/used-cars-price-prediction/

"},{"location":"projects/machine-learning/used-cars-price-prediction/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn (>=1.5.0 required for Target Encoding)
  • xgboost
  • catboost
  • matplotlib
  • seaborn
"},{"location":"projects/machine-learning/used-cars-price-prediction/#description","title":"DESCRIPTION","text":"

Why is it necessary?

  • This project aims to predict the prices of used cars listed on an online marketplace based on their features and usage by previous owners. This model can be used by sellers to estimate an approximate price for their cars when they list them on the marketplace. Buyers can use the model to check if the listed price is fair when they decide to buy a used vehicle.
How did you start approaching this project? (Initial thoughts and planning)
  • Researching previous projects and articles related to the problem.
  • Data exploration to understand the features.
  • Identifying different preprocessing strategies for different feature types.
  • Choosing key metrics for the problem - Root Mean Squared Error (for error estimation), R2-Score (for model explainability)
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Dealing with features that have high cardinality
  • Target-encoding Categorical Variables
  • Cars Price Prediction
"},{"location":"projects/machine-learning/used-cars-price-prediction/#explanation","title":"EXPLANATION","text":""},{"location":"projects/machine-learning/used-cars-price-prediction/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"Feature Name Description Type Values/Range Name Car model Categorical Names of car models Location City where the car is listed for sale Categorical Names of cities Year Year of original purchase of car Numerical Years (e.g., 2010, 2015, etc.) Kilometers_Driven Odometer reading of the car Numerical Measured in kilometers Fuel_Type Fuel type of the car Categorical [Petrol, Diesel, CNG, Electric, etc.] Transmission Transmission type of the car Categorical [Automatic, Manual] Owner_Type Number of previous owners of the car Numerical Whole numbers Mileage Current mileage provided by the car Numerical Measured in km/l or equivalent Engine Engine capacity of the car Numerical Measured in CC (Cubic Centimeters) Power Engine power output of the car Numerical Measured in BHP (Brake Horsepower) Seats Seating capacity of the car Numerical Whole numbers New_Price Original price of the car at the time of purchase Numerical Measured in currency"},{"location":"projects/machine-learning/used-cars-price-prediction/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5

Exploratory Data Analysis

  • Summary statistics
  • Data visualization for numerical feature distributions
  • Target splits for categorical features

Data cleaning and Preprocessing

  • Removing rare categories of brands
  • Removing outliers for numerical features and target
  • Categorical feature encoding for low-cardinality features
  • Target encoding for high-cardinality categorical features (in model pipeline)

Feature engineering and selection

  • Extracting brand name from model name for a lower-cardinality feature.
  • Converting categorical Owner_Type to numerical Num_Previous_Owners.
  • Feature selection based on model-based feature importances and statistical tests.

Modeling

  • Holdout dataset created for model testing
  • Setting up a framework for easier testing of multiple models.
  • Models trained: LLinear Regression, K-Nearest Neighbors, Decision Tree, Random Forest, AdaBoost, Multi-Layer Perceptron, XGBoost and CatBoost.
  • Models were ensembled using Simple and Weighted averaging.

Result analysis

  • Predictions made on holdout test set
  • Models compared based on chosen metrics: RMSE and R2-Score.
  • Visualized predicted prices vs actual prices to analyze errors.
"},{"location":"projects/machine-learning/used-cars-price-prediction/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1

Training time & Model complexity vs Reducing error

  • Solution: Limiting depth and number of estimators for tree-based models. Overfitting detection and early stopping mechanism for neural network training.
"},{"location":"projects/machine-learning/used-cars-price-prediction/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Error?};\n    B -->|Yes| C[Hmm...];\n    C --> D[Debug];\n    D --> B;\n    B ---->|No| E[Yay!];
Data Exploration PriceYearKM DrivenEnginePowerMileageSeats

Feature Selection Feature CorrelationTarget CorrelationMutual Information

"},{"location":"projects/machine-learning/used-cars-price-prediction/#models-used-and-their-performance","title":"MODELS USED AND THEIR PERFORMANCE","text":"Model RMSE R2-Score Linear Regression 3.5803 0.7915 K-Nearest Neighbors 2.8261 0.8701 Decision Tree 2.6790 0.8833 Random Forest 2.4619 0.9014 AdaBoost 2.3629 0.9092 Multi-layer Perceptron 2.6255 0.8879 XGBoost w/o preprocessing 2.1649 0.9238 XGBoost with preprocessing 2.0987 0.9284 CatBoost w/o preprocessing 2.1734 0.9232 Simple average ensemble 2.2804 0.9154 Weighted average ensemble 2.1296 0.9262"},{"location":"projects/machine-learning/used-cars-price-prediction/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/machine-learning/used-cars-price-prediction/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  1. Features related to car configuration such as Power, Engine and Transmission are some of the most informative features. Usage-related features such as Year and current Mileage are also important.
  2. Seating capacity and Number of previous owners had relatively less predictive power. However, none of the features were candidates for removal.
Improvements in understanding machine learning concepts
  1. Implemented target-encoding for high-cardinality categorical features.
  2. Designed pipelines to avoid data leakage.
  3. Ensembling models using prediction averaging.
Challenges faced and how they were overcome
  1. Handling mixed feature types in preprocessing pipelines.
  2. Regularization and overfitting detection to reduce training time while maintaining performance.
"},{"location":"projects/machine-learning/used-cars-price-prediction/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1Application 2
  • Sellers can use the model to estimate an approximate price for their cars when they list them on the marketplace.
  • Buyers can use the model to check if the listed price is fair when they decide to buy a used vehicle.
"},{"location":"projects/machine-learning/used-cars-price-prediction/#features-planned-but-not-implemented","title":"FEATURES PLANNED BUT NOT IMPLEMENTED","text":"Feature 1
  • Complex model-ensembling through stacking or hill-climbing was not implemented due to significantly longer training time.
"},{"location":"projects/natural-language-processing/","title":"Natural Language Processing \ud83d\udde3\ufe0f","text":"Chatbot Implementation Project

Developing a modern chatbot system using NLP techniques and AI algorithms.

\ud83d\udcc5 2025-01-21 | \u23f1\ufe0f 15 mins

"},{"location":"projects/natural-language-processing/chatbot-project-implementation/","title":"Chatbot Implementation Project","text":""},{"location":"projects/natural-language-processing/chatbot-project-implementation/#aim","title":"AIM","text":"

To develop a chatbot using Natural Language Processing (NLP) and a Naive Bayes classifier for intent classification. The chatbot takes user input, predicts the intent, and generates an appropriate response based on predefined intents and responses stored in a CSV file.

"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#dataset-link","title":"DATASET LINK","text":"

https://drive.google.com/file/d/1J7mGS16EkgCEtN7UJtBlJACeqoDbdS4F/view?usp=drive_link

"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#notebook-link","title":"NOTEBOOK LINK","text":"

https://colab.research.google.com/drive/1L2LKfbVv4pb4yzczcRnnU4AkEW-kCZSZ?usp=sharing

"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • nltk
  • scikit-learn
  • numpy
  • pickle
"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#description","title":"DESCRIPTION","text":"What is the Requirement of the Project?

-A chatbot is required to automate conversations and provide immediate responses to user queries. It can be used to answer FAQs, provide customer support, and improve user interaction.

Why is it Necessary?
  • Chatbots are essential for improving user engagement and providing 24/7 service.
  • They automate responses, saving time and providing immediate help.
How is it Beneficial and Used?
  • Chatbots can be used for:
  • Customer service automation.
  • Answering user questions.
  • Guiding users through processes on websites or apps.
"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#initial-thoughts-and-planning","title":"\"Initial Thoughts and Planning\"","text":"
  • Intents and Responses: Gathered and stored in CSV format.
  • Preprocessing: Tokenized and lemmatized text to prepare for model training.
  • Model: Built a Naive Bayes classifier to predict intents.
  • Deployment: Deployed the model to predict user queries and return appropriate responses.
"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#additional-resources-used","title":"Additional Resources Used","text":"
  • Scikit-learn Documentation
  • Tutorial: Building Chatbots with NLP and Machine Learning
"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#features-in-the-dataset","title":"FEATURES IN THE DATASET","text":"Feature Description intents User query categories like greetings, farewells. responses Predefined chatbot responses for each intent."},{"location":"projects/natural-language-processing/chatbot-project-implementation/#steps-and-implementation","title":"STEPS AND IMPLEMENTATION","text":"step 1step 2step 3step 4step 5

Data Preprocessing - Loaded the intents from CSV files. - Cleaned data by removing duplicates and handling null values.

Vectorization - Used TfidfVectorizer to convert text into vectors. - Split data into training and testing sets.

Model Training - Trained a Naive Bayes classifier on the preprocessed data. - Saved the model for future use with pickle. - Created an intent-response mapping.

Prediction and Response Generation** - The chatbot predicts the intent based on user input. - Fetches and returns the appropriate response.

Testing - Conducted live interaction tests with the chatbot.

"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#features-not-implemented-yet","title":"Features Not Implemented Yet","text":"
  • Integration of a deep learning model (e.g., RNN or LSTM) for better context handling.
"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#flow-chart","title":"Flow Chart","text":"

graph TD\n    A[Data Preprocessing] --> B[Vectorization]\n    B --> C[Model Training]\n    C --> D[Prediction and Response Generation]\n    D --> E[Testing the Chatbot]\n\n    A1[Load intents from CSV] --> A2[Clean data: remove duplicates and handle nulls]\n    A --> A1\n    A --> A2\n\n    B1[Use TfidfVectorizer to convert text into vectors] --> B2[Split data into training and testing sets]\n    B --> B1\n    B --> B2\n\n    C1[Train Naive Bayes classifier] --> C2[Save model with pickle] --> C3[Create intent-response mapping]\n    C --> C1\n    C --> C2\n    C --> C3\n\n    D1[Chatbot predicts intent] --> D2[Fetch appropriate response based on intent] --> D3[Return response to user]\n    D --> D1\n    D --> D2\n    D --> D3\n\n    E1[Live interaction with chatbot] --> E2[Test accuracy and responses]\n    E --> E1\n    E --> E2
#### Example Chatbot Interaction:\n\n```text\nYou: Hello\nBot: Hi, How can I assist you?\n

"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#models-and-evaluation-metrics","title":"MODELS AND EVALUATION METRICS","text":"Model Accuracy Precision Recall Naive Bayes 92% 91% 90%"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#conclusion","title":"CONCLUSION","text":"What Have You Learned?
  • Building a chatbot using NLP techniques can automate interactions and provide user-friendly interfaces for businesses. -The Naive Bayes classifier is an effective yet simple model for intent prediction.
"},{"location":"projects/natural-language-processing/chatbot-project-implementation/#use-cases","title":"USE CASES","text":"Application 1Application 2

Customer Support Automation

-Provide 24/7 automated support for customers.

FAQ Automation

  • Automatically respond to frequently asked questions on websites or apps.
"},{"location":"projects/natural-language-processing/email_spam_detection/","title":"Email Spam Detection","text":""},{"location":"projects/natural-language-processing/email_spam_detection/#aim","title":"AIM","text":"

To develop a machine learning-based system that classifies email content as spam or ham (not spam).

"},{"location":"projects/natural-language-processing/email_spam_detection/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/ashfakyeafi/spam-email-classification

"},{"location":"projects/natural-language-processing/email_spam_detection/#notebook-link","title":"NOTEBOOK LINK","text":"

https://www.kaggle.com/code/inshak9/email-spam-detection

"},{"location":"projects/natural-language-processing/email_spam_detection/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn
  • matplotlib
  • seaborn
"},{"location":"projects/natural-language-processing/email_spam_detection/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • A robust system to detect spam emails is essential to combat increasing spam content.
  • It improves user experience by automatically filtering unwanted messages.
Why is it necessary?
  • Spam emails consume resources, time, and may pose security risks like phishing.
  • Helps organizations and individuals streamline their email communication.
How is it beneficial and used?
  • Provides a quick and automated solution for spam classification.
  • Used in email services, IT systems, and anti-spam software to filter messages.
How did you start approaching this project? (Initial thoughts and planning)
  • Analyzed the dataset and prepared features.
  • Implemented various machine learning models for comparison.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Documentation from scikit-learn
  • Blog: Introduction to Spam Classification with ML
"},{"location":"projects/natural-language-processing/email_spam_detection/#explanation","title":"EXPLANATION","text":""},{"location":"projects/natural-language-processing/email_spam_detection/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"

The dataset contains features like word frequency, capital letter counts, and others that help in distinguishing spam emails from ham.

Feature Description word_freq_x Frequency of specific words in the email body capital_run_length Length of consecutive capital letters char_freq Frequency of special characters like ; and $ is_spam Target variable (1 = Spam, 0 = Ham)"},{"location":"projects/natural-language-processing/email_spam_detection/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5Step 6

Initial data exploration and understanding: - Loaded the dataset using pandas. - Explored dataset features and target variable distribution.

Data cleaning and preprocessing: - Checked for missing values. - Standardized features using scaling techniques.

Feature engineering and selection: - Extracted relevant features for spam classification. - Used correlation matrix to select significant features.

Model training and evaluation: - Trained models: KNN, Naive Bayes, SVM, and Random Forest. - Evaluated models using accuracy, precision, and recall.

Model optimization and fine-tuning: - Tuned hyperparameters using GridSearchCV.

Validation and testing: - Tested models on unseen data to check performance.

"},{"location":"projects/natural-language-processing/email_spam_detection/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2
  • Accuracy vs. Training Time:
  • Models like Random Forest took longer to train but achieved higher accuracy compared to Naive Bayes.
  • Complexity vs. Interpretability:
  • Simpler models like Naive Bayes were more interpretable but slightly less accurate.
"},{"location":"projects/natural-language-processing/email_spam_detection/#screenshots","title":"SCREENSHOTS","text":"

Project flowchart

  graph LR\n    A[Start] --> B[Load Dataset];\n    B --> C[Preprocessing];\n    C --> D[Train Models];\n    D --> E{Compare Performance};\n    E -->|Best Model| F[Deploy];\n    E -->|Retry| C;
Confusion Matrix SVMNaive BayesDecision TreeAdaBoostRandom Forest

"},{"location":"projects/natural-language-processing/email_spam_detection/#models-used-and-their-evaluation-metrics","title":"MODELS USED AND THEIR EVALUATION METRICS","text":"Model Accuracy Precision Recall KNN 90% 89% 88% Naive Bayes 92% 91% 90% SVM 94% 93% 91% Random Forest 95% 94% 93% AdaBoost 97% 97% 100%"},{"location":"projects/natural-language-processing/email_spam_detection/#models-comparison-graphs","title":"MODELS COMPARISON GRAPHS","text":"

Models Comparison Graphs

Accuracy Comparison

"},{"location":"projects/natural-language-processing/email_spam_detection/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/natural-language-processing/email_spam_detection/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  • Feature importance significantly impacts spam detection.
  • Simple models like Naive Bayes can achieve competitive performance.
Improvements in understanding machine learning concepts
  • Gained hands-on experience with classification models and model evaluation techniques.
Challenges faced and how they were overcome
  • Balancing between accuracy and training time was challenging, solved using model tuning.
"},{"location":"projects/natural-language-processing/email_spam_detection/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1Application 2

Email Service Providers - Automated filtering of spam emails for improved user experience.

Enterprise Email Security - Used in enterprise software to detect phishing and spam emails.

"},{"location":"projects/natural-language-processing/email_spam_detection/#features-planned-but-not-implemented","title":"FEATURES PLANNED BUT NOT IMPLEMENTED","text":"Feature 1
  • Integration of deep learning models (LSTM) for improved accuracy.
"},{"location":"projects/natural-language-processing/name_entity_recognition/","title":"Name Entity Recognition (NER) Project","text":""},{"location":"projects/natural-language-processing/name_entity_recognition/#aim","title":"AIM","text":"

To develop a system that identifies and classifies named entities (such as persons, organizations, locations, dates, etc.) in text using Named Entity Recognition (NER) with SpaCy.

"},{"location":"projects/natural-language-processing/name_entity_recognition/#dataset-link","title":"DATASET LINK","text":"

N/A (This project uses text input for NER analysis, not a specific dataset) - It uses real time data as input .

"},{"location":"projects/natural-language-processing/name_entity_recognition/#notebook-link","title":"NOTEBOOK LINK","text":"

[Note book link ] (https://colab.research.google.com/drive/1pBIEFA4a9LzyZKUFQMCypQ22M6bDbXM3?usp=sharing)

"},{"location":"projects/natural-language-processing/name_entity_recognition/#libraries-needed","title":"LIBRARIES NEEDED","text":"
  • SpaCy
"},{"location":"projects/natural-language-processing/name_entity_recognition/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • Named Entity Recognition (NER) is essential to automatically extract and classify key entities from text, such as persons, organizations, locations, and more.
  • This helps in analyzing and organizing data efficiently, enabling various NLP applications like document analysis and information retrieval.
Why is it necessary?
  • NER is used for understanding and structuring unstructured text, which is widely applied in industries such as healthcare, finance, and e-commerce.
  • It allows users to extract actionable insights from large volumes of text data
How is it beneficial and used?
  • NER plays a key role in tasks such as document summarization, information retrieval.
  • It automates the extraction of relevant entities, which reduces manual effort and improves efficiency.
How did you start approaching this project? (Initial thoughts and planning)
  • The project leverages SpaCy's pre-trained NER models, enabling easy text analysis without the need for training custom models.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#mention-any-additional-resources-used-blogs-books-chapters-articles-research-papers-etc","title":"Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.)","text":"
  • SpaCy Documentation: SpaCy NER
  • NLP in Python by Steven Bird et al.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#explanation","title":"EXPLANATION","text":""},{"location":"projects/natural-language-processing/name_entity_recognition/#details-of-the-different-entity-types","title":"DETAILS OF THE DIFFERENT ENTITY TYPES","text":"

The system extracts the following entity types:

Entity Type Description PERSON Names of people (e.g., \"Anuska\") ORG Organizations (e.g., \"Google\", \"Tesla\") LOC Locations (e.g., \"New York\", \"Mount Everest\") DATE Dates (e.g., \"January 1st, 2025\") GPE Geopolitical entities (e.g., \"India\", \"California\")"},{"location":"projects/natural-language-processing/name_entity_recognition/#what-i-have-done","title":"WHAT I HAVE DONE","text":""},{"location":"projects/natural-language-processing/name_entity_recognition/#step-1-data-collection-and-preparation","title":"Step 1: Data collection and preparation","text":"
  • Gathered sample text for analysis (provided by users in the app).
  • Explored the text structure and identified entity types.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#step-2-ner-model-implementation","title":"Step 2: NER model implementation","text":"
  • Integrated SpaCy's pre-trained NER model (en_core_web_sm).
  • Extracted named entities and visualized them with labels and color coding.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#step-3-testing-and-validation","title":"Step 3: Testing and validation","text":"
  • Validated results with multiple test cases to ensure entity accuracy.
  • Allowed users to input custom text for NER analysis in real-time.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":""},{"location":"projects/natural-language-processing/name_entity_recognition/#trade-off-1-pre-trained-model-vs-custom-model","title":"Trade Off 1: Pre-trained model vs. custom model","text":"
  • Pre-trained models provide quick results but may lack accuracy for domain-specific entities.
  • Custom models can improve accuracy but require additional data and training time.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#trade-off-2-real-time-analysis-vs-batch-processing","title":"Trade Off 2: Real-time analysis vs. batch processing","text":"
  • Real-time analysis in a web app enhances user interaction but might slow down with large text inputs.
  • Batch processing could be more efficient for larger datasets.
"},{"location":"projects/natural-language-processing/name_entity_recognition/#screenshots","title":"SCREENSHOTS","text":""},{"location":"projects/natural-language-processing/name_entity_recognition/#ner-example","title":"NER Example","text":"

``` mermaid graph LR A[Start] --> B[Text Input]; B --> C[NER Analysis]; C --> D{Entities Extracted}; D -->|Person| E[Anuska]; D -->|Location| F[New York]; D -->|Organization| G[Google]; D -->|Date| H[January 1st, 2025];

"},{"location":"projects/natural-language-processing/next-word-pred/","title":"Next Word Prediction using LSTM","text":""},{"location":"projects/natural-language-processing/next-word-pred/#aim","title":"AIM","text":"

To predict the next word using LSTM.

"},{"location":"projects/natural-language-processing/next-word-pred/#dataset-link","title":"DATASET LINK","text":"

Dataset

"},{"location":"projects/natural-language-processing/next-word-pred/#notebook-link","title":"NOTEBOOK LINK","text":"

Code

"},{"location":"projects/natural-language-processing/next-word-pred/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn
  • matplotlib
  • seaborn
  • tensorflow
  • keras
"},{"location":"projects/natural-language-processing/next-word-pred/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • To create an intelligent system capable of predicting the next word in a sentence based on its context.
  • The need for such a system arises in applications like autocomplete, chatbots, and virtual assistants.
Why is it necessary?
  • Enhances user experience in text-based applications by offering accurate suggestions.
  • Reduces typing effort, especially in mobile applications.
How is it beneficial and used?
  • Improves productivity: By predicting words, users can complete sentences faster.
  • Supports accessibility: Assists individuals with disabilities in typing.
  • Boosts efficiency: Helps in real-time text generation in NLP applications like chatbots and email composition.
How did you start approaching this project? (Initial thoughts and planning)
  • Studied LSTM architecture and its suitability for sequential data.
  • Explored similar projects and research papers to understand data preprocessing techniques.
  • Experimented with tokenization, padding, and sequence generation for the dataset.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Blogs on LSTM from Towards Data Science.
  • TensorFlow and Keras official documentation.
"},{"location":"projects/natural-language-processing/next-word-pred/#explanation","title":"EXPLANATION","text":""},{"location":"projects/natural-language-processing/next-word-pred/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":""},{"location":"projects/natural-language-processing/next-word-pred/#project-workflow","title":"PROJECT WORKFLOW","text":"Step 1Step 2Step 3Step 4Step 5Step 6

Initial data exploration and understanding:

  • Gathered text data from open-source datasets.
  • Analyzed the structure of the data.
  • Performed basic text statistics to understand word frequency and distribution.

Data cleaning and preprocessing

  • Removed punctuation and convert text to lowercase.
  • Tokenized text into sequences and pad them to uniform length.

Feature engineering and selection

  • Created input-output pairs for next-word prediction using sliding window techniques on tokenized sequences.

Model training and evaluation:

  • Used an embedding layer to represent words in a dense vector space.
  • Implemented LSTM-based sequential models to learn context and dependencies in text.
  • Experimented with hyperparameters like sequence length, LSTM units, learning rate, and batch size.

Model optimization and fine-tuning

  • Adjusted hyperparameters like embedding size, LSTM units, and learning rate.

Validation and testing

  • Used metrics like accuracy and perplexity to assess prediction quality.
  • Validated the model on unseen data to test generalization.
"},{"location":"projects/natural-language-processing/next-word-pred/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade-Off 1Trade-Off 2

Accuracy vs Training Time:

  • Solution: Balanced by reducing the model's complexity and using an efficient optimizer.

Model complexity vs. Overfitting:

  • Solution: Implemented dropout layers and monitored validation loss during training.
"},{"location":"projects/natural-language-processing/next-word-pred/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Data Preprocessed?};\n    B -->|No| C[Clean and Tokenize];\n    C --> D[Create Sequences];\n    D --> B;\n    B -->|Yes| E[Model Designed?];\n    E -->|No| F[Build LSTM/Transformer];\n    F --> E;\n    E -->|Yes| G[Train Model];\n    G --> H{Performant?};\n    H -->|No| I[Optimize Hyperparameters];\n    I --> G;\n    H -->|Yes| J[Deploy Model];\n    J --> K[End];
"},{"location":"projects/natural-language-processing/next-word-pred/#models-used-and-their-evaluation-metrics","title":"MODELS USED AND THEIR EVALUATION METRICS","text":"Model Accuracy MSE R2 Score LSTM 72% - -"},{"location":"projects/natural-language-processing/next-word-pred/#models-comparison-graphs","title":"MODELS COMPARISON GRAPHS","text":"

Models Comparison Graphs

LSTM Loss

"},{"location":"projects/natural-language-processing/next-word-pred/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/natural-language-processing/next-word-pred/#key-learnings","title":"KEY LEARNINGS","text":"

Insights gained from the data

  • The importance of preprocessing for NLP tasks.
  • How padding and embeddings improve the model\u2019s ability to generalize.
Improvements in understanding machine learning concepts
  • Learned how LSTMs handle sequential dependencies.
  • Understood the role of softmax activation in predicting word probabilities.
Challenges faced and how they were overcome
  • Challenge: Large vocabulary size causing high memory usage.
  • Solution: Limited vocabulary to the top frequent words.
"},{"location":"projects/natural-language-processing/next-word-pred/#use-cases","title":"USE CASES","text":"Application 1Application 2

Text Autocompletion

  • Used in applications like Gmail and search engines to enhance typing speed.

Virtual Assistants

  • Enables better conversational capabilities in chatbots and AI assistants.
"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/","title":"Twitter Sentiment Analysis","text":""},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#aim","title":"AIM","text":"

To analyze sentiment in Twitter data using natural language processing techniques.

"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/datasets/kazanova/sentiment140

"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#notebook-link","title":"NOTEBOOK LINK","text":"

https://drive.google.com/drive/folders/1F6BLxvp6qIAgGZOZ2rC370EmKhj5W1FC?usp=sharing

"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • pandas
  • numpy
  • scikit-learn
  • seaborn
  • matplotlib
  • tensorflow
  • keras
  • nltk
  • multiprocessing
  • tqdm
  • os
"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • The project aims to perform sentiment analysis on Twitter data.
  • This involves extracting tweets related to specific topics or keywords, processing these tweets using natural language processing (NLP) techniques to determine the sentiment (positive or negative), and presenting insights derived from the analysis.
Why is it necessary?
  • Twitter is a rich source of real-time public opinion and sentiment. Analyzing tweets can provide valuable insights into public perception of events, products, brands, or topics of interest.
  • This information is crucial for businesses, governments, and researchers to make informed decisions, understand public sentiment trends, and gauge the success of marketing campaigns or policy changes.
How is it beneficial and used?
  • Business Insights: Companies can understand customer feedback and sentiments towards their products or services.
  • Brand Management: Monitor brand sentiment and respond to customer concerns or issues in real-time.
  • Market Research: Identify trends and sentiments related to specific topics or industries.
  • Social Listening: Understand public opinion on current events, policies, or social issues.
  • Customer Service Improvement: Improve customer service by analyzing sentiment towards customer interactions.
How did you start approaching this project? (Initial thoughts and planning)
  • Choose appropriate NLP techniques for sentiment analysis, such as:

    • Bag-of-Words (BoW) and TF-IDF: Represent tweets as numerical vectors.
    • Sentiment Lexicons: Use dictionaries of words annotated with sentiment scores (e.g., Vader sentiment lexicon).
    • Machine Learning Models: Train supervised classifiers (e.g., Naive Bayes, SVM, or neural networks) on labeled data for sentiment prediction.
  • Model Evaluation: Evaluate the performance of the sentiment analysis model using metrics like accuracy. Cross-validation techniques can be used to ensure robustness.

  • Visualization and Insights: Visualize sentiment trends over time or across different categories using charts (e.g., line plots, bar charts). Generate insights based on the analysis results.

  • Deployment: Deploy the sentiment analysis system as a standalone application or integrate it into existing systems for real-time monitoring and analysis.

Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.)
  • GeeksforGeeks Twitter Sentiment Analysis
  • YouTube Video
"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#explanation","title":"EXPLANATION","text":""},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":""},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5Step 6

Initial data exploration and understanding:

  • Gathered Twitter data using pre-existing datasets (Kaggle).
  • Understand the structure of the data (e.g., tweet text, metadata like timestamps, user information).
  • Explore basic statistics and distributions of data features.

Data cleaning and preprocessing:

  • Remove or handle noisy data such as URLs, special characters, and emojis.
  • Tokenize tweets into individual words or tokens.
  • Remove stopwords (commonly used words that do not carry significant meaning).
  • Normalize text through techniques like stemming to reduce variations of words.

Feature engineering and selection:

  • Convert text data into numerical representations suitable for machine learning models (e.g., Bag-of-Words, TF-IDF).
  • Select relevant features that contribute most to the sentiment analysis task.

Model training and evaluation:

  • Split the dataset into training and testing sets.
  • Choose appropriate machine learning models (e.g., Naive Bayes, RNN LSTM, logistic regression) for sentiment analysis.
  • Train the models on the training data and evaluate their performance using metrics like accuracy.

Model optimization and fine-tuning:

  • Fine-tune the hyperparameters of the selected models to improve performance.
  • Consider techniques like grid search or random search to find optimal parameters.
  • Experiment with different models or combinations of models to achieve better results.

Validation and testing:

  • Validate the trained models on a separate validation set to ensure generalizability.
  • Test the final model on unseen data (testing set or new tweets) to assess its performance in real-world scenarios.
  • Iterate on the model and preprocessing steps based on validation results to improve accuracy and robustness.
"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade-off 1Trade-off 2

Stemming process took a lot of computational time to process over 1.6 million datapoints.

  • Solution: Divided the data into batches and applied parallel processing.

In RNN based LSTM, overfitting problem occurred.

  • Solution: Tried to fix it using Dropout layer, early stopping criteria.
"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#screenshots","title":"SCREENSHOTS","text":"

Project structure or tree diagram

  graph LR\n  A[Start] --> B{Error?};\n  B -->|Yes| C[Hmm...];\n  C --> D[Debug];\n  D --> B;\n  B ---->|No| E[Yay!];
Visualizations and EDA of different features Sentiment Distribution

Model performance graphs LR Confusion MatrixLR ROC CurveNaive Bayes Confusion MatrixNaive Bayes ROC Curve

"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#models-used-and-their-evaluation-metrics","title":"MODELS USED AND THEIR EVALUATION METRICS","text":"Model Accuracy MSE R2 Score Logistic Regression 77% 0.1531724703945824 0.3873101184216704 Naive Bayes 75% 0.17476773790874897 0.3009290483650041 RNN LSTM 77.84% - -"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#models-comparison-graphs","title":"MODELS COMPARISON GRAPHS","text":"

Models Comparison Graphs

LSTM AccuracyLSTM Loss

"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#conclusion","title":"CONCLUSION","text":""},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#key-learnings","title":"KEY LEARNINGS","text":"

Insights gained from the data

  • Data Variety: Twitter data provides a rich source of real-time, diverse opinions and sentiments.
  • Text Preprocessing: Importance of cleaning and preprocessing text data (e.g., removing stopwords, stemming/lemmatization) for better analysis.
  • Feature Extraction: Techniques like TF-IDF (Term Frequency-Inverse Document Frequency) and word embeddings (e.g., Word2Vec, GloVe) to represent text numerically for machine learning models.
Improvements in understanding machine learning concepts
  • Model Selection: Experimenting with various algorithms to find the most suitable for sentiment classification (e.g., logistic regression, naive bayes, neural networks).
Challenges faced and how they were overcome
  • Noise in Data: Dealing with noise from hashtags, emojis, and slang in tweets through effective preprocessing techniques.
  • Computational Resources: Managing large volumes of data and resource-intensive computations by optimizing code and leveraging cloud computing platforms if necessary.
"},{"location":"projects/natural-language-processing/twitter_sentiment_analysis/#use-cases","title":"USE CASES","text":"Application 1Application 2

Brand Monitoring and Customer Feedback Analysis

  • This application allows businesses to leverage Twitter sentiment analysis as a valuable tool for customer relationship management, brand reputation management, and strategic decision-making based on real-time customer feedback and sentiment analysis.

Financial Market Analysis and Investment Decisions

  • This application showcases how Twitter sentiment analysis can be leveraged in the financial sector to gain competitive advantages, improve investment strategies, and manage risks effectively based on public sentiment towards financial markets and specific stocks.
"},{"location":"projects/statistics/","title":"Statistics \ud83d\udcc3","text":"Bangladesh Premier League Analysis

Exploring team performances, player stats, and key insights from the BPL.

\ud83d\udcc5 2025-01-10 | \u23f1\ufe0f 10 mins

"},{"location":"projects/statistics/bangladesh-premier-league-analysis/","title":"Bangladesh Premier League Analysis","text":""},{"location":"projects/statistics/bangladesh-premier-league-analysis/#aim","title":"AIM","text":"

The main goal of the project is to analyze the performance of the bangladesh players in their premier league and obtaining the top 5 players in all of them in different fields like bowling, batting, toss_winner, highest runner, man of the match, etc.

"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#dataset-link","title":"DATASET LINK","text":"

https://www.kaggle.com/abdunnoor11/bpl-data

"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#my-notebook-link","title":"MY NOTEBOOK LINK","text":"

https://colab.research.google.com/drive/1equud2jwKnmE1qbbTJLsi2BbjuA7B1Si?usp=sharing

"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#libraries-needed","title":"LIBRARIES NEEDED","text":"LIBRARIES USED
  • matplotlib
  • pandas
  • sklearn
  • seaborn
  • numpy
  • scipy
  • xgboost
  • Tensorflow
  • Keras
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#description","title":"DESCRIPTION","text":"

What is the requirement of the project?

  • This project aims to analyze player performance data from the Bangladesh Premier League (BPL) to classify players into categories such as best, good, average, and poor based on their performance.
  • The analysis provides valuable insights for players and coaches, highlighting who needs more training and who requires less, which can aid in strategic planning for future matches.
Why is it necessary?
  • Analyzing player performance helps in understanding strengths and weaknesses, which can significantly reduce the chances of losing and increase the chances of winning future matches.
  • It aids in making informed decisions about team selection and match strategies.
How is it beneficial and used?
  • For Players: Provides feedback on their performance, helping them to improve specific aspects of their game.
  • For Coaches: Helps in identifying areas where players need improvement, which can be focused on during training sessions.
  • For Team Management: Assists in strategic decision-making regarding player selection and match planning.
  • For Fans and Analysts: Offers insights into player performances and trends over the league, enhancing the understanding and enjoyment of the game.
How did you start approaching this project? (Initial thoughts and planning)
  • Perform initial data exploration to understand the structure and contents of the dataset.
  • To learn about the topic and searching the related content like what is league, About bangladesh league, their players and much more.
  • Learn about the features in details by searching on the google or quora.
Mention any additional resources used (blogs, books, chapters, articles, research papers, etc.).
  • Articles on cricket analytics from websites such as ESPNcricinfo and Cricbuzz.
  • https://www.linkedin.com/pulse/premier-league-202223-data-analysis-part-i-ayomide-aremu-cole-iwn4e/
  • https://analyisport.com/insights/how-is-data-used-in-the-premier-league/
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#explanation","title":"EXPLANATION","text":""},{"location":"projects/statistics/bangladesh-premier-league-analysis/#details-of-the-different-features","title":"DETAILS OF THE DIFFERENT FEATURES","text":"
There are 3 different types of the datasets.\n\n- Batsman Dataset\n- Bowler Dataset\n- BPL (Bangladesh Premier League) Dataset\n
  • There are 12 features in Batsman Dataset
Feature Name Description id All matches unique id season Season match_no Number of matches date Date of Play player_name Player Name comment How did the batsman get out? R Batsman's run B How many balls faced the batsman? M How long their innings was in minutes? fours Fours sixs Sixes SR Strike rate
  • There are 12 features in Bowler Dataset
Feature Name Description id All matches unique id season Season match_no Number of matches date Date of Play player_name Player Name O Overs M middle overs R Runs W Wickets ECON The average number of runs they have conceded per over bowled WD Wide balls NB No balls
  • There are 19 features in BPL Dataset
Feature Name Description id All matches unique id season Season match_no Number of matches date Date of Play team_1 First Team team_1_score First Team Score team_2 Second Team team_2_score Second Team Score player_of_match Which team won the toss? toss_winner Which team won the toss? toss_decision Toss winner team decision winner Match Winner venue Venue city City win_by_wickets Win by wickets. win_by_runs Win by runs result Result of the winner umpire_1 First Umpire Name umpire_2 Second Umpire Name"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#what-i-have-done","title":"WHAT I HAVE DONE","text":"Step 1Step 2Step 3Step 4Step 5Step 6Step 7Step 8
  • Performed Exploratory Data Analysis on data.
  • Created data visualisations to understand the data in a better way.
  • Found strong relationships between independent features and dependent feature using correlation.
  • Handled missing values using strong correlations,dropping unnecessary ones.
  • Used different Regression techniques like Linear Regression,Ridge Regression,Lasso Regression and deep neural networks to predict the dependent feature in most suitable manner.
  • Compared various models and used best performance model to make predictions.
  • Used Mean Squared Error and R2 Score for evaluating model's performance.
  • Visualized best model's performance using matplotlib and seaborn library.
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#project-trade-offs-and-solutions","title":"PROJECT TRADE-OFFS AND SOLUTIONS","text":"Trade Off 1Trade Off 2Trade Off 3Trade Off 4

Handling missing and inconsistent data entries.

  • Solution
  • Data Imputation: For missing numerical values, I used techniques such as mean, median, or mode imputation based on the distribution of the data.
  • Data Cleaning: For inconsistent entries, I standardized the data by removing duplicates, correcting typos, and ensuring uniform formatting.
  • Dropping Irrelevant Data: In cases where the missing data was extensive and could not be reliably imputed, I decided to drop those rows/columns to maintain data integrity.

Extracting target variables from the dataset.

  • Solution
  • Feature Engineering: Created new features that could serve as target variables, such as aggregating player statistics to determine top performers.
  • Domain Knowledge: Utilized cricket domain knowledge to identify relevant metrics (e.g., strike rate, economy rate) and used them to define target variables.
  • Label Encoding: For categorical target variables (e.g., player categories like best, good, average, poor), I used label encoding techniques to convert them into numerical format for analysis.

Creating clear and informative visualizations that effectively communicate the findings.

  • Solution
  • Tool Selection: Used powerful visualization tools like Matplotlib and Seaborn in Python, which provide a wide range of customization options.
  • Visualization Best Practices: Followed best practices such as using appropriate chart types (e.g., bar charts for categorical data, scatter plots for correlations), adding labels and titles, and ensuring readability.
  • Iterative Refinement: Iteratively refined visualizations based on feedback and self-review to enhance clarity and informativeness.

Correctly interpreting the results to provide actionable insights.

  • Solution
  • Cross-validation: Used cross-validation techniques to ensure the reliability and accuracy of the analysis results.
  • Collaboration with Experts: Engaged with cricket experts and enthusiasts to validate the findings and gain additional perspectives.
  • Contextual Understanding: Interpreted results within the context of the game, considering factors such as player roles, match conditions, and historical performance to provide meaningful and actionable insights.
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#screenshots","title":"SCREENSHOTS","text":"

Project workflow

  graph LR\n    A[Start] --> B{Error?};\n    B -->|Yes| C[Hmm...];\n    C --> D[Debug];\n    D --> B;\n    B ---->|No| E[Yay!];
Visualizations and EDA of different features Top 5 Player Of MatchTop 5 Batsman RunnersTop 5 Four RunnersTop 5 OversTop 5 RunsTop 5 UmpiresTop 5 WicketsToss Winners

"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#models-used-and-their-accuracies","title":"MODELS USED AND THEIR ACCURACIES","text":"Model MSE R2 Random Forest Regression 19.355984 0.371316 Gradient Boosting Regression 19.420494 0.369221 XG Boost Regression 21.349168 0.306577 Ridge Regression 26.813981 0.129080 Linear Regression 26.916888 0.125737 Deep Neural Network 27.758216 0.098411 Decision Tree Regression 29.044533 0.056631"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#models-comparison-graphs","title":"MODELS COMPARISON GRAPHS","text":"

Models Comparison Graphs

RF Regression PlotConclusion Graph

"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#conclusion","title":"CONCLUSION","text":"
We can see that R2 Score and Mean Absolute Error is best for Random Forest Regression.\nBy Using Neural network, We cannot get the minimum Mean Squared Error value possible.\nRandom Forest Regression model can predict most accurate results for predicting bangladesh premier league winning team which is the highest model performance in comparison with other Models.\n
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#what-you-have-learned","title":"WHAT YOU HAVE LEARNED","text":"

Insights gained from the data

  • Identified key performance indicators for players in the Bangladesh Premier League, such as top scorers, best bowlers, and players with the most man of the match awards.
  • Discovered trends and patterns in player performances that could inform future strategies and training programs.
  • Gained a deeper understanding of the distribution of player performances across different matches and seasons.
Improvements in understanding machine learning concepts
  • Enhanced knowledge of data cleaning and preprocessing techniques to handle real-world datasets.
  • Improved skills in exploratory data analysis (EDA) to extract meaningful insights from raw data.
  • Learned how to use visualization tools to effectively communicate data-driven findings.
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#use-cases-of-this-model","title":"USE CASES OF THIS MODEL","text":"Application 1Application 2

Team Selection and Strategy Planning

  • Coaches and team managers can use the model to analyze player performance data and make informed decisions about team selection and match strategies. By identifying top performers and areas for improvement, the model can help optimize team composition and tactics for future matches.

Player Performance Monitoring and Training

  • The model can be used to track player performance over time and identify trends in their performance. This information can be used by coaches to tailor training programs to address specific weaknesses and enhance overall player development. By monitoring performance metrics, the model can help ensure that players are continuously improving.
"},{"location":"projects/statistics/bangladesh-premier-league-analysis/#features-planned-but-not-implemented","title":"FEATURES PLANNED BUT NOT IMPLEMENTED","text":"Feature 1Feature 2

Real-time Performance Tracking

  • Implementing a real-time tracking system to update player performance metrics during live matches.

Advanced Predictive Analytics - Using advanced machine learning algorithms to predict future player performances and match outcomes.

"}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml new file mode 100644 index 00000000..8db91de5 --- /dev/null +++ b/sitemap.xml @@ -0,0 +1,475 @@ + + + + https://avdhesh-varshney.github.io/AI-Code/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithm-readme-template/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/contribute/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/project-readme-template/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/artificial-intelligence/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/artificial-intelligence/evolutionary-algorithms/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/artificial-intelligence/expert-systems/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/artificial-intelligence/knowledge-based-systems/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/artificial-intelligence/reinforcement-learning/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/artificial-intelligence/search-and-optimization/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/computer-vision/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/computer-vision/image-augmentation/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/computer-vision/image-processing/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/computer-vision/object-detection/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/computer-vision/semantic-segmentation/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/deep-learning/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/deep-learning/architectures/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/deep-learning/neural-networks/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/deep-learning/neural-networks/convolutional-neural-network/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/deep-learning/optimization-algorithms/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/deep-learning/pre-trained-models/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/generative-adversarial-networks/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/generative-adversarial-networks/ac-gan/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/generative-adversarial-networks/basic-gan/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/generative-adversarial-networks/c-gan/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/generative-adversarial-networks/eb-gan/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/generative-adversarial-networks/info-gan/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/large-language-models/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/large-language-models/bert/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/large-language-models/bloom/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/large-language-models/gpt-series/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/large-language-models/t5/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/boosting/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/boosting/light-gbm/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/encoding/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/encoding/ordinal-encoder/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/imputation/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/min-max-scaler/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/data-preprocessing/scaling-and-normalization/standard-scaler/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/classifications/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/AdaBoost_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Decision_Tree_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Elastic_Net_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Gradient_Boosting_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Huber_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/K_Nearest_Neighbors_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Lasso_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Logistic_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Neural_Network_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Polynomial_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Random_Forest_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Ridge_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/Support_Vector_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/XG_Boost_Regression/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/bayesian/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/supervised/regressions/linear/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/unsupervised/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/unsupervised/clustering/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/unsupervised/clustering/kmeans-clustering/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/machine-learning/unsupervised/dimensionality-reduction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Bag_Of_Words/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Fast_Text/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/GloVe/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/NLTK_Setup/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/N_L_P_Introduction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Text_PreProcessing_Techniques/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Tf_Idf/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Transformers/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Word_2_Vec/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/natural-language-processing/Word_Embeddings/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/descriptive/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/inferential/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/errors/Mean_Absolute_Error/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/errors/Mean_Squared_Error/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/errors/R2_Squared_Error/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/errors/Root_Mean_Squared_Error/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/loss-functions/Cross_Entropy_Loss/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/loss-functions/Hinge_Loss/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/loss-functions/Kullback_Leibler_Divergence_Loss/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/metrics-and-losses/loss-functions/Ranking_Losses/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/algorithms/statistics/probability/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/artificial-intelligence/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/computer-vision/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/computer-vision/black_and_white_image_colorizer/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/computer-vision/brightness-control/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/computer-vision/counting-bicep-reps/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/computer-vision/face-detection/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/computer-vision/music_genre_classification_model/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/deep-learning/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/deep-learning/brain-tumor-detection-model/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/generative-adversarial-networks/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/large-language-models/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/air-quality-prediction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/cardiovascular-disease-prediction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/health-insurance-cross-sell-prediction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/heart-disease-detection-model/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/poker-hand-prediction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/sleep-quality-prediction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/machine-learning/used-cars-price-prediction/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/natural-language-processing/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/natural-language-processing/chatbot-project-implementation/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/natural-language-processing/email_spam_detection/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/natural-language-processing/name_entity_recognition/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/natural-language-processing/next-word-pred/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/natural-language-processing/twitter_sentiment_analysis/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/statistics/ + 2025-01-26 + + + https://avdhesh-varshney.github.io/AI-Code/projects/statistics/bangladesh-premier-league-analysis/ + 2025-01-26 + + \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz new file mode 100644 index 0000000000000000000000000000000000000000..a461d16368b25307d857b3eed206ef7d10cf1fba GIT binary patch literal 1434 zcmV;L1!ejliwFn+dzEJb|8r?{Wo=<_E_iKh0NtEfZ`?Kz$KU%Y48POs7)a3;j)NS| zfw-yRxM|;r5jCr2%|n@?*4}*j4&`JUH*snp4;gk}XwlN@H@xOQJofP6RSY|8B=$Ky z*uA&6zq^CU<=&^^!S3(p&&*G|508F0oG37s9l1I2gWXjr<1dTF<>lob`+^Zh-pe#B z*sbJ(7auYg`+Gm{9{sQ*pZW#Yf6br6klj)J7M8Dpb-zMhnKclv5^nYeUshAM=kwyx zOY{Ss%W+q##%6dieGx?AsC+uhTsS3WT+hv*Evi@1_q#wX{S{2 zYD6lAH!RIjJ?%xxmjU|$O$Us9E7*iSEi*Zcn2HGql*DCdr)J@FD+8%Fc`QEq_fXrn z7HpT}C?h(VwP(dxK7uq?n1)*SstfR%RGgGUW<~Au-<-Ldy=ad`62;}oBI{D|&M@k$ z{2p;sHxk{1s-usubvzNWPQ-*0$*`HQTxPmx1X9%SR;$$PPyegaf=}I#LTz;ub|jJZ z1U`vAk@sRy!zpeZNeXc-{;02EERa{&djAU(oEadsLMuYmHwD}jEOsr?)+j&|ZvYa- z7a$E&m43QLt1rWK60IsH3^_;Lh{GrmKaxk~m0j*@;fo+zG@&}h{25o5GnXa+gCADK z3|&-<+Cu?}hf!Zhd>7P@D-%sJXOcu*=bVAmk`2Dj0;r1h??TNEf~50oU&<8a>iz%7 zi0G4vaAoQpZ8fLcZQ>$%K)rUOcp8z`p0RHM(aGz%&WF#1%q<`j4pdu9gSd}+cw7a6 zJ=R@!c6*Lj zr3KjMA-}~IKwqDnzp`(zOk)$Y_Qamrm#I*0XfUXa)}9icZnLBvyH)bqJQ`?{v5rhvJw&efR^jYK`NBR|$XL<;GAS|VX8 z9)d!;h=nAbm&!tBMs5QViL#78zl!X(k2nl$5u_&jVE|J4*i2kye2pzi{beJU=A=96LV8g&Ng zqUz}rt=>V@QwN=|GZ-3pL=$E{!JmUpPM4=tw;GtvI~)z+$T~rn;84lA$y*uY#NJuy-z4qx#$GHt z?+VvJt>^gUEl6EGfo94SGyPNJMo|fBK8&`7)OSE;i>B&C!tGSx$w)*pFUgD&mv}`( z&u6d>sP}md>?Wys(|bl>)X%*_2xTSOh$jLOM#j|;%CIPhHQpM`S@2Ii(I*>oV@00Nn~omy7xMOH@9N0d@Q~#hv&uf)AEQ zh*p`5{Tg+Gh_=p}myIQKc}TtyaJ(u;d2A o^d+87iZei8pOm_eUfi1u$-^Y-{F{q#P8iG4-?6uQZb4B10DB+JeE