num_folds = 5 k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100] # k_choices = [1, 5] x_train_folds = [] y_train_folds = [] # x_train_folds = np.array(np.split(x_train, num_folds)) # y_train_folds = np.array(np.split(y_train, num_folds)) x_train_folds = np.array(np.split(x_test, num_folds)) y_train_folds = np.array(np.split(y_test, num_folds)) k_to_accuracies = {} for k in k_choices: curr_acc = [] for i in np.arange(num_folds): indx = np.array([j for j in range(num_folds) if j != i]) X_test_n = x_train_folds[i] y_test_n = y_train_folds[i].reshape(-1,) x_train_folds[indx] x_train_n = np.concatenate(x_train_folds[indx], axis=0) y_train_n = np.concatenate(y_train_folds[indx], axis=None) classifier = KNearestNeighbor() classifier.train(x_train_n, y_train_n) dists = classifier.compute_distances_two_loops(X_test_n) y_test_n_pred = classifier.predict_labels(dists, k) num_correct = np.sum(y_test_n_pred == y_test_n) accuracy = float(num_correct) / len(y_test_n) curr_acc.append(accuracy) k_to_accuracies[k] = curr_acc