scratchkit 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. scratchkit-0.2.0/LICENSE +201 -0
  2. scratchkit-0.2.0/PKG-INFO +241 -0
  3. scratchkit-0.2.0/README.md +189 -0
  4. scratchkit-0.2.0/pyproject.toml +199 -0
  5. scratchkit-0.2.0/setup.cfg +4 -0
  6. scratchkit-0.2.0/src/mlscratch/__init__.py +56 -0
  7. scratchkit-0.2.0/src/mlscratch/__main__.py +118 -0
  8. scratchkit-0.2.0/src/mlscratch/bayesian/__init__.py +53 -0
  9. scratchkit-0.2.0/src/mlscratch/bayesian/bayesian_linear_regression.py +171 -0
  10. scratchkit-0.2.0/src/mlscratch/bayesian/bayesian_network.py +248 -0
  11. scratchkit-0.2.0/src/mlscratch/bayesian/bayesian_nn.py +315 -0
  12. scratchkit-0.2.0/src/mlscratch/bayesian/gaussian_process.py +207 -0
  13. scratchkit-0.2.0/src/mlscratch/bayesian/hmm.py +277 -0
  14. scratchkit-0.2.0/src/mlscratch/bayesian/init.py +52 -0
  15. scratchkit-0.2.0/src/mlscratch/bayesian/kalman_filter.py +182 -0
  16. scratchkit-0.2.0/src/mlscratch/bayesian/naive_bayes.py +209 -0
  17. scratchkit-0.2.0/src/mlscratch/metrics/__init__.py +59 -0
  18. scratchkit-0.2.0/src/mlscratch/metrics/classification.py +365 -0
  19. scratchkit-0.2.0/src/mlscratch/metrics/regression.py +79 -0
  20. scratchkit-0.2.0/src/mlscratch/neural/__init__.py +121 -0
  21. scratchkit-0.2.0/src/mlscratch/neural/attention.py +420 -0
  22. scratchkit-0.2.0/src/mlscratch/neural/autoencoder.py +543 -0
  23. scratchkit-0.2.0/src/mlscratch/neural/boltzmann.py +231 -0
  24. scratchkit-0.2.0/src/mlscratch/neural/cnn.py +593 -0
  25. scratchkit-0.2.0/src/mlscratch/neural/cvnn.py +322 -0
  26. scratchkit-0.2.0/src/mlscratch/neural/gan.py +364 -0
  27. scratchkit-0.2.0/src/mlscratch/neural/hopfield.py +193 -0
  28. scratchkit-0.2.0/src/mlscratch/neural/perceptron.py +398 -0
  29. scratchkit-0.2.0/src/mlscratch/neural/rbf_network.py +230 -0
  30. scratchkit-0.2.0/src/mlscratch/neural/recurrent.py +569 -0
  31. scratchkit-0.2.0/src/mlscratch/preprocessing/__init__.py +38 -0
  32. scratchkit-0.2.0/src/mlscratch/preprocessing/encoders.py +140 -0
  33. scratchkit-0.2.0/src/mlscratch/preprocessing/model_selection.py +119 -0
  34. scratchkit-0.2.0/src/mlscratch/preprocessing/polynomial.py +105 -0
  35. scratchkit-0.2.0/src/mlscratch/preprocessing/scalers.py +220 -0
  36. scratchkit-0.2.0/src/mlscratch/py.typed +0 -0
  37. scratchkit-0.2.0/src/mlscratch/reinforcement/__init__.py +59 -0
  38. scratchkit-0.2.0/src/mlscratch/reinforcement/ddpg.py +363 -0
  39. scratchkit-0.2.0/src/mlscratch/reinforcement/dqn.py +319 -0
  40. scratchkit-0.2.0/src/mlscratch/reinforcement/ppo.py +452 -0
  41. scratchkit-0.2.0/src/mlscratch/reinforcement/q_learning.py +352 -0
  42. scratchkit-0.2.0/src/mlscratch/reinforcement/sac.py +382 -0
  43. scratchkit-0.2.0/src/mlscratch/reinforcement/utils.py +594 -0
  44. scratchkit-0.2.0/src/mlscratch/supervised/__init__.py +76 -0
  45. scratchkit-0.2.0/src/mlscratch/supervised/_validation.py +50 -0
  46. scratchkit-0.2.0/src/mlscratch/supervised/adaboost.py +255 -0
  47. scratchkit-0.2.0/src/mlscratch/supervised/decision_tree.py +495 -0
  48. scratchkit-0.2.0/src/mlscratch/supervised/gradient_boosting.py +354 -0
  49. scratchkit-0.2.0/src/mlscratch/supervised/knn.py +234 -0
  50. scratchkit-0.2.0/src/mlscratch/supervised/lasso_regression.py +125 -0
  51. scratchkit-0.2.0/src/mlscratch/supervised/linear_models.py +459 -0
  52. scratchkit-0.2.0/src/mlscratch/supervised/linear_regression.py +197 -0
  53. scratchkit-0.2.0/src/mlscratch/supervised/logistic_regression.py +119 -0
  54. scratchkit-0.2.0/src/mlscratch/supervised/naive_bayes.py +113 -0
  55. scratchkit-0.2.0/src/mlscratch/supervised/random_forest.py +321 -0
  56. scratchkit-0.2.0/src/mlscratch/supervised/ridge_regression.py +93 -0
  57. scratchkit-0.2.0/src/mlscratch/supervised/svm.py +356 -0
  58. scratchkit-0.2.0/src/mlscratch/unsupervised/__init__.py +39 -0
  59. scratchkit-0.2.0/src/mlscratch/unsupervised/apriori.py +178 -0
  60. scratchkit-0.2.0/src/mlscratch/unsupervised/dbscan.py +141 -0
  61. scratchkit-0.2.0/src/mlscratch/unsupervised/gmm.py +204 -0
  62. scratchkit-0.2.0/src/mlscratch/unsupervised/hierarchical_clustering.py +137 -0
  63. scratchkit-0.2.0/src/mlscratch/unsupervised/ica.py +167 -0
  64. scratchkit-0.2.0/src/mlscratch/unsupervised/kmeans.py +135 -0
  65. scratchkit-0.2.0/src/mlscratch/unsupervised/kmedoids.py +133 -0
  66. scratchkit-0.2.0/src/mlscratch/unsupervised/pca.py +103 -0
  67. scratchkit-0.2.0/src/mlscratch/unsupervised/tsne.py +200 -0
  68. scratchkit-0.2.0/src/scratchkit.egg-info/PKG-INFO +241 -0
  69. scratchkit-0.2.0/src/scratchkit.egg-info/SOURCES.txt +71 -0
  70. scratchkit-0.2.0/src/scratchkit.egg-info/dependency_links.txt +1 -0
  71. scratchkit-0.2.0/src/scratchkit.egg-info/entry_points.txt +2 -0
  72. scratchkit-0.2.0/src/scratchkit.egg-info/requires.txt +26 -0
  73. scratchkit-0.2.0/src/scratchkit.egg-info/top_level.txt +1 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,241 @@
