def main(): data = datasets.load_digits() X = data.data y = data.target pca = PCA() X = pca.transform(X, n_components=5) # Reduce to 5 dimensions X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, seed=1) clf = RandomForest(debug=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print("Accuracy:", accuracy_score(y_test, y_pred)) pca.plot_in_2d(X_test, y_pred)
digit2 = 8 idx = np.append( np.where(data.target == digit1)[0], np.where(data.target == digit2)[0]) y = data.target[idx] # Change labels to {0, 1} y[y == digit1] = 0 y[y == digit2] = 1 X = data.data[idx] X = normalize(X) # .......................... # DIMENSIONALITY REDUCTION # .......................... pca = PCA() X = pca.transform(X, n_components=5) # Reduce to 5 dimensions # .......................... # TRAIN / TEST SPLIT # .......................... X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) # Rescale label for Adaboost to {-1, 1} rescaled_y_train = 2 * y_train - np.ones(np.shape(y_train)) rescaled_y_test = 2 * y_test - np.ones(np.shape(y_test)) # ....... # SETUP # ....... adaboost = Adaboost(n_clf=8) naive_bayes = NaiveBayes() knn = KNN(k=4)
data = datasets.load_digits() digit1 = 1 digit2 = 8 idx = np.append(np.where(data.target == digit1)[0], np.where(data.target == digit2)[0]) y = data.target[idx] # Change labels to {0, 1} y[y == digit1] = 0 y[y == digit2] = 1 X = data.data[idx] X = normalize(X) # .......................... # DIMENSIONALITY REDUCTION # .......................... pca = PCA() X = pca.transform(X, n_components=5) # Reduce to 5 dimensions # .......................... # TRAIN / TEST SPLIT # .......................... X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) # Rescale label for Adaboost to {-1, 1} rescaled_y_train = 2*y_train - np.ones(np.shape(y_train)) rescaled_y_test = 2*y_test - np.ones(np.shape(y_test)) # ....... # SETUP # ....... adaboost = Adaboost(n_clf = 8) naive_bayes = NaiveBayes()