def test_non_square_precomputed_distances(): # Precomputed distance matrices must be square matrices. tsne = TSNE(metric="precomputed") assert_raises_regexp(ValueError, ".* square distance matrix", tsne.fit_transform, np.array([[0.0], [1.0]]))
def test_early_exaggeration_too_small(): # Early exaggeration factor must be >= 1. tsne = TSNE(early_exaggeration=0.99) assert_raises_regexp(ValueError, "early_exaggeration .*", tsne.fit_transform, np.array([[0.0], [0.0]]))
def test_too_few_iterations(): # Number of gradient descent iterations must be at least 200. tsne = TSNE(n_iter=199) assert_raises_regexp(ValueError, "n_iter .*", tsne.fit_transform, np.array([[0.0], [0.0]]))
def test_n_components_range(): # barnes_hut method should only be used with n_components <= 3 tsne = TSNE(n_components=4, method="barnes_hut") assert_raises_regexp(ValueError, "'n_components' should be .*", tsne.fit_transform, np.array([[0.0], [1.0]]))
import pandas as pd from scipy.io import loadmat from sklearn.manifold.t_sne import TSNE chris_data = loadmat('/home/itskov/Downloads/dataForEyal.mat') tnse_manifold = TSNE().fit_transform(chris_data['PCs']) pass
def test_init_ndarray_precomputed(): # Initialize TSNE with ndarray and metric 'precomputed' # Make sure no FutureWarning is thrown from _fit tsne = TSNE(init=np.zeros((100, 2)), metric="precomputed") tsne.fit(np.zeros((100, 100)))
def test_angle_out_of_range_checks(): # check the angle parameter range for angle in [-1, -1e-6, 1 + 1e-6, 2]: tsne = TSNE(angle=angle) assert_raises_regexp(ValueError, "'angle' must be between 0.0 - 1.0", tsne.fit_transform, np.array([[0.0], [1.0]]))
def test_too_few_iterations(): # Number of gradient descent iterations must be at least 200. tsne = TSNE(n_iter=199) with pytest.raises(ValueError, match="n_iter .*"): tsne.fit_transform(np.array([[0.0], [0.0]]))
def test_non_square_precomputed_distances(): # Precomputed distance matrices must be square matrices. tsne = TSNE(metric="precomputed") with pytest.raises(ValueError, match=".* square distance matrix"): tsne.fit_transform(np.array([[0.0], [1.0]]))
img_path_dict = dict(zip(letters, paths)) images = [] for letter in letters: img = Image.open(img_path_dict[letter]) img = img.crop((80, 50, 400, 365)) img.thumbnail((16, 16), Image.ANTIALIAS) images.append(img) images_dict = dict(zip(letters, images)) X, Y = get_input_data( '../letter_classification/letter_classification_train.data') images = [] for letter in Y: images.append(images_dict[letter]) tsne = TSNE(n_components=2, init='pca', verbose=1, learning_rate=600, n_iter=5000) t0 = time.time() X_tsne = tsne.fit_transform(X) plot_embedding( X_tsne, Y, X, images, "t-SNE embedding of the letters. Time: %.3f seconds" % (time.time() - t0)) plt.show()
def test_early_exaggeration_too_small(): # Early exaggeration factor must be >= 1. tsne = TSNE(early_exaggeration=0.99) with pytest.raises(ValueError, match="early_exaggeration .*"): tsne.fit_transform(np.array([[0.0], [0.0]]))
#create reduced feature matrix reader=csv.reader(open("reduced_features.csv","r"),delimiter=",") X=list(reader) X=np.array(X) X=X.astype(np.float) #create result vector reader=csv.reader(open("target_output.csv","r"),delimiter=",") y=list(reader) y=np.array(y) y=y.astype(np.int) y=y.ravel() X_Train_embedded = TSNE(n_components=2).fit_transform(X) print X_Train_embedded.shape model = BernoulliNB().fit(X,y) y_predicted = model.predict(X) # replace the above by your data and model # create meshgrid resolution = 1024 # 100x100 background pixels X2d_xmin, X2d_xmax = np.min(X_Train_embedded[:,0]), np.max(X_Train_embedded[:,0]) X2d_ymin, X2d_ymax = np.min(X_Train_embedded[:,1]), np.max(X_Train_embedded[:,1]) xx, yy = np.meshgrid(np.linspace(X2d_xmin, X2d_xmax, resolution), np.linspace(X2d_ymin, X2d_ymax, resolution)) # approximate Voronoi tesselation on resolution x resolution grid using 1-NN background_model = KNeighborsClassifier(n_neighbors=1).fit(X_Train_embedded, y_predicted) voronoiBackground = background_model.predict(np.c_[xx.ravel(), yy.ravel()]) voronoiBackground = voronoiBackground.reshape((resolution, resolution))
def test_exact_no_precomputed_sparse(): tsne = TSNE(metric='precomputed', method='exact') with pytest.raises(TypeError, match='sparse'): tsne.fit_transform(sp.csr_matrix([[0, 5], [5, 0]]))
def test_bad_precomputed_distances(method, D, retype, message_regex): tsne = TSNE(metric="precomputed", method=method) with pytest.raises(ValueError, match=message_regex): tsne.fit_transform(retype(D))
def test_init_not_available(): # 'init' must be 'pca', 'random', or numpy array. tsne = TSNE(init="not available") m = "'init' must be 'pca', 'random', or a numpy array" assert_raises_regexp(ValueError, m, tsne.fit_transform, np.array([[0.0], [1.0]]))
def test_init_not_available(): # 'init' must be 'pca', 'random', or numpy array. tsne = TSNE(init="not available") m = "'init' must be 'pca', 'random', or a numpy array" with pytest.raises(ValueError, match=m): tsne.fit_transform(np.array([[0.0], [1.0]]))
def test_init_ndarray(): # Initialize TSNE with ndarray and test fit tsne = TSNE(init=np.zeros((100, 2))) X_embedded = tsne.fit_transform(np.ones((100, 5))) assert_array_equal(np.zeros((100, 2)), X_embedded)
def test_method_not_available(): # 'nethod' must be 'barnes_hut' or 'exact' tsne = TSNE(method='not available') with pytest.raises(ValueError, match="'method' must be 'barnes_hut' or "): tsne.fit_transform(np.array([[0.0], [1.0]]))
def test_method_not_available(): # 'nethod' must be 'barnes_hut' or 'exact' tsne = TSNE(method='not available') assert_raises_regexp(ValueError, "'method' must be 'barnes_hut' or ", tsne.fit_transform, np.array([[0.0], [1.0]]))
def test_n_components_range(): # barnes_hut method should only be used with n_components <= 3 tsne = TSNE(n_components=4, method="barnes_hut") with pytest.raises(ValueError, match="'n_components' should be .*"): tsne.fit_transform(np.array([[0.0], [1.0]]))
def test_pca_initialization_not_compatible_with_precomputed_kernel(): # Precomputed distance matrices must be square matrices. tsne = TSNE(metric="precomputed", init="pca") assert_raises_regexp(ValueError, "The parameter init=\"pca\" cannot be " "used with metric=\"precomputed\".", tsne.fit_transform, np.array([[0.0], [1.0]]))
def test_distance_not_available(): """'metric' must be valid.""" tsne = TSNE(metric="not available") assert_raises_regexp(ValueError, "Unknown metric not available.*", tsne.fit_transform, np.array([[0.0], [1.0]]))
def test_chebyshev_metric(): # t-SNE should allow metrics that cannot be squared (issue #3526). random_state = check_random_state(0) tsne = TSNE(metric="chebyshev") X = random_state.randn(5, 2) tsne.fit_transform(X)
name = image.split('/')[-1] if name in valid_names: img = open_image(image) pred = learn.predict(img) embedding = pred[-1].detach().numpy() x.append(embedding) y.append(n_class) n_class += 1 x, y = np.asarray(x), np.asarray(y) print(x.shape, y.shape) print(np.unique(y)) x_tsne = TSNE(n_components=2).fit_transform(x) print(x_tsne.shape) color = ['darkcyan', 'g', 'y', 'magenta', 'lightgreen', 'mediumslateblue', 'yellow', 'brown', 'k', 'b'] labels = [str(i) for i in range(10)] times = [0 for i in range(10)] for i in range(len(x_tsne)): if times[y[i]] == 0: plt.scatter(x_tsne[i, 0], x_tsne[i, 1], color=color[y[i]], label=labels[y[i]]) times[y[i]] += 1 else: plt.scatter(x_tsne[i, 0], x_tsne[i, 1], color=color[y[i]]) plt.legend() plt.title('TSNE validation') plt.savefig('tsne_val.png')