1
+ Metadata-Version: 2.4
2
+ Name: scratchkit
3
+ Version: 0.2.0
4
+ Summary: Pure-NumPy from-scratch implementations of ML/AI/RL/Bayesian algorithms — no PyTorch, no TensorFlow, no scikit-learn. (import name: mlscratch)
5
+ Author-email: Mattral <mattral@example.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/Mattral/ML-AI-Algorithms-from-scratch
8
+ Project-URL: Documentation, https://mattral.github.io/ML-AI-Algorithms-from-scratch/
9
+ Project-URL: Repository, https://github.com/Mattral/ML-AI-Algorithms-from-scratch
10
+ Project-URL: Issues, https://github.com/Mattral/ML-AI-Algorithms-from-scratch/issues
11
+ Project-URL: Changelog, https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/CHANGELOG.md
12
+ Keywords: machine-learning,deep-learning,reinforcement-learning,bayesian,numpy,from-scratch,education,algorithms,neural-network,unsupervised,supervised
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Intended Audience :: Education
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: License :: OSI Approved :: Apache Software License
18
+ Classifier: Operating System :: OS Independent
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
24
+ Classifier: Topic :: Education
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.10
27
+ Description-Content-Type: text/markdown
28
+ License-File: LICENSE
29
+ Requires-Dist: numpy>=1.23
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=7; extra == "dev"
32
+ Requires-Dist: pytest-cov>=4; extra == "dev"
33
+ Requires-Dist: pytest-benchmark>=4; extra == "dev"
34
+ Requires-Dist: hypothesis>=6; extra == "dev"
35
+ Requires-Dist: scikit-learn>=1.3; extra == "dev"
36
+ Requires-Dist: ruff>=0.4; extra == "dev"
37
+ Requires-Dist: black>=24; extra == "dev"
38
+ Requires-Dist: mypy>=1.8; extra == "dev"
39
+ Requires-Dist: build>=1; extra == "dev"
40
+ Requires-Dist: twine>=5; extra == "dev"
41
+ Provides-Extra: docs
42
+ Requires-Dist: mkdocs>=1.6; extra == "docs"
43
+ Requires-Dist: mkdocs-material>=9.5; extra == "docs"
44
+ Requires-Dist: mkdocstrings[python]>=0.25; extra == "docs"
45
+ Provides-Extra: notebooks
46
+ Requires-Dist: jupyter>=1.0; extra == "notebooks"
47
+ Requires-Dist: matplotlib>=3.7; extra == "notebooks"
48
+ Requires-Dist: pandas>=2.0; extra == "notebooks"
49
+ Provides-Extra: all
50
+ Requires-Dist: scratchkit[dev,docs,notebooks]; extra == "all"
51
+ Dynamic: license-file
52
+
53
+ # ML-AI-Algorithms-from-scratch
54
+
55
+ **60+ ML/AI/DL/RL/Bayesian algorithms implemented from scratch in NumPy — plus `mlscratch`, a pip-installable package (`pip install scratchkit`) with a consistent, scikit-learn-style API and 1,100+ tests.**
56
+
57
+ [![CI](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/actions/workflows/ci.yml/badge.svg)](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/actions)
58
+ [![PyPI](https://img.shields.io/pypi/v/scratchkit.svg)](https://pypi.org/project/scratchkit/)
59
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/LICENSE)
60
+ [![Python 3.10+](https://img.shields.io/badge/Python-3.10+-blue.svg)](https://python.org)
61
+ [![Stars](https://img.shields.io/github/stars/Mattral/ML-AI-Algorithms-from-scratch?style=social)](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/stargazers)
62
+
63
+ > **What's here:** readable, standalone implementations of algorithms you already know by name, written to show the math in code, not to be fast.
64
+ >
65
+ > **What's new:** `src/mlscratch/` — a pip-installable package with `fit()`/`predict()`/`transform()` APIs, full type hints, and a test suite that cross-checks correctness against scikit-learn wherever a reference implementation exists.
66
+
67
+ ---
68
+
69
+ ## What makes this different from the dozens of similar repos
70
+
71
+ There are many "ML from scratch" repos on GitHub. The honest differentiators here:
72
+
73
+ - **Bayesian methods are first-class.** Most from-scratch repos stop at supervised learning + neural nets. This one includes Bayesian Neural Networks, Gaussian Processes, Hidden Markov Models, Bayesian Networks, and Kalman Filters — algorithms most tutorials skip because they're harder to implement correctly.
74
+ - **RL goes beyond DQN.** DDPG, TD3, SAC, and PPO are included alongside tabular Q-Learning and DQN — non-trivial to implement correctly from scratch, and rare to see done well in a single repo.
75
+ - **The `src/mlscratch` package is real, not a wrapper.** Every estimator is implemented in pure NumPy — no calling out to scikit-learn at runtime. scikit-learn only appears in the *test suite*, as a correctness oracle, never as a dependency of the library itself.
76
+ - **Kernel SVM via real SMO, gradient boosting with proper Newton-step leaves, multiclass-native AdaBoost (SAMME.R)** — the ensemble/kernel methods aren't toy simplifications; several are verified to match scikit-learn's output to floating-point tolerance on real benchmarks.
77
+
78
+ ---
79
+
80
+ ## Quick start
81
+
82
+ ### Browse the standalone scripts (no install needed)
83
+
84
+ ```bash
85
+ git clone https://github.com/Mattral/ML-AI-Algorithms-from-scratch
86
+ cd ML-AI-Algorithms-from-scratch
87
+
88
+ pip install numpy matplotlib scikit-learn # only deps, for the standalone scripts
89
+
90
+ python "Supervised/LinearRegression/linear_regression.py"
91
+ python "Neural Networks/Transformer/transformer.py"
92
+ python "Reinforcement/PPO/ppo.py"
93
+ ```
94
+
95
+ ### Use the package
96
+
97
+ ```bash
98
+ pip install scratchkit # from PyPI — the import name is still `mlscratch`
99
+ ```
100
+
101
+ ```bash
102
+ # — or, for local development —
103
+ pip install -e . # installs src/mlscratch in editable mode
104
+ # pip install -e ".[dev]" # + pytest, ruff, black, mypy, for development
105
+
106
+ pytest tests/ -v # run the test suite
107
+ python -m mlscratch info # package + sub-package summary
108
+ python -m mlscratch list supervised
109
+ ```
110
+
111
+ ```python
112
+ from mlscratch.supervised import RandomForestClassifier
113
+ from mlscratch.preprocessing import StandardScaler, train_test_split
114
+ from mlscratch.metrics import classification_report
115
+
116
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, stratify=y)
117
+
118
+ scaler = StandardScaler().fit(X_train)
119
+ model = RandomForestClassifier(n_estimators=200, max_depth=6, oob_score=True)
120
+ model.fit(scaler.transform(X_train), y_train)
121
+
122
+ print(f"OOB score: {model.oob_score_:.3f}")
123
+ print(classification_report(y_test, model.predict(scaler.transform(X_test))))
124
+ ```
125
+
126
+ See [`examples/`](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/tree/main/examples) for six runnable end-to-end scripts covering decision trees, random forests, kernel SVMs, gradient boosting, AdaBoost, and a full no-sklearn classification + regression pipeline.
127
+
128
+ ---
129
+
130
+ ## What's implemented
131
+
132
+ ### `mlscratch` package (`src/mlscratch/`)
133
+
134
+ | Sub-package | Contents | Tests |
135
+ |---|---|---|
136
+ | `mlscratch.supervised` | Linear/Ridge/Lasso/ElasticNet/Logistic regression, KNN, **DecisionTree** (classifier + regressor), **RandomForest** (bagging + OOB scoring), kernel **SVC** (SMO; linear/poly/rbf/sigmoid, one-vs-rest multiclass), **GradientBoosting** (classifier + regressor, squared/absolute-error loss), **AdaBoost** (SAMME / SAMME.R, multiclass-native) | 162 |
137
+ | `mlscratch.unsupervised` | K-Means++, K-Medoids, DBSCAN, Agglomerative Clustering, PCA, t-SNE, FastICA, Gaussian Mixture Model (EM), Apriori | 120 |
138
+ | `mlscratch.bayesian` | Naive Bayes (Gaussian/Multinomial/Bernoulli), Bayesian Linear Regression, Bayesian Network, Bayesian Neural Network (mean-field VI), Gaussian Process Regression, Hidden Markov Model, Kalman Filter | 171 |
139
+ | `mlscratch.reinforcement` | Q-Learning, Double Q-Learning, DQN (Double + Dueling + PER), DDPG, TD3, PPO (GAE-λ), SAC, plus shared `GridWorld`/`ReplayBuffer`/`PrioritizedReplayBuffer` utilities | 218 |
140
+ | `mlscratch.neural` | Single/Multi-Layer Perceptron, Autoencoder (vanilla/denoising/variational), RNN/LSTM/Encoder-Decoder, a small CNN (Conv2D/Pool/BatchNorm), Attention + Transformer encoder, GAN, Hopfield Network, Restricted Boltzmann Machine, RBF Network, Complex-Valued NN | 372 |
141
+ | `mlscratch.metrics` | accuracy/precision/recall/F1, confusion matrix, `classification_report`, ROC/AUC, log loss, MSE/RMSE/MAE/MAPE, R², explained variance — every metric checked against scikit-learn | 48 |
142
+ | `mlscratch.preprocessing` | StandardScaler, MinMaxScaler, RobustScaler, Normalizer, LabelEncoder, OneHotEncoder, PolynomialFeatures, `train_test_split` (with stratification) | 62 |
143
+
144
+ **1,153 tests total.** A handful (~18) fail under the newest NumPy/SciPy releases in this environment due to upstream API drift in unrelated modules (Bayesian networks, reinforcement learning buffers, ICA) — tracked as known issues, not part of this release's scope.
145
+
146
+ ### Standalone scripts (original, by category)
147
+
148
+ These are the original from-scratch scripts the package above was distilled from — browse them like a reference, run them directly, no install required.
149
+
150
+ - **`Supervised/`** — Linear/Ridge/Lasso Regression, Logistic Regression, k-NN, Decision Trees, Random Forest, Naive Bayes, SVM
151
+ - **`Unsupervised/`** — K-Means++, K-Medoids, DBSCAN, Hierarchical Clustering, PCA, t-SNE, ICA, Gaussian Mixture Model, EM, Self-Organising Map, Apriori
152
+ - **`Neural Networks/`** — Single/Multi-Layer Perceptron, Simple RNN, LSTM, Simple CNN, Encoder-Decoder, Self-Attention, Transformer, Autoencoder, GAN, Boltzmann Machine, Hopfield Network, RBF Networks
153
+ - **`Reinforcement/`** — Q-Learning, DQN, DDPG, PPO, SAC
154
+ - **`Bayesian Learning/`** — Bayesian Inference, Bayesian Linear Regression, Bayesian Network, Bayesian Neural Networks, Gibbs Sampling, Metropolis-Hastings, Variational Inference
155
+
156
+ ---
157
+
158
+ ## Design philosophy
159
+
160
+ Every implementation applies the same principles:
161
+
162
+ - Explicit loops over vectorised one-liners when clarity improves
163
+ - Model logic, loss computation, and parameter updates in separate functions
164
+ - The package layer (`src/mlscratch`) calls **only** NumPy at runtime — scikit-learn appears solely in the test suite, as a correctness oracle
165
+ - Short files: most standalone scripts are 100–300 lines; package modules favor one well-documented class per concern
166
+
167
+ **This trades raw performance for readability and correctness-by-inspection. That's intentional.**
168
+
169
+ If you're looking for production-speed implementations, use scikit-learn, PyTorch, or JAX. If you want to read the math in code form — or verify it against a reference implementation in the test suite — this is the repo.
170
+
171
+ ---
172
+
173
+ ## Recommended learning path
174
+
175
+ If you're working through this systematically:
176
+
177
+ 1. Start with `Supervised/LinearRegression` (or `mlscratch.supervised.LinearRegression`) — the simplest possible end-to-end example
178
+ 2. Move to `LogisticRegression` — same structure, adds sigmoid + cross-entropy
179
+ 3. Then `DecisionTreeClassifier` → `RandomForestClassifier` → `GradientBoostingClassifier`/`AdaBoostClassifier` — the tree-ensemble family, building on a shared CART implementation
180
+ 4. Then `Neural Networks/SingleLayerPerceptron` → `MultiLayerPerceptron` — backprop from first principles
181
+ 5. Then any of: Unsupervised (PCA → GMM → t-SNE), Reinforcement (Q-Learning → DQN → PPO/SAC), or Bayesian (Naive Bayes → Bayesian Linear Regression → Variational Inference)
182
+
183
+ Each folder/module is reasonably self-contained — jump to any algorithm without reading the others first.
184
+
185
+ ---
186
+
187
+ ## Repository layout
188
+
189
+ ```
190
+ ML-AI-Algorithms-from-scratch/
191
+
192
+ ├── Supervised/ Standalone scripts: LinearRegression, SVM, etc.
193
+ ├── Unsupervised/ Standalone scripts: KMeans++, DBSCAN, t-SNE, etc.
194
+ ├── Neural Networks/ Standalone scripts: MLP, LSTM, Transformer, GAN, etc.
195
+ ├── Reinforcement/ Standalone scripts: DQN, DDPG, PPO, SAC, etc.
196
+ ├── Bayesian Learning/ Standalone scripts: BNN, VI, MCMC, etc.
197
+
198
+ ├── src/mlscratch/ Pip-installable package
199
+ │ ├── supervised/ Linear models, KNN, trees, ensembles, kernel SVM
200
+ │ ├── unsupervised/ Clustering, dimensionality reduction, association rules
201
+ │ ├── bayesian/ Naive Bayes, BLR, BNN, GP, HMM, Bayesian Networks, Kalman
202
+ │ ├── reinforcement/ Q-Learning, DQN, DDPG, TD3, PPO, SAC
203
+ │ ├── neural/ Perceptrons, autoencoders, RNN/CNN, attention, GAN, ...
204
+ │ ├── metrics/ Classification & regression evaluation metrics
205
+ │ └── preprocessing/ Scalers, encoders, polynomial features, train_test_split
206
+
207
+ ├── examples/ Runnable end-to-end scripts (no sklearn at runtime)
208
+ ├── tests/ 1,153 tests, mirroring the src/mlscratch layout
209
+ ├── docs/ Roadmap (MkDocs site planned, see roadmap.md)
210
+ ├── pyproject.toml Package metadata + deps
211
+ ├── CHANGELOG.md Keep-a-Changelog formatted release history
212
+ ├── roadmap.md P0 / P1 / P2 backlog
213
+ ├── .github/workflows/ CI: lint → test matrix → build → PyPI release
214
+ └── README.md
215
+ ```
216
+
217
+ ---
218
+
219
+ ## Contributing
220
+
221
+ The most useful contributions right now:
222
+
223
+ - **Add a standalone script** for an algorithm not yet covered (check the folder first)
224
+ - **Port a standalone script** into `src/mlscratch` with a matching test file in `tests/`
225
+ - **Fix a numerical issue** — some implementations have known edge cases under newer NumPy/SciPy releases (see the known-issues note above; open an issue or PR)
226
+
227
+ Standard flow: fork → branch → PR. CI runs `ruff`, `black --check`, and the full `pytest` suite on every PR. See [`CONTRIBUTING.md`](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/CONTRIBUTING.md) for the full guide, and [`roadmap.md`](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/roadmap.md) for what's planned next.
228
+
229
+ ---
230
+
231
+ ## Honest scope
232
+
233
+ The standalone scripts under `Supervised/`, `Neural Networks/`, etc. are a **learning reference**, not a performance library: some use toy datasets, a few have hardcoded hyperparameters to keep the code short, and none are tuned for speed at scale.
234
+
235
+ The `src/mlscratch` package is more rigorous (typed, tested, cross-checked against scikit-learn) but is still pure-Python/NumPy — it will not outrun scikit-learn or XGBoost on large datasets, and that was never the goal. The public API is stabilising but may still change between minor versions before a 1.0 release; pin a version if you're building on top of it.
236
+
237
+ ---
238
+
239
+ ## License
240
+
241
+ Apache 2.0 — see [LICENSE](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/LICENSE).
@@ -0,0 +1,189 @@
1
+ # ML-AI-Algorithms-from-scratch
2
+
3
+ **60+ ML/AI/DL/RL/Bayesian algorithms implemented from scratch in NumPy — plus `mlscratch`, a pip-installable package (`pip install scratchkit`) with a consistent, scikit-learn-style API and 1,100+ tests.**
4
+
5
+ [![CI](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/actions/workflows/ci.yml/badge.svg)](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/actions)
6
+ [![PyPI](https://img.shields.io/pypi/v/scratchkit.svg)](https://pypi.org/project/scratchkit/)
7
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/LICENSE)
8
+ [![Python 3.10+](https://img.shields.io/badge/Python-3.10+-blue.svg)](https://python.org)
9
+ [![Stars](https://img.shields.io/github/stars/Mattral/ML-AI-Algorithms-from-scratch?style=social)](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/stargazers)
10
+
11
+ > **What's here:** readable, standalone implementations of algorithms you already know by name, written to show the math in code, not to be fast.
12
+ >
13
+ > **What's new:** `src/mlscratch/` — a pip-installable package with `fit()`/`predict()`/`transform()` APIs, full type hints, and a test suite that cross-checks correctness against scikit-learn wherever a reference implementation exists.
14
+
15
+ ---
16
+
17
+ ## What makes this different from the dozens of similar repos
18
+
19
+ There are many "ML from scratch" repos on GitHub. The honest differentiators here:
20
+
21
+ - **Bayesian methods are first-class.** Most from-scratch repos stop at supervised learning + neural nets. This one includes Bayesian Neural Networks, Gaussian Processes, Hidden Markov Models, Bayesian Networks, and Kalman Filters — algorithms most tutorials skip because they're harder to implement correctly.
22
+ - **RL goes beyond DQN.** DDPG, TD3, SAC, and PPO are included alongside tabular Q-Learning and DQN — non-trivial to implement correctly from scratch, and rare to see done well in a single repo.
23
+ - **The `src/mlscratch` package is real, not a wrapper.** Every estimator is implemented in pure NumPy — no calling out to scikit-learn at runtime. scikit-learn only appears in the *test suite*, as a correctness oracle, never as a dependency of the library itself.
24
+ - **Kernel SVM via real SMO, gradient boosting with proper Newton-step leaves, multiclass-native AdaBoost (SAMME.R)** — the ensemble/kernel methods aren't toy simplifications; several are verified to match scikit-learn's output to floating-point tolerance on real benchmarks.
25
+
26
+ ---
27
+
28
+ ## Quick start
29
+
30
+ ### Browse the standalone scripts (no install needed)
31
+
32
+ ```bash
33
+ git clone https://github.com/Mattral/ML-AI-Algorithms-from-scratch
34
+ cd ML-AI-Algorithms-from-scratch
35
+
36
+ pip install numpy matplotlib scikit-learn # only deps, for the standalone scripts
37
+
38
+ python "Supervised/LinearRegression/linear_regression.py"
39
+ python "Neural Networks/Transformer/transformer.py"
40
+ python "Reinforcement/PPO/ppo.py"
41
+ ```
42
+
43
+ ### Use the package
44
+
45
+ ```bash
46
+ pip install scratchkit # from PyPI — the import name is still `mlscratch`
47
+ ```
48
+
49
+ ```bash
50
+ # — or, for local development —
51
+ pip install -e . # installs src/mlscratch in editable mode
52
+ # pip install -e ".[dev]" # + pytest, ruff, black, mypy, for development
53
+
54
+ pytest tests/ -v # run the test suite
55
+ python -m mlscratch info # package + sub-package summary
56
+ python -m mlscratch list supervised
57
+ ```
58
+
59
+ ```python
60
+ from mlscratch.supervised import RandomForestClassifier
61
+ from mlscratch.preprocessing import StandardScaler, train_test_split
62
+ from mlscratch.metrics import classification_report
63
+
64
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, stratify=y)
65
+
66
+ scaler = StandardScaler().fit(X_train)
67
+ model = RandomForestClassifier(n_estimators=200, max_depth=6, oob_score=True)
68
+ model.fit(scaler.transform(X_train), y_train)
69
+
70
+ print(f"OOB score: {model.oob_score_:.3f}")
71
+ print(classification_report(y_test, model.predict(scaler.transform(X_test))))
72
+ ```
73
+
74
+ See [`examples/`](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/tree/main/examples) for six runnable end-to-end scripts covering decision trees, random forests, kernel SVMs, gradient boosting, AdaBoost, and a full no-sklearn classification + regression pipeline.
75
+
76
+ ---
77
+
78
+ ## What's implemented
79
+
80
+ ### `mlscratch` package (`src/mlscratch/`)
81
+
82
+ | Sub-package | Contents | Tests |
83
+ |---|---|---|
84
+ | `mlscratch.supervised` | Linear/Ridge/Lasso/ElasticNet/Logistic regression, KNN, **DecisionTree** (classifier + regressor), **RandomForest** (bagging + OOB scoring), kernel **SVC** (SMO; linear/poly/rbf/sigmoid, one-vs-rest multiclass), **GradientBoosting** (classifier + regressor, squared/absolute-error loss), **AdaBoost** (SAMME / SAMME.R, multiclass-native) | 162 |
85
+ | `mlscratch.unsupervised` | K-Means++, K-Medoids, DBSCAN, Agglomerative Clustering, PCA, t-SNE, FastICA, Gaussian Mixture Model (EM), Apriori | 120 |
86
+ | `mlscratch.bayesian` | Naive Bayes (Gaussian/Multinomial/Bernoulli), Bayesian Linear Regression, Bayesian Network, Bayesian Neural Network (mean-field VI), Gaussian Process Regression, Hidden Markov Model, Kalman Filter | 171 |
87
+ | `mlscratch.reinforcement` | Q-Learning, Double Q-Learning, DQN (Double + Dueling + PER), DDPG, TD3, PPO (GAE-λ), SAC, plus shared `GridWorld`/`ReplayBuffer`/`PrioritizedReplayBuffer` utilities | 218 |
88
+ | `mlscratch.neural` | Single/Multi-Layer Perceptron, Autoencoder (vanilla/denoising/variational), RNN/LSTM/Encoder-Decoder, a small CNN (Conv2D/Pool/BatchNorm), Attention + Transformer encoder, GAN, Hopfield Network, Restricted Boltzmann Machine, RBF Network, Complex-Valued NN | 372 |
89
+ | `mlscratch.metrics` | accuracy/precision/recall/F1, confusion matrix, `classification_report`, ROC/AUC, log loss, MSE/RMSE/MAE/MAPE, R², explained variance — every metric checked against scikit-learn | 48 |
90
+ | `mlscratch.preprocessing` | StandardScaler, MinMaxScaler, RobustScaler, Normalizer, LabelEncoder, OneHotEncoder, PolynomialFeatures, `train_test_split` (with stratification) | 62 |
91
+
92
+ **1,153 tests total.** A handful (~18) fail under the newest NumPy/SciPy releases in this environment due to upstream API drift in unrelated modules (Bayesian networks, reinforcement learning buffers, ICA) — tracked as known issues, not part of this release's scope.
93
+
94
+ ### Standalone scripts (original, by category)
95
+
96
+ These are the original from-scratch scripts the package above was distilled from — browse them like a reference, run them directly, no install required.
97
+
98
+ - **`Supervised/`** — Linear/Ridge/Lasso Regression, Logistic Regression, k-NN, Decision Trees, Random Forest, Naive Bayes, SVM
99
+ - **`Unsupervised/`** — K-Means++, K-Medoids, DBSCAN, Hierarchical Clustering, PCA, t-SNE, ICA, Gaussian Mixture Model, EM, Self-Organising Map, Apriori
100
+ - **`Neural Networks/`** — Single/Multi-Layer Perceptron, Simple RNN, LSTM, Simple CNN, Encoder-Decoder, Self-Attention, Transformer, Autoencoder, GAN, Boltzmann Machine, Hopfield Network, RBF Networks
101
+ - **`Reinforcement/`** — Q-Learning, DQN, DDPG, PPO, SAC
102
+ - **`Bayesian Learning/`** — Bayesian Inference, Bayesian Linear Regression, Bayesian Network, Bayesian Neural Networks, Gibbs Sampling, Metropolis-Hastings, Variational Inference
103
+
104
+ ---
105
+
106
+ ## Design philosophy
107
+
108
+ Every implementation applies the same principles:
109
+
110
+ - Explicit loops over vectorised one-liners when clarity improves
111
+ - Model logic, loss computation, and parameter updates in separate functions
112
+ - The package layer (`src/mlscratch`) calls **only** NumPy at runtime — scikit-learn appears solely in the test suite, as a correctness oracle
113
+ - Short files: most standalone scripts are 100–300 lines; package modules favor one well-documented class per concern
114
+
115
+ **This trades raw performance for readability and correctness-by-inspection. That's intentional.**
116
+
117
+ If you're looking for production-speed implementations, use scikit-learn, PyTorch, or JAX. If you want to read the math in code form — or verify it against a reference implementation in the test suite — this is the repo.
118
+
119
+ ---
120
+
121
+ ## Recommended learning path
122
+
123
+ If you're working through this systematically:
124
+
125
+ 1. Start with `Supervised/LinearRegression` (or `mlscratch.supervised.LinearRegression`) — the simplest possible end-to-end example
126
+ 2. Move to `LogisticRegression` — same structure, adds sigmoid + cross-entropy
127
+ 3. Then `DecisionTreeClassifier` → `RandomForestClassifier` → `GradientBoostingClassifier`/`AdaBoostClassifier` — the tree-ensemble family, building on a shared CART implementation
128
+ 4. Then `Neural Networks/SingleLayerPerceptron` → `MultiLayerPerceptron` — backprop from first principles
129
+ 5. Then any of: Unsupervised (PCA → GMM → t-SNE), Reinforcement (Q-Learning → DQN → PPO/SAC), or Bayesian (Naive Bayes → Bayesian Linear Regression → Variational Inference)
130
+
131
+ Each folder/module is reasonably self-contained — jump to any algorithm without reading the others first.
132
+
133
+ ---
134
+
135
+ ## Repository layout
136
+
137
+ ```
138
+ ML-AI-Algorithms-from-scratch/
139
+
140
+ ├── Supervised/ Standalone scripts: LinearRegression, SVM, etc.
141
+ ├── Unsupervised/ Standalone scripts: KMeans++, DBSCAN, t-SNE, etc.
142
+ ├── Neural Networks/ Standalone scripts: MLP, LSTM, Transformer, GAN, etc.
143
+ ├── Reinforcement/ Standalone scripts: DQN, DDPG, PPO, SAC, etc.
144
+ ├── Bayesian Learning/ Standalone scripts: BNN, VI, MCMC, etc.
145
+
146
+ ├── src/mlscratch/ Pip-installable package
147
+ │ ├── supervised/ Linear models, KNN, trees, ensembles, kernel SVM
148
+ │ ├── unsupervised/ Clustering, dimensionality reduction, association rules
149
+ │ ├── bayesian/ Naive Bayes, BLR, BNN, GP, HMM, Bayesian Networks, Kalman
150
+ │ ├── reinforcement/ Q-Learning, DQN, DDPG, TD3, PPO, SAC
151
+ │ ├── neural/ Perceptrons, autoencoders, RNN/CNN, attention, GAN, ...
152
+ │ ├── metrics/ Classification & regression evaluation metrics
153
+ │ └── preprocessing/ Scalers, encoders, polynomial features, train_test_split
154
+
155
+ ├── examples/ Runnable end-to-end scripts (no sklearn at runtime)
156
+ ├── tests/ 1,153 tests, mirroring the src/mlscratch layout
157
+ ├── docs/ Roadmap (MkDocs site planned, see roadmap.md)
158
+ ├── pyproject.toml Package metadata + deps
159
+ ├── CHANGELOG.md Keep-a-Changelog formatted release history
160
+ ├── roadmap.md P0 / P1 / P2 backlog
161
+ ├── .github/workflows/ CI: lint → test matrix → build → PyPI release
162
+ └── README.md
163
+ ```
164
+
165
+ ---
166
+
167
+ ## Contributing
168
+
169
+ The most useful contributions right now:
170
+
171
+ - **Add a standalone script** for an algorithm not yet covered (check the folder first)
172
+ - **Port a standalone script** into `src/mlscratch` with a matching test file in `tests/`
173
+ - **Fix a numerical issue** — some implementations have known edge cases under newer NumPy/SciPy releases (see the known-issues note above; open an issue or PR)
174
+
175
+ Standard flow: fork → branch → PR. CI runs `ruff`, `black --check`, and the full `pytest` suite on every PR. See [`CONTRIBUTING.md`](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/CONTRIBUTING.md) for the full guide, and [`roadmap.md`](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/roadmap.md) for what's planned next.
176
+
177
+ ---
178
+
179
+ ## Honest scope
180
+
181
+ The standalone scripts under `Supervised/`, `Neural Networks/`, etc. are a **learning reference**, not a performance library: some use toy datasets, a few have hardcoded hyperparameters to keep the code short, and none are tuned for speed at scale.
182
+
183
+ The `src/mlscratch` package is more rigorous (typed, tested, cross-checked against scikit-learn) but is still pure-Python/NumPy — it will not outrun scikit-learn or XGBoost on large datasets, and that was never the goal. The public API is stabilising but may still change between minor versions before a 1.0 release; pin a version if you're building on top of it.
184
+
185
+ ---
186
+
187
+ ## License
188
+
189
+ Apache 2.0 — see [LICENSE](https://github.com/Mattral/ML-AI-Algorithms-from-scratch/blob/main/LICENSE).