def knn_classifier(X, y): """ K Nearest Neighbours classifier Train and test given the entire data Predict classes for the provided examples """ knn = KNN(X,y) knn.train() print(knn.evaluate()) knn.predict_for_examples(examples)
X_val, y_val = data['X_val'], data['y_val'] X_test, y_test = data['X_test'], data['y_test'] X_train = np.reshape(X_train, (X_train.shape[0], -1)) X_val = np.reshape(X_val, (X_val.shape[0], -1)) X_test = np.reshape(X_test, (X_test.shape[0], -1)) def get_acc(pred, y_test): return np.sum(y_test == pred) / len(y_test) * 100 print("finished reading data") knn = KNN(5) knn.train(X_train, y_train) pred_knn = knn.predict(X_test) print('The testing accuracy is given by : %f' % (get_acc(pred_knn, y_test))) ''' knn = KNN(5) knn.train(X_train, y_train) pred_knn = knn.predict(X_test) print('The testing accuracy is given by : %f' % (get_acc(pred_knn, y_test))) percept_ = Perceptron() percept_.train(X_train, y_train) pred_percept = percept_.predict(X_test) print('The testing accuracy is given by : %f' % (get_acc(pred_percept, y_test)))