ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
README.md
(13780B)
1 # Machine Learning From Scratch
2
3 ## About
4 Python implementations of some of the fundamental Machine Learning models and algorithms from scratch.
5
6 The purpose of this project is not to produce as optimized and computationally efficient algorithms as possible
7 but rather to present the inner workings of them in a transparent and accessible way.
8
9 ## Table of Contents
10 - [Machine Learning From Scratch](#machine-learning-from-scratch)
11 * [About](#about)
12 * [Table of Contents](#table-of-contents)
13 * [Installation](#installation)
14 * [Examples](#examples)
15 + [Polynomial Regression](#polynomial-regression)
16 + [Classification With CNN](#classification-with-cnn)
17 + [Density-Based Clustering](#density-based-clustering)
18 + [Generating Handwritten Digits](#generating-handwritten-digits)
19 + [Deep Reinforcement Learning](#deep-reinforcement-learning)
20 + [Image Reconstruction With RBM](#image-reconstruction-with-rbm)
21 + [Evolutionary Evolved Neural Network](#evolutionary-evolved-neural-network)
22 + [Genetic Algorithm](#genetic-algorithm)
23 + [Association Analysis](#association-analysis)
24 * [Implementations](#implementations)
25 + [Supervised Learning](#supervised-learning)
26 + [Unsupervised Learning](#unsupervised-learning)
27 + [Reinforcement Learning](#reinforcement-learning)
28 + [Deep Learning](#deep-learning)
29 * [Contact](#contact)
30
31 ## Installation
32 $ git clone https://github.com/eriklindernoren/ML-From-Scratch
33 $ cd ML-From-Scratch
34 $ python setup.py install
35
36 ## Examples
37 ### Polynomial Regression
38 $ python mlfromscratch/examples/polynomial_regression.py
39
40 <p align="center">
41 <img src="http://eriklindernoren.se/images/p_reg.gif" width="640"\>
42 </p>
43 <p align="center">
44 Figure: Training progress of a regularized polynomial regression model fitting <br>
45 temperature data measured in Linköping, Sweden 2016.
46 </p>
47
48 ### Classification With CNN
49 $ python mlfromscratch/examples/convolutional_neural_network.py
50
51 +---------+
52 | ConvNet |
53 +---------+
54 Input Shape: (1, 8, 8)
55 +----------------------+------------+--------------+
56 | Layer Type | Parameters | Output Shape |
57 +----------------------+------------+--------------+
58 | Conv2D | 160 | (16, 8, 8) |
59 | Activation (ReLU) | 0 | (16, 8, 8) |
60 | Dropout | 0 | (16, 8, 8) |
61 | BatchNormalization | 2048 | (16, 8, 8) |
62 | Conv2D | 4640 | (32, 8, 8) |
63 | Activation (ReLU) | 0 | (32, 8, 8) |
64 | Dropout | 0 | (32, 8, 8) |
65 | BatchNormalization | 4096 | (32, 8, 8) |
66 | Flatten | 0 | (2048,) |
67 | Dense | 524544 | (256,) |
68 | Activation (ReLU) | 0 | (256,) |
69 | Dropout | 0 | (256,) |
70 | BatchNormalization | 512 | (256,) |
71 | Dense | 2570 | (10,) |
72 | Activation (Softmax) | 0 | (10,) |
73 +----------------------+------------+--------------+
74 Total Parameters: 538570
75
76 Training: 100% [------------------------------------------------------------------------] Time: 0:01:55
77 Accuracy: 0.987465181058
78
79 <p align="center">
80 <img src="http://eriklindernoren.se/images/mlfs_cnn1.png" width="640">
81 </p>
82 <p align="center">
83 Figure: Classification of the digit dataset using CNN.
84 </p>
85
86 ### Density-Based Clustering
87 $ python mlfromscratch/examples/dbscan.py
88
89 <p align="center">
90 <img src="http://eriklindernoren.se/images/mlfs_dbscan.png" width="640">
91 </p>
92 <p align="center">
93 Figure: Clustering of the moons dataset using DBSCAN.
94 </p>
95
96 ### Generating Handwritten Digits
97 $ python mlfromscratch/unsupervised_learning/generative_adversarial_network.py
98
99 +-----------+
100 | Generator |
101 +-----------+
102 Input Shape: (100,)
103 +------------------------+------------+--------------+
104 | Layer Type | Parameters | Output Shape |
105 +------------------------+------------+--------------+
106 | Dense | 25856 | (256,) |
107 | Activation (LeakyReLU) | 0 | (256,) |
108 | BatchNormalization | 512 | (256,) |
109 | Dense | 131584 | (512,) |
110 | Activation (LeakyReLU) | 0 | (512,) |
111 | BatchNormalization | 1024 | (512,) |
112 | Dense | 525312 | (1024,) |
113 | Activation (LeakyReLU) | 0 | (1024,) |
114 | BatchNormalization | 2048 | (1024,) |
115 | Dense | 803600 | (784,) |
116 | Activation (TanH) | 0 | (784,) |
117 +------------------------+------------+--------------+
118 Total Parameters: 1489936
119
120 +---------------+
121 | Discriminator |
122 +---------------+
123 Input Shape: (784,)
124 +------------------------+------------+--------------+
125 | Layer Type | Parameters | Output Shape |
126 +------------------------+------------+--------------+
127 | Dense | 401920 | (512,) |
128 | Activation (LeakyReLU) | 0 | (512,) |
129 | Dropout | 0 | (512,) |
130 | Dense | 131328 | (256,) |
131 | Activation (LeakyReLU) | 0 | (256,) |
132 | Dropout | 0 | (256,) |
133 | Dense | 514 | (2,) |
134 | Activation (Softmax) | 0 | (2,) |
135 +------------------------+------------+--------------+
136 Total Parameters: 533762
137
138
139 <p align="center">
140 <img src="http://eriklindernoren.se/images/gan_mnist5.gif" width="640">
141 </p>
142 <p align="center">
143 Figure: Training progress of a Generative Adversarial Network generating <br>
144 handwritten digits.
145 </p>
146
147 ### Deep Reinforcement Learning
148 $ python mlfromscratch/examples/deep_q_network.py
149
150 +----------------+
151 | Deep Q-Network |
152 +----------------+
153 Input Shape: (4,)
154 +-------------------+------------+--------------+
155 | Layer Type | Parameters | Output Shape |
156 +-------------------+------------+--------------+
157 | Dense | 320 | (64,) |
158 | Activation (ReLU) | 0 | (64,) |
159 | Dense | 130 | (2,) |
160 +-------------------+------------+--------------+
161 Total Parameters: 450
162
163 <p align="center">
164 <img src="http://eriklindernoren.se/images/mlfs_dql1.gif" width="640">
165 </p>
166 <p align="center">
167 Figure: Deep Q-Network solution to the CartPole-v1 environment in OpenAI gym.
168 </p>
169
170 ### Image Reconstruction With RBM
171 $ python mlfromscratch/examples/restricted_boltzmann_machine.py
172
173 <p align="center">
174 <img src="http://eriklindernoren.se/images/rbm_digits1.gif" width="640">
175 </p>
176 <p align="center">
177 Figure: Shows how the network gets better during training at reconstructing <br>
178 the digit 2 in the MNIST dataset.
179 </p>
180
181 ### Evolutionary Evolved Neural Network
182 $ python mlfromscratch/examples/neuroevolution.py
183
184 +---------------+
185 | Model Summary |
186 +---------------+
187 Input Shape: (64,)
188 +----------------------+------------+--------------+
189 | Layer Type | Parameters | Output Shape |
190 +----------------------+------------+--------------+
191 | Dense | 1040 | (16,) |
192 | Activation (ReLU) | 0 | (16,) |
193 | Dense | 170 | (10,) |
194 | Activation (Softmax) | 0 | (10,) |
195 +----------------------+------------+--------------+
196 Total Parameters: 1210
197
198 Population Size: 100
199 Generations: 3000
200 Mutation Rate: 0.01
201
202 [0 Best Individual - Fitness: 3.08301, Accuracy: 10.5%]
203 [1 Best Individual - Fitness: 3.08746, Accuracy: 12.0%]
204 ...
205 [2999 Best Individual - Fitness: 94.08513, Accuracy: 98.5%]
206 Test set accuracy: 96.7%
207
208 <p align="center">
209 <img src="http://eriklindernoren.se/images/evo_nn4.png" width="640">
210 </p>
211 <p align="center">
212 Figure: Classification of the digit dataset by a neural network which has<br>
213 been evolutionary evolved.
214 </p>
215
216 ### Genetic Algorithm
217 $ python mlfromscratch/examples/genetic_algorithm.py
218
219 +--------+
220 | GA |
221 +--------+
222 Description: Implementation of a Genetic Algorithm which aims to produce
223 the user specified target string. This implementation calculates each
224 candidate's fitness based on the alphabetical distance between the candidate
225 and the target. A candidate is selected as a parent with probabilities proportional
226 to the candidate's fitness. Reproduction is implemented as a single-point
227 crossover between pairs of parents. Mutation is done by randomly assigning
228 new characters with uniform probability.
229
230 Parameters
231 ----------
232 Target String: 'Genetic Algorithm'
233 Population Size: 100
234 Mutation Rate: 0.05
235
236 [0 Closest Candidate: 'CJqlJguPlqzvpoJmb', Fitness: 0.00]
237 [1 Closest Candidate: 'MCxZxdr nlfiwwGEk', Fitness: 0.01]
238 [2 Closest Candidate: 'MCxZxdm nlfiwwGcx', Fitness: 0.01]
239 [3 Closest Candidate: 'SmdsAklMHn kBIwKn', Fitness: 0.01]
240 [4 Closest Candidate: ' lotneaJOasWfu Z', Fitness: 0.01]
241 ...
242 [292 Closest Candidate: 'GeneticaAlgorithm', Fitness: 1.00]
243 [293 Closest Candidate: 'GeneticaAlgorithm', Fitness: 1.00]
244 [294 Answer: 'Genetic Algorithm']
245
246 ### Association Analysis
247 $ python mlfromscratch/examples/apriori.py
248 +-------------+
249 | Apriori |
250 +-------------+
251 Minimum Support: 0.25
252 Minimum Confidence: 0.8
253 Transactions:
254 [1, 2, 3, 4]
255 [1, 2, 4]
256 [1, 2]
257 [2, 3, 4]
258 [2, 3]
259 [3, 4]
260 [2, 4]
261 Frequent Itemsets:
262 [1, 2, 3, 4, [1, 2], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 4], [2, 3, 4]]
263 Rules:
264 1 -> 2 (support: 0.43, confidence: 1.0)
265 4 -> 2 (support: 0.57, confidence: 0.8)
266 [1, 4] -> 2 (support: 0.29, confidence: 1.0)
267
268
269 ## Implementations
270 ### Supervised Learning
271 - [Adaboost](mlfromscratch/supervised_learning/adaboost.py)
272 - [Bayesian Regression](mlfromscratch/supervised_learning/bayesian_regression.py)
273 - [Decision Tree](mlfromscratch/supervised_learning/decision_tree.py)
274 - [Elastic Net](mlfromscratch/supervised_learning/regression.py)
275 - [Gradient Boosting](mlfromscratch/supervised_learning/gradient_boosting.py)
276 - [K Nearest Neighbors](mlfromscratch/supervised_learning/k_nearest_neighbors.py)
277 - [Lasso Regression](mlfromscratch/supervised_learning/regression.py)
278 - [Linear Discriminant Analysis](mlfromscratch/supervised_learning/linear_discriminant_analysis.py)
279 - [Linear Regression](mlfromscratch/supervised_learning/regression.py)
280 - [Logistic Regression](mlfromscratch/supervised_learning/logistic_regression.py)
281 - [Multi-class Linear Discriminant Analysis](mlfromscratch/supervised_learning/multi_class_lda.py)
282 - [Multilayer Perceptron](mlfromscratch/supervised_learning/multilayer_perceptron.py)
283 - [Naive Bayes](mlfromscratch/supervised_learning/naive_bayes.py)
284 - [Neuroevolution](mlfromscratch/supervised_learning/neuroevolution.py)
285 - [Particle Swarm Optimization of Neural Network](mlfromscratch/supervised_learning/particle_swarm_optimization.py)
286 - [Perceptron](mlfromscratch/supervised_learning/perceptron.py)
287 - [Polynomial Regression](mlfromscratch/supervised_learning/regression.py)
288 - [Random Forest](mlfromscratch/supervised_learning/random_forest.py)
289 - [Ridge Regression](mlfromscratch/supervised_learning/regression.py)
290 - [Support Vector Machine](mlfromscratch/supervised_learning/support_vector_machine.py)
291 - [XGBoost](mlfromscratch/supervised_learning/xgboost.py)
292
293 ### Unsupervised Learning
294 - [Apriori](mlfromscratch/unsupervised_learning/apriori.py)
295 - [Autoencoder](mlfromscratch/unsupervised_learning/autoencoder.py)
296 - [DBSCAN](mlfromscratch/unsupervised_learning/dbscan.py)
297 - [FP-Growth](mlfromscratch/unsupervised_learning/fp_growth.py)
298 - [Gaussian Mixture Model](mlfromscratch/unsupervised_learning/gaussian_mixture_model.py)
299 - [Generative Adversarial Network](mlfromscratch/unsupervised_learning/generative_adversarial_network.py)
300 - [Genetic Algorithm](mlfromscratch/unsupervised_learning/genetic_algorithm.py)
301 - [K-Means](mlfromscratch/unsupervised_learning/k_means.py)
302 - [Partitioning Around Medoids](mlfromscratch/unsupervised_learning/partitioning_around_medoids.py)
303 - [Principal Component Analysis](mlfromscratch/unsupervised_learning/principal_component_analysis.py)
304 - [Restricted Boltzmann Machine](mlfromscratch/unsupervised_learning/restricted_boltzmann_machine.py)
305
306 ### Reinforcement Learning
307 - [Deep Q-Network](mlfromscratch/reinforcement_learning/deep_q_network.py)
308
309 ### Deep Learning
310 + [Neural Network](mlfromscratch/deep_learning/neural_network.py)
311 + [Layers](mlfromscratch/deep_learning/layers.py)
312 * Activation Layer
313 * Average Pooling Layer
314 * Batch Normalization Layer
315 * Constant Padding Layer
316 * Convolutional Layer
317 * Dropout Layer
318 * Flatten Layer
319 * Fully-Connected (Dense) Layer
320 * Fully-Connected RNN Layer
321 * Max Pooling Layer
322 * Reshape Layer
323 * Up Sampling Layer
324 * Zero Padding Layer
325 + Model Types
326 * [Convolutional Neural Network](mlfromscratch/examples/convolutional_neural_network.py)
327 * [Multilayer Perceptron](mlfromscratch/examples/multilayer_perceptron.py)
328 * [Recurrent Neural Network](mlfromscratch/examples/recurrent_neural_network.py)
329
330 ## Contact
331 If there's some implementation you would like to see here or if you're just feeling social,
332 feel free to [email](mailto:eriklindernoren@gmail.com) me or connect with me on [LinkedIn](https://www.linkedin.com/in/eriklindernoren/).