40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from __future__ import division, print_function
|
|
import numpy as np
|
|
from sklearn import datasets
|
|
|
|
# Import helper functions
|
|
from mlfromscratch.supervised_learning import Adaboost
|
|
from mlfromscratch.utils.data_manipulation import train_test_split
|
|
from mlfromscratch.utils.data_operation import accuracy_score
|
|
from mlfromscratch.utils import Plot
|
|
|
|
def main():
|
|
data = datasets.load_digits()
|
|
X = data.data
|
|
y = data.target
|
|
|
|
digit1 = 1
|
|
digit2 = 8
|
|
idx = np.append(np.where(y == digit1)[0], np.where(y == digit2)[0])
|
|
y = data.target[idx]
|
|
# Change labels to {-1, 1}
|
|
y[y == digit1] = -1
|
|
y[y == digit2] = 1
|
|
X = data.data[idx]
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
|
|
|
|
# Adaboost classification with 5 weak classifiers
|
|
clf = Adaboost(n_clf=5)
|
|
clf.fit(X_train, y_train)
|
|
y_pred = clf.predict(X_test)
|
|
|
|
accuracy = accuracy_score(y_test, y_pred)
|
|
print ("Accuracy:", accuracy)
|
|
|
|
# Reduce dimensions to 2d using pca and plot the results
|
|
Plot().plot_in_2d(X_test, y_pred, title="Adaboost", accuracy=accuracy)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |