ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
adaboost.py
(1139B)
1 from __future__ import division, print_function
2 import numpy as np
3 from sklearn import datasets
4
5 # Import helper functions
6 from mlfromscratch.supervised_learning import Adaboost
7 from mlfromscratch.utils.data_manipulation import train_test_split
8 from mlfromscratch.utils.data_operation import accuracy_score
9 from mlfromscratch.utils import Plot
10
11 def main():
12 data = datasets.load_digits()
13 X = data.data
14 y = data.target
15
16 digit1 = 1
17 digit2 = 8
18 idx = np.append(np.where(y == digit1)[0], np.where(y == digit2)[0])
19 y = data.target[idx]
20 # Change labels to {-1, 1}
21 y[y == digit1] = -1
22 y[y == digit2] = 1
23 X = data.data[idx]
24
25 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
26
27 # Adaboost classification with 5 weak classifiers
28 clf = Adaboost(n_clf=5)
29 clf.fit(X_train, y_train)
30 y_pred = clf.predict(X_test)
31
32 accuracy = accuracy_score(y_test, y_pred)
33 print ("Accuracy:", accuracy)
34
35 # Reduce dimensions to 2d using pca and plot the results
36 Plot().plot_in_2d(X_test, y_pred, title="Adaboost", accuracy=accuracy)
37
38
39 if __name__ == "__main__":
40 main()