def visualize_zs(zs, labels, perplexity=30, learning_rate=200, n_iter=1000): plt.clf() plt.figure(figsize=(7,6)) points = TSNE(n_components=2, random_state=0, perplexity=perplexity, learning_rate=learning_rate, n_iter=n_iter).fit_transform(zs) plt.scatter(points.transpose()[0], points.transpose()[1], s=20, c=labels) plt.colorbar() plt.show()
def visualize_zsp(zs, perplexities, labels=None, learning_rate=200, n_iter=2000): plt.clf() fig = plt.figure(figsize=(len(perplexities) * 4, 4)) for i, perplexity in enumerate(perplexities): points = TSNE(n_components=2, random_state=0, perplexity=perplexity, learning_rate=learning_rate, n_iter=n_iter).fit_transform(zs) ax = fig.add_subplot(1, len(perplexities), i + 1) ax.scatter(points.transpose()[0], points.transpose()[1], s=5) if labels is None else ax.scatter(points.transpose()[0], points.transpose()[1], s=5, c=labels) fig.show()
def CalculateTNSE(tdata): u_tnse = TSNE(n_components=2).fit_transform(tdata.T) u_tnse = u_tnse.transpose() u_tnse = u_tnse[:2] dataTNSE = np.concatenate((u_tnse[0][:,None],u_tnse[1][:,None]), axis = 1) W_TNSE = tdata.dot(dataTNSE) return W_TNSE
def compute_tsne__csv(input_filename, output_filename): df = pd.read_csv(input_filename, header=0) features_df = df[df.columns[2:12]] X = np.array(features_df) X_embedded = TSNE( n_components=2, early_exaggeration=48, # default is 12 learning_rate=100, # default is 200 verbose=0, # default is 0 ).fit_transform(X) X_embedded__transposed = X_embedded.transpose() df.insert(14, 'tsne_0', X_embedded__transposed[0], True) df.insert(15, 'tsne_1', X_embedded__transposed[1], True) df.to_csv(output_filename, index=False)
df.to_csv(output_filename, index=False) if __name__ == '__main__': df = pd.read_csv( 'transitional_data/rnaseq_data__genes_x_timepoints__normalized__go_labeled.csv' ) features_df = df[df.columns[2:12]] X = np.array(features_df) X_embedded = TSNE( n_components=2, early_exaggeration=48, # default is 12 learning_rate=100, # default is 200 verbose=2, # default is 0 ).fit_transform(X) X_embedded__transposed = X_embedded.transpose() scatter_plot(X_embedded__transposed[0], X_embedded__transposed[1]) df.insert(14, 'tsne_0', X_embedded__transposed[0], True) df.insert(15, 'tsne_1', X_embedded__transposed[1], True) df.to_csv( 'transitional_data/rnaseq_data__genes_x_timepoints__normalized__go_labeled__tsne.csv', index=False)
testVec = data[:, border:] # test data vectors # initialize kohonen network kohNet = nf.netInit(7) # teach network on learnVec kohNetLearned, clustLearnVec = nf.netLearn(kohNet, learnVec) # test network clustTestVec = nf.netClust(kohNetLearned, testVec) # reduce data dimension to 3 by using TSNE (for possible visualising) data_embedded = TSNE(n_components=3, method='barnes_hut').fit_transform(data.transpose()) data_embedded = data_embedded.transpose() # output results clustVec = np.concatenate((clustLearnVec, clustTestVec)) print("\nClusters of data\n", clustVec, "\n") print("Number of classes: ", kohNetLearned['numClusters']) print("\nEmbedded data\n", data_embedded) print('\n-- The algorithm took', np.float16(time.time() - start_time), 'seconds to complete --') # execution time # In[ ]:
import numpy as np import matplotlib.pyplot as plt from sklearn.manifold import TSNE X = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) X_embedded = TSNE(n_components=2).fit_transform(X) Y = X_embedded.transpose() plt.plot(Y[0], Y[1], "p") plt.show()