Spaces:
Sleeping
Sleeping
File size: 51,194 Bytes
d6212ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 |
import streamlit as st
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import LinearSVC, SVC
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import RidgeClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split, learning_curve
import matplotlib.pyplot as plt
import seaborn as sns
def setup_page_config():
"""Configure the Streamlit page"""
st.set_page_config(
page_title="Algorithm Education",
page_icon="🤖",
layout="wide"
)
def page_introduction():
"""Display the introduction section of the page"""
st.title("Machine Learning Algorithm Education 🎓")
st.markdown("""
Welcome to the Algorithm Education page! This interactive guide helps you understand various machine learning
algorithms used in classification tasks. Each algorithm is explained in detail with:
- 📝 Clear descriptions and explanations
- ✅ Advantages and limitations
- 🎯 Practical use cases
- 📊 Mathematical foundations
- 💻 Implementation examples
- 🔬 Interactive demonstrations
- 📚 Academic references
### How to Use This Guide
1. Select an algorithm from the dropdown menu below
2. Explore its characteristics and implementation details
3. Try the interactive demo with different datasets
4. Compare performance metrics and visualizations
### Available Algorithms
This guide covers popular classification algorithms including:
- Naive Bayes variants
- Support Vector Machines
- Neural Networks
- Tree-based methods
- Nearest Neighbors
- Linear Classifiers
- Ensemble Methods
### Why Understanding Algorithms Matters
Choosing the right algorithm for your machine learning task is crucial for:
- Achieving optimal performance
- Efficient resource utilization
- Meeting specific problem constraints
- Understanding model behavior and limitations
""")
def algorithm_info():
"""Display detailed algorithm information"""
# First show the introduction
page_introduction()
algorithms = {
"Gaussian Naive Bayes (GaussianNB)": {
"description": """
A probabilistic classifier based on Bayes' theorem with strong independence assumptions between features.
Assumes features follow a Gaussian (normal) distribution.
""",
"pros": [
"Simple and fast",
"Works well with small datasets",
"Good for high-dimensional data",
"Performs well when features are normally distributed"
],
"cons": [
"Assumes feature independence (often unrealistic)",
"Limited by Gaussian distribution assumption",
"May underperform when features are highly correlated"
],
"use_cases": [
"Text classification",
"Spam detection",
"Medical diagnosis",
"Real-time prediction scenarios"
],
"math_details": {
"main_formula": r"""
P(y|x_1,...,x_n) = \frac{P(y)\prod_{i=1}^{n}P(x_i|y)}{P(x_1,...,x_n)}
""",
"component_formulas": [
{
"name": "Gaussian Probability Density",
"formula": r"""
P(x_i|y) = \frac{1}{\sqrt{2\pi\sigma^2_y}} \exp\left(-\frac{(x_i-\mu_y)^2}{2\sigma^2_y}\right)
"""
},
{
"name": "Class Prior Probability",
"formula": r"""
P(y) = \frac{\text{number of samples in class y}}{\text{total number of samples}}
"""
}
],
"explanation": """
- P(y|x₁,...,xₙ) is the posterior probability of class y given features
- P(y) is the prior probability of class y
- P(xᵢ|y) is the likelihood of feature xᵢ given class y
- μy and σ²y are the mean and variance of features in class y
"""
},
"references": [
{
"title": "Naive Bayes and Text Classification",
"authors": "Sebastian Raschka",
"publication": "arXiv preprint",
"year": "2014",
"url": "https://arxiv.org/abs/1410.5329"
},
{
"title": "scikit-learn: Machine Learning in Python",
"authors": "Pedregosa et al.",
"publication": "Journal of Machine Learning Research",
"year": "2011",
"url": "https://jmlr.csail.mit.edu/papers/v12/pedregosa11a.html"
},
{
"title": "Fundamental Mathematical Formulas Used in Machine Learning",
"authors": "Showmik Setta",
"publication": "Medium",
"year": "2023",
"url": "https://medium.com/@showmiklovesport/fundamental-mathematical-formulas-used-in-machine-learning-beginner-21c0843e61e0"
}
]
},
"Linear Support Vector Classification (LinearSVC)": {
"description": """
A linear classifier that finds the hyperplane that best separates classes by maximizing the margin between them.
Optimized implementation of Support Vector Classification for linear classification.
""",
"pros": [
"Effective for high-dimensional spaces",
"Memory efficient",
"Faster than standard SVC with linear kernel",
"Works well when classes are linearly separable"
],
"cons": [
"Only suitable for linear classification",
"Sensitive to feature scaling",
"May struggle with overlapping classes",
"No probability estimates by default"
],
"use_cases": [
"Text classification",
"Image classification",
"Bioinformatics",
"High-dimensional data analysis"
],
"math_details": {
"main_formula": r"""
\min_{w,b} \frac{1}{2}||w||^2 + C\sum_{i=1}^{n} \max(0, 1-y_i(w^Tx_i+b))
""",
"component_formulas": [
{
"name": "Decision Function",
"formula": r"""
f(x) = w^Tx + b
"""
},
{
"name": "Margin Width",
"formula": r"""
\text{margin} = \frac{2}{||w||}
"""
}
],
"explanation": """
- w is the weight vector
- b is the bias term
- C is the regularization parameter
- yᵢ are the true labels (±1)
- xᵢ are the input features
"""
},
"references": [
{
"title": "A Tutorial on Support Vector Machines for Pattern Recognition",
"authors": "Christopher J.C. Burges",
"publication": "Data Mining and Knowledge Discovery",
"year": "1998",
"url": "https://link.springer.com/article/10.1023/A:1009715923555"
},
{
"title": "Support Vector Machines",
"authors": "Andrew Ng",
"publication": "CS229 Lecture Notes, Stanford University",
"year": "2018",
"url": "http://cs229.stanford.edu/notes/cs229-notes3.pdf"
},
{
"title": "Machine Learning Algorithms: Mathematical Deep Dive",
"authors": "Vidushi Meel",
"publication": "viso.ai",
"year": "2021",
"url": "https://viso.ai/deep-learning/machine-learning-algorithms-mathematical-guide/"
}
]
},
"Support Vector Classification (SVC)": {
"description": """
A powerful classifier that can perform non-linear classification using different kernel functions to transform
the feature space. Creates an optimal hyperplane in a transformed feature space.
""",
"pros": [
"Effective for non-linear classification",
"Works well with high-dimensional data",
"Robust against overfitting",
"Versatile through different kernel functions"
],
"cons": [
"Computationally intensive for large datasets",
"Sensitive to feature scaling",
"Kernel selection can be challenging",
"Memory intensive for large datasets"
],
"use_cases": [
"Image classification",
"Handwriting recognition",
"Bioinformatics",
"Pattern recognition"
],
"math_details": {
"main_formula": r"""
\min_{w,b} \frac{1}{2}||w||^2 + C\sum_{i=1}^{n} \xi_i
""",
"component_formulas": [
{
"name": "Kernel Function (RBF)",
"formula": r"""
K(x,x') = \exp\left(-\gamma ||x-x'||^2\right)
"""
},
{
"name": "Decision Function",
"formula": r"""
f(x) = \sum_{i=1}^{n} \alpha_i y_i K(x_i,x) + b
"""
}
],
"explanation": """
- K(x,x') is the kernel function
- γ is the kernel coefficient
- αᵢ are the dual coefficients
- ξᵢ are the slack variables
"""
},
"references": [
{
"title": "Support Vector Networks",
"authors": "Cortes C., Vapnik V.",
"publication": "Machine Learning",
"year": "1995",
"url": "https://link.springer.com/article/10.1007/BF00994018"
},
{
"title": "A Practical Guide to Support Vector Classification",
"authors": "Hsu, Chang, and Lin",
"publication": "BJU International",
"year": "2003",
"url": "https://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf"
},
{
"title": "Machine Learning Algorithms: Mathematical Deep Dive",
"authors": "Vidushi Meel",
"publication": "viso.ai",
"year": "2021",
"url": "https://viso.ai/deep-learning/machine-learning-algorithms-mathematical-guide/"
}
]
},
"Multi-layer Perceptron (MLPClassifier)": {
"description": """
A neural network classifier that learns non-linear models by training multiple layers of nodes.
Each node uses a non-linear activation function to transform inputs.
""",
"pros": [
"Can learn highly non-linear patterns",
"Capable of learning complex relationships",
"Good generalization with proper regularization",
"Can handle multiple classes naturally"
],
"cons": [
"Requires careful hyperparameter tuning",
"Computationally intensive",
"Sensitive to feature scaling",
"May get stuck in local minima"
],
"use_cases": [
"Image recognition",
"Speech recognition",
"Complex pattern recognition",
"Financial prediction"
],
"math_details": {
"main_formula": r"""
h_l = \sigma(W_l h_{l-1} + b_l)
""",
"component_formulas": [
{
"name": "ReLU Activation",
"formula": r"""
\sigma(x) = \max(0,x)
"""
},
{
"name": "Softmax Output",
"formula": r"""
P(y=j|x) = \frac{e^{z_j}}{\sum_{k=1}^K e^{z_k}}
"""
}
],
"explanation": """
- hₗ is the output of layer l
- Wₗ is the weight matrix for layer l
- bₗ is the bias vector for layer l
- σ is the activation function
"""
},
"references": [
{
"title": "Learning representations by back-propagating errors",
"authors": "Rumelhart, D. E., Hinton, G. E., & Williams, R. J.",
"publication": "Nature",
"year": "1986",
"url": "https://www.nature.com/articles/323533a0"
},
{
"title": "Gradient-based learning applied to document recognition",
"authors": "LeCun Y., Bottou L., Bengio Y., & Haffner P.",
"publication": "Proceedings of the IEEE",
"year": "1998",
"url": "https://ieeexplore.ieee.org/document/726791"
},
{
"title": "Fundamental Mathematical Formulas Used in Machine Learning",
"authors": "Showmik Setta",
"publication": "Medium",
"year": "2023",
"url": "https://medium.com/@showmiklovesport/fundamental-mathematical-formulas-used-in-machine-learning-beginner-21c0843e61e0"
}
]
},
"Extra Trees Classifier": {
"description": """
An ensemble method that builds multiple randomized decision trees and averages their predictions.
Similar to Random Forest but with additional randomization in the tree-building process.
""",
"pros": [
"Lower variance than Random Forest",
"Faster training than Random Forest",
"Good at handling high-dimensional data",
"Less prone to overfitting"
],
"cons": [
"May have slightly lower accuracy than Random Forest",
"Can be memory intensive",
"Less interpretable than single decision trees",
"May require more trees than Random Forest"
],
"use_cases": [
"Feature selection",
"Large dataset classification",
"Remote sensing",
"Biomedical classification"
],
"math_details": {
"main_formula": r"""
\hat{f}_{et}(x) = \frac{1}{B}\sum_{b=1}^B \hat{f}_b(x)
""",
"component_formulas": [
{
"name": "Random Split Selection",
"formula": r"""
\text{gain}(s,D) = \frac{|D_l|}{|D|}H(D_l) + \frac{|D_r|}{|D|}H(D_r)
"""
},
{
"name": "Entropy",
"formula": r"""
H(D) = -\sum_{k=1}^K p_k\log(p_k)
"""
}
],
"explanation": """
- B is the number of trees
- fᵦ is the prediction of the b-th tree
- Dₗ and Dᵣ are left and right splits
- pₖ is the proportion of class k in the node
"""
},
"references": [
{
"title": "Extremely randomized trees",
"authors": "Geurts P., Ernst D., & Wehenkel L.",
"publication": "Machine Learning",
"year": "2006",
"url": "https://link.springer.com/article/10.1007/s10994-006-6226-1"
},
{
"title": "scikit-learn: Machine Learning in Python",
"authors": "Pedregosa et al.",
"publication": "Journal of Machine Learning Research",
"year": "2011",
"url": "https://jmlr.csail.mit.edu/papers/v12/pedregosa11a.html"
},
{
"title": "Fundamental Mathematical Formulas Used in Machine Learning",
"authors": "Showmik Setta",
"publication": "Medium",
"year": "2023",
"url": "https://medium.com/@showmiklovesport/fundamental-mathematical-formulas-used-in-machine-learning-beginner-21c0843e61e0"
}
]
},
"Random Forest Classifier": {
"description": """
An ensemble learning method that constructs multiple decision trees and combines their predictions.
Each tree is built using a random subset of features and bootstrap samples of the data.
""",
"pros": [
"Robust against overfitting",
"Handles non-linear relationships well",
"Provides feature importance",
"Works well with high-dimensional data"
],
"cons": [
"Can be computationally intensive",
"Less interpretable than single decision trees",
"Memory intensive for large datasets",
"May overfit on noisy datasets"
],
"use_cases": [
"Credit risk assessment",
"Medical diagnosis",
"Market prediction",
"Image classification"
],
"math_details": {
"main_formula": r"""
\hat{f}_{rf}(x) = \frac{1}{B}\sum_{b=1}^B \hat{f}_b(x)
""",
"component_formulas": [
{
"name": "Random Split Selection",
"formula": r"""
\text{gain}(s,D) = \frac{|D_l|}{|D|}H(D_l) + \frac{|D_r|}{|D|}H(D_r)
"""
},
{
"name": "Entropy",
"formula": r"""
H(D) = -\sum_{k=1}^K p_k\log(p_k)
"""
}
],
"explanation": """
- B is the number of trees
- fᵦ is the prediction of the b-th tree
- Dₗ and Dᵣ are left and right splits
- pₖ is the proportion of class k in the node
"""
},
"references": [
{
"title": "Random Forests",
"authors": "Breiman L.",
"publication": "Machine Learning",
"year": "2001",
"url": "https://link.springer.com/article/10.1023/A:1010933404324"
},
{
"title": "An Introduction to Statistical Learning",
"authors": "James G., Witten D., Hastie T., & Tibshirani R.",
"publication": "Springer",
"year": "2013",
"url": "https://www.statlearning.com/"
},
{
"title": "Machine Learning Algorithms: Mathematical Deep Dive",
"authors": "Vidushi Meel",
"publication": "viso.ai",
"year": "2021",
"url": "https://viso.ai/deep-learning/machine-learning-algorithms-mathematical-guide/"
}
]
},
"K-Nearest Neighbors (KNeighborsClassifier)": {
"description": """
A non-parametric method that classifies a data point based on the majority class of its k nearest neighbors
in the feature space. Simple but effective algorithm.
""",
"pros": [
"Simple to understand and implement",
"No training phase",
"Naturally handles multi-class cases",
"Non-parametric (no assumptions about data)"
],
"cons": [
"Computationally intensive for large datasets",
"Sensitive to irrelevant features",
"Requires feature scaling",
"Memory intensive (stores all training data)"
],
"use_cases": [
"Recommendation systems",
"Pattern recognition",
"Data imputation",
"Anomaly detection"
],
"math_details": {
"main_formula": r"""
\hat{f}_{knn}(x) = \frac{1}{k}\sum_{i=1}^k y_i
""",
"component_formulas": [
{
"name": "Distance Function",
"formula": r"""
d(x,x') = \sum_{i=1}^p |x_i - x'_i|^2
"""
},
{
"name": "Decision Function",
"formula": r"""
f(x) = \text{sign}\left(\sum_{i=1}^k y_i \cdot \text{weight}(d(x,x_i))\right)
"""
}
],
"explanation": """
- d(x,x') is the distance function
- xᵢ are the k nearest neighbors
- yᵢ are the labels of the k nearest neighbors
- weight(d(x,x')) is the weight function based on distance
"""
},
"references": [
{
"title": "Nearest Neighbor Pattern Classification",
"authors": "Cover T. & Hart P.",
"publication": "IEEE Transactions on Information Theory",
"year": "1967",
"url": "https://ieeexplore.ieee.org/document/1053964"
},
{
"title": "A Survey of Nearest Neighbor Techniques",
"authors": "Bhatia N. & Vandana",
"publication": "International Journal of Computer Science and Information Security",
"year": "2010",
"url": "https://arxiv.org/abs/1007.0085"
},
{
"title": "Machine Learning Algorithms: Mathematical Deep Dive",
"authors": "Vidushi Meel",
"publication": "viso.ai",
"year": "2021",
"url": "https://viso.ai/deep-learning/machine-learning-algorithms-mathematical-guide/"
}
]
},
"Ridge Classifier": {
"description": """
A linear classifier that uses L2 regularization to prevent overfitting. Similar to logistic regression
but with different loss function and regularization.
""",
"pros": [
"Good for multicollinear data",
"Less prone to overfitting",
"Computationally efficient",
"Works well with many features"
],
"cons": [
"Only for linear classification",
"May underfit complex patterns",
"Sensitive to feature scaling",
"No probability estimates"
],
"use_cases": [
"High-dimensional data classification",
"Text classification",
"Gene expression analysis",
"Simple binary classification"
],
"math_details": {
"main_formula": r"""
\min_{w} ||Xw - y||^2_2 + \alpha ||w||^2_2
""",
"component_formulas": [
{
"name": "Decision Function",
"formula": r"""
f(x) = w^Tx
"""
},
{
"name": "L2 Penalty",
"formula": r"""
\text{penalty} = \alpha ||w||^2_2 = \alpha \sum_{j=1}^p w_j^2
"""
}
],
"explanation": """
- w is the weight vector
- α is the regularization strength
- X is the feature matrix
- y is the target vector
- p is the number of features
"""
},
"references": [
{
"title": "Ridge Regression: Biased Estimation for Nonorthogonal Problems",
"authors": "Hoerl A.E. & Kennard R.W.",
"publication": "Technometrics",
"year": "1970",
"url": "https://www.tandfonline.com/doi/abs/10.1080/00401706.1970.10488634"
},
{
"title": "The Elements of Statistical Learning",
"authors": "Hastie T., Tibshirani R., & Friedman J.",
"publication": "Springer",
"year": "2009",
"url": "https://web.stanford.edu/~hastie/ElemStatLearn/"
},
{
"title": "Fundamental Mathematical Formulas Used in Machine Learning",
"authors": "Showmik Setta",
"publication": "Medium",
"year": "2023",
"url": "https://medium.com/@showmiklovesport/fundamental-mathematical-formulas-used-in-machine-learning-beginner-21c0843e61e0"
}
]
},
"Multinomial Naive Bayes": {
"description": """
A specialized version of Naive Bayes for multinomially distributed data. Commonly used for text
classification with word counts.
""",
"pros": [
"Fast training and prediction",
"Works well with high-dimensional data",
"Good for text classification",
"Handles multiple classes well"
],
"cons": [
"Assumes feature independence",
"Requires non-negative features",
"Sensitive to feature distribution",
"May underperform with continuous data"
],
"use_cases": [
"Document classification",
"Spam detection",
"Language detection",
"Topic modeling"
],
"math_details": {
"main_formula": r"""
P(y|x) = \frac{P(y)\prod_{i=1}^n P(x_i|y)}{\sum_{k} P(y_k)\prod_{i=1}^n P(x_i|y_k)}
""",
"component_formulas": [
{
"name": "Feature Probability",
"formula": r"""
P(x_i|y) = \frac{N_{yi} + \alpha}{N_y + \alpha n}
"""
},
{
"name": "Log Probability",
"formula": r"""
\log P(y|x) = \log P(y) + \sum_{i=1}^n \log P(x_i|y)
"""
}
],
"explanation": """
- Nyᵢ is the count of feature i in class y
- Ny is the total count of all features in class y
- α is the smoothing parameter
- n is the number of features
"""
},
"references": [
{
"title": "A comparison of event models for naive Bayes text classification",
"authors": "McCallum A. & Nigam K.",
"publication": "AAAI-98 Workshop on Learning for Text Categorization",
"year": "1998",
"url": "https://www.cs.cmu.edu/~knigam/papers/multinomial-aaaiws98.pdf"
},
{
"title": "An empirical study of the naive Bayes classifier",
"authors": "Rish I.",
"publication": "IJCAI 2001 Workshop on Empirical Methods in Artificial Intelligence",
"year": "2001",
"url": "https://www.researchgate.net/publication/228845263_An_Empirical_Study_of_the_Naive_Bayes_Classifier"
},
{
"title": "Fundamental Mathematical Formulas Used in Machine Learning",
"authors": "Showmik Setta",
"publication": "Medium",
"year": "2023",
"url": "https://medium.com/@showmiklovesport/fundamental-mathematical-formulas-used-in-machine-learning-beginner-21c0843e61e0"
}
]
},
"AdaBoost Classifier": {
"description": """
An ensemble method that builds a strong classifier by iteratively adding weak learners, focusing on
previously misclassified examples.
""",
"pros": [
"Good generalization",
"Less prone to overfitting",
"Can identify hard-to-classify instances",
"Works well with weak learners"
],
"cons": [
"Sensitive to noisy data and outliers",
"Sequential nature (can't parallelize)",
"Can be computationally intensive",
"May require careful tuning"
],
"use_cases": [
"Face detection",
"Object recognition",
"Medical diagnosis",
"Fraud detection"
],
"math_details": {
"main_formula": r"""
F(x) = \text{sign}\left(\sum_{t=1}^T \alpha_t h_t(x)\right)
""",
"component_formulas": [
{
"name": "Weak Learner Weight",
"formula": r"""
\alpha_t = \frac{1}{2}\ln\left(\frac{1-\epsilon_t}{\epsilon_t}\right)
"""
},
{
"name": "Sample Weight Update",
"formula": r"""
w_{i,t+1} = w_{i,t}\exp(-y_i\alpha_th_t(x_i))
"""
}
],
"explanation": """
- hₜ(x) is the weak learner prediction
- αₜ is the weight of weak learner t
- εₜ is the weighted error rate
- wᵢ,ₜ is the weight of sample i at iteration t
"""
},
"references": [
{
"title": "A Decision-Theoretic Generalization of On-Line Learning and an Application to Boosting",
"authors": "Freund Y. & Schapire R.E.",
"publication": "Journal of Computer and System Sciences",
"year": "1997",
"url": "https://www.sciencedirect.com/science/article/pii/S002200009791504X"
},
{
"title": "Experiments with a New Boosting Algorithm",
"authors": "Freund Y. & Schapire R.E.",
"publication": "International Conference on Machine Learning",
"year": "1996",
"url": "https://icml.cc/Conferences/1996/papers/boosting.pdf"
},
{
"title": "Machine Learning Algorithms: Mathematical Deep Dive",
"authors": "Vidushi Meel",
"publication": "viso.ai",
"year": "2021",
"url": "https://viso.ai/deep-learning/machine-learning-algorithms-mathematical-guide/"
}
]
}
}
# Add implementation details to each algorithm
for algo_name in algorithms:
algorithms[algo_name]["implementation"] = {
"Gaussian Naive Bayes (GaussianNB)": {
"code": """
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import make_classification
# Create sample dataset
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2)
# Initialize and train the model
gnb = GaussianNB()
gnb.fit(X, y)
# Make predictions
y_pred = gnb.predict(X)
""",
"key_parameters": {
"var_smoothing": "Portion of the largest variance of all features that is added to variances for calculation stability",
"priors": "Prior probabilities of the classes"
},
"tips": [
"Normalize features if they have very different scales",
"Good as a baseline model for comparison",
"Check feature distributions - should be roughly Gaussian"
]
},
"Linear Support Vector Classification (LinearSVC)": {
"code": """
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
svc = LinearSVC(random_state=42, max_iter=1000)
svc.fit(X_scaled, y)
""",
"key_parameters": {
"C": "Regularization parameter (default=1.0)",
"max_iter": "Maximum iterations for convergence",
"dual": "Dual or primal formulation"
},
"tips": [
"Always scale your features",
"Increase max_iter if model doesn't converge",
"Try different C values using cross-validation"
]
},
"Support Vector Classification (SVC)": {
"code": """
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
svc = SVC(random_state=42)
svc.fit(X_scaled, y)
""",
"key_parameters": {
"C": "Regularization parameter (default=1.0)",
"kernel": "Kernel function used to transform the data",
"gamma": "Kernel coefficient for 'rbf', 'poly', and 'sigmoid' kernels"
},
"tips": [
"Always scale your features",
"Try different kernels and gamma values",
"Increase C if model underfits",
"Decrease C if model overfits"
]
},
"Multi-layer Perceptron (MLPClassifier)": {
"code": """
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
mlp = MLPClassifier(random_state=42)
mlp.fit(X_scaled, y)
""",
"key_parameters": {
"hidden_layer_sizes": "Number of neurons in each layer",
"activation": "Activation function used in the hidden layers",
"solver": "Optimization algorithm used to train the model",
"alpha": "L2 regularization parameter"
},
"tips": [
"Always scale your features",
"Try different activation functions",
"Increase hidden_layer_sizes if model underfits",
"Decrease hidden_layer_sizes if model overfits"
]
},
"Extra Trees Classifier": {
"code": """
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
et = ExtraTreesClassifier(random_state=42)
et.fit(X_scaled, y)
""",
"key_parameters": {
"n_estimators": "Number of trees in the forest",
"max_depth": "Maximum depth of the trees",
"min_samples_split": "Minimum number of samples required to split an internal node",
"min_samples_leaf": "Minimum number of samples required to be at a leaf node"
},
"tips": [
"Always scale your features",
"Try different max_depth values",
"Increase n_estimators if model underfits",
"Decrease n_estimators if model overfits"
]
},
"Random Forest Classifier": {
"code": """
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
rf = RandomForestClassifier(random_state=42)
rf.fit(X_scaled, y)
""",
"key_parameters": {
"n_estimators": "Number of trees in the forest",
"max_depth": "Maximum depth of the trees",
"min_samples_split": "Minimum number of samples required to split an internal node",
"min_samples_leaf": "Minimum number of samples required to be at a leaf node"
},
"tips": [
"Always scale your features",
"Try different max_depth values",
"Increase n_estimators if model underfits",
"Decrease n_estimators if model overfits"
]
},
"K-Nearest Neighbors (KNeighborsClassifier)": {
"code": """
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
knn = KNeighborsClassifier()
knn.fit(X_scaled, y)
""",
"key_parameters": {
"n_neighbors": "Number of neighbors to use",
"weights": "Weight function used in prediction",
"algorithm": "Algorithm used to compute the nearest neighbors",
"leaf_size": "Maximum number of samples in each leaf"
},
"tips": [
"Always scale your features",
"Try different n_neighbors values",
"Increase leaf_size if model underfits",
"Decrease leaf_size if model overfits"
]
},
"Ridge Classifier": {
"code": """
from sklearn.linear_model import RidgeClassifier
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
ridge = RidgeClassifier(random_state=42)
ridge.fit(X_scaled, y)
""",
"key_parameters": {
"alpha": "Regularization parameter (default=1.0)",
"solver": "Optimization algorithm used to train the model",
"max_iter": "Maximum number of iterations for the solver to converge"
},
"tips": [
"Always scale your features",
"Try different alpha values",
"Increase max_iter if model doesn't converge",
"Decrease max_iter if model overfits"
]
},
"Multinomial Naive Bayes": {
"code": """
from sklearn.naive_bayes import MultinomialNB
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
nb = MultinomialNB()
nb.fit(X_scaled, y)
""",
"key_parameters": {
"alpha": "Regularization parameter (default=1.0)",
"fit_prior": "Whether to learn class prior probabilities or not",
"class_prior": "Prior probabilities of the classes"
},
"tips": [
"Always scale your features",
"Try different alpha values",
"Increase alpha if model underfits",
"Decrease alpha if model overfits"
]
},
"AdaBoost Classifier": {
"code": """
from sklearn.ensemble import AdaBoostClassifier
from sklearn.preprocessing import StandardScaler
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Initialize and train the model
ada = AdaBoostClassifier(random_state=42)
ada.fit(X_scaled, y)
""",
"key_parameters": {
"n_estimators": "Number of trees in the forest",
"learning_rate": "Learning rate used to update the weights of the weak classifiers",
"algorithm": "Optimization algorithm used to train the model"
},
"tips": [
"Always scale your features",
"Try different learning_rate values",
"Increase n_estimators if model underfits",
"Decrease n_estimators if model overfits"
]
}
}.get(algo_name, {})
# Algorithm selector
selected_algo = st.selectbox(
"Select an algorithm to learn more:",
list(algorithms.keys())
)
# Display algorithm information
if selected_algo:
st.header(selected_algo)
# Description
st.subheader("Description")
st.write(algorithms[selected_algo]["description"])
# Two-column layout for pros and cons
col1, col2 = st.columns(2)
with col1:
st.subheader("Advantages")
for pro in algorithms[selected_algo]["pros"]:
st.markdown(f"✅ {pro}")
with col2:
st.subheader("Disadvantages")
for con in algorithms[selected_algo]["cons"]:
st.markdown(f"⚠️ {con}")
# Use cases
st.subheader("Common Use Cases")
for use_case in algorithms[selected_algo]["use_cases"]:
st.markdown(f"🎯 {use_case}")
# Add mathematical details section
st.markdown("---")
display_math_details(algorithms[selected_algo])
# Add visual separator
st.markdown("---")
# Implementation section
if "implementation" in algorithms[selected_algo]:
st.subheader("Implementation Example")
# Code example
st.code(algorithms[selected_algo]["implementation"]["code"], language="python")
# Key Parameters
st.subheader("Key Parameters")
for param, desc in algorithms[selected_algo]["implementation"]["key_parameters"].items():
st.markdown(f"**`{param}`**: {desc}")
# Implementation Tips
st.subheader("Implementation Tips")
for tip in algorithms[selected_algo]["implementation"]["tips"]:
st.markdown(f"💡 {tip}")
# Add interactive demo section
st.subheader("Interactive Demo")
if st.checkbox("Show Interactive Demo"):
st.write("Select dataset:")
dataset_choice = st.selectbox(
"Choose a sample dataset",
["Iris", "Breast Cancer", "Wine", "Digits"]
)
if st.button("Run Demo"):
try:
with st.spinner("Running demo..."):
demo_results = run_algorithm_demo(selected_algo, dataset_choice)
# Display results
st.write("Model Performance:")
st.write(f"Accuracy: {demo_results['accuracy']:.4f}")
# Show confusion matrix
st.write("Confusion Matrix:")
st.pyplot(demo_results['confusion_matrix_plot'])
# Show learning curve
st.write("Learning Curve:")
st.pyplot(demo_results['learning_curve_plot'])
except Exception as e:
st.error(f"Error running demo: {str(e)}")
# Add a references section to display in the UI
if st.checkbox("Show References"):
st.subheader("Academic References")
if "references" in algorithms[selected_algo]:
for ref in algorithms[selected_algo]["references"]:
st.markdown(f"**{ref['title']}**")
st.markdown(f"*{ref['authors']}* ({ref['year']})")
st.markdown(f"Published in: {ref['publication']}")
st.markdown(f"[Link to Publication]({ref['url']})")
st.markdown("---")
else:
st.write("No references available for this algorithm.")
def run_algorithm_demo(algorithm_name, dataset_name):
"""Run a demo of the selected algorithm on the chosen dataset."""
from sklearn.datasets import load_iris, load_breast_cancer, load_wine, load_digits
from sklearn.model_selection import train_test_split, learning_curve
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns
# Load dataset
dataset_loaders = {
"Iris": load_iris,
"Breast Cancer": load_breast_cancer,
"Wine": load_wine,
"Digits": load_digits
}
data = dataset_loaders[dataset_name]()
X, y = data.data, data.target
# Split and scale data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Initialize and train model
model = get_model_instance(algorithm_name)
model.fit(X_train_scaled, y_train)
# Get predictions and accuracy
y_pred = model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
# Create confusion matrix plot
plt.figure(figsize=(8, 6))
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='viridis')
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
cm_plot = plt.gcf()
plt.close()
# Create learning curve plot
train_sizes, train_scores, test_scores = learning_curve(
model, X_train_scaled, y_train, cv=5,
train_sizes=np.linspace(0.1, 1.0, 5)
)
plt.figure(figsize=(8, 6))
plt.plot(train_sizes, np.mean(train_scores, axis=1), label='Training score')
plt.plot(train_sizes, np.mean(test_scores, axis=1), label='Cross-validation score')
plt.xlabel('Training Examples')
plt.ylabel('Score')
plt.title('Learning Curve')
plt.legend(loc='best')
lc_plot = plt.gcf()
plt.close()
return {
'accuracy': accuracy,
'confusion_matrix_plot': cm_plot,
'learning_curve_plot': lc_plot
}
def get_model_instance(algorithm_name):
"""Return an instance of the specified algorithm."""
models = {
"Gaussian Naive Bayes (GaussianNB)": GaussianNB(),
"Linear Support Vector Classification (LinearSVC)": LinearSVC(random_state=42),
"Support Vector Classification (SVC)": SVC(random_state=42),
"Multi-layer Perceptron (MLPClassifier)": MLPClassifier(random_state=42),
"Extra Trees Classifier": ExtraTreesClassifier(random_state=42),
"Random Forest Classifier": RandomForestClassifier(random_state=42),
"K-Nearest Neighbors (KNeighborsClassifier)": KNeighborsClassifier(),
"Ridge Classifier": RidgeClassifier(random_state=42),
"Multinomial Naive Bayes": MultinomialNB(),
"AdaBoost Classifier": AdaBoostClassifier(random_state=42)
}
return models[algorithm_name]
def display_math_details(algorithm):
"""Display mathematical details for the algorithm."""
if "math_details" in algorithm:
st.subheader("Mathematical Details")
# Main formula
st.write("Main Formula:")
st.latex(algorithm["math_details"]["main_formula"])
# Component formulas
st.write("Component Formulas:")
for component in algorithm["math_details"]["component_formulas"]:
st.write(f"**{component['name']}:**")
st.latex(component["formula"])
# Explanation
st.write("**Variable Explanations:**")
st.markdown(algorithm["math_details"]["explanation"])
if __name__ == "__main__":
setup_page_config()
algorithm_info()
|