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 == "1.3": dataset = load_dataset("animals.pkl") X = dataset["X"].astype(float) animals = dataset["animals"] n, d = X.shape model = MDS(n_components=2) Z = model.compress(X) fig, ax = plt.subplots() ax.scatter(Z[:, 0], Z[:, 1]) plt.ylabel("z2") plt.xlabel("z1") plt.title("MDS") for i in range(n): ax.annotate(animals[i], (Z[i, 0], Z[i, 1])) utils.savefig("MDS_animals.png") elif question == "1.4": dataset = load_dataset("animals.pkl") X = dataset["X"].astype(float) animals = dataset["animals"]
ax[1, 1].imshow(Xhat_pca[i].reshape(h, w).T, cmap='gray') ax[1, 2].set_title('$|x_i-\hat{x_i}|$>threshold (L1)') ax[1, 2].imshow( (np.abs(X[i] - Xhat_pca[i]) < threshold).reshape(h, w).T, cmap='gray') utils.savefig('highway_{:03d}.jpg'.format(i)) elif question == '4': dataset = load_dataset('animals.pkl') X = dataset['X'].astype(float) animals = dataset['animals'] n, d = X.shape model = MDS(n_components=2) Z = model.compress(X) fig, ax = plt.subplots() ax.scatter(Z[:, 0], Z[:, 1]) plt.ylabel('z2') plt.xlabel('z1') plt.title('MDS') for i in range(n): ax.annotate(animals[i], (Z[i, 0], Z[i, 1])) utils.savefig('MDS_animals.png') elif question == '4.1': dataset = load_dataset('animals.pkl') X = dataset['X'].astype(float) animals = dataset['animals']
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 == '1.3': dataset = load_dataset('animals.pkl') X = dataset['X'].astype(float) animals = dataset['animals'] n, d = X.shape model = MDS(n_components=2) Z = model.compress(X) print( utils.euclidean_dist_squared(Z, Z).shape, utils.euclidean_dist_squared(X, X).shape) print( np.sqrt(utils.euclidean_dist_squared(Z, Z)) - np.sqrt(utils.euclidean_dist_squared(X, X))) # diff = np.sum(np.square(np.sqrt(utils.euclidean_dist_squared(Z, Z)) - np.sqrt(utils.euclidean_dist_squared(X, X)))) # print(diff) fig, ax = plt.subplots() ax.scatter(Z[:, 0], Z[:, 1]) plt.ylabel('z2') plt.xlabel('z1') plt.title('MDS')