Xtest = Xtest.astype('float32') model = KNN(k=3) model.fit(X, y) yhat = model.predict(X) tr_error = np.mean(yhat != y) print("k is 3", "training error is ", tr_error) yhat = model.predict(Xvalid) v_error = np.mean(yhat != yvalid) print("k is 3", "validation error is ", v_error) elif Model == "softmax": # standardize X X, mu, sigma = utils.standardize_cols(X) Xvalid, _, _ = utils.standardize_cols(Xvalid, mu, sigma) Xtest, _, _ = utils.standardize_cols(Xtest, mu, sigma) model = softmax.softmaxClassifier() minibatch = [500, 1000, 1500] alpha = [0.01, 0.001, 0.0005] min_val_error = 1 best_batch = 0 best_alpha = 0 for m in range(3): for a in range(3): val_error = [] # cross validation for train, validate in kf.split(X, y):
plt.figure() plt.scatter(X[:, f1], X[:, f2]) plt.xlabel("$x_{%d}$" % f1) plt.ylabel("$x_{%d}$" % f2) for i in range(n): plt.annotate(animals[i], (X[i, f1], X[i, f2])) utils.savefig('two_random_features.png') elif question == '2.2': dataset = load_dataset('animals.pkl') X = dataset['X'].astype(float) animals = dataset['animals'] n, d = X.shape # standardize columns X = utils.standardize_cols(X) model = PCA(k=2) model.fit(X) Z = model.compress(X) fig, ax = plt.subplots() plt.ylabel('z2') plt.xlabel('z1') ax.scatter(Z[:, 0], Z[:, 1]) for i in range(n): ax.annotate(animals[i], (Z[i, 0], Z[i, 1])) utils.savefig('q2_2_PCA_animals.png') elif question == '3.1': X = load_dataset('highway.pkl')['X'].astype(float) / 255
parser = argparse.ArgumentParser() parser.add_argument('-q', '--question', required=True, choices=['1.2', '2.1', '3', '3.1', '3.2']) io_args = parser.parse_args() question = io_args.question if question == '1.2': dataset = utils.load_dataset('animals') X = dataset['X'].astype(float) animals = dataset['animals'] n, d = X.shape k = 5 X = utils.standardize_cols(X) # standardize columns model = PCA(k=2) model.fit(X) Z = model.compress(X) # Plot the matrix plt.imshow(Z) utils.savefig('q1_unsatisfying_visualization_1.png') ## Randomly plot two features, and label all points fig, ax = plt.subplots() ax.scatter(Z[:, 0], Z[:, 1]) for i in range(n): ax.annotate(animals[i], (Z[i, 0], Z[i, 1]))