本文整理汇总了Python中sklearn.manifold.TSNE类的典型用法代码示例。如果您正苦于以下问题:Python TSNE类的具体用法?Python TSNE怎么用?Python TSNE使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TSNE类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: display_closestwords_tsnescatterplot
def display_closestwords_tsnescatterplot(model, word):
arr = np.empty((0,300), dtype='f')
word_labels = [word]
# get close words
close_words = model.similar_by_word(word)
# add the vector for each of the closest words to the array
arr = np.append(arr, np.array([model[word]]), axis=0)
for wrd_score in close_words:
wrd_vector = model[wrd_score[0]]
word_labels.append(wrd_score[0])
arr = np.append(arr, np.array([wrd_vector]), axis=0)
# find tsne coords for 2 dimensions
tsne = TSNE(n_components=2, random_state=0)
np.set_printoptions(suppress=True)
Y = tsne.fit_transform(arr)
x_coords = Y[:, 0]
y_coords = Y[:, 1]
# display scatter plot
plt.scatter(x_coords, y_coords)
for label, x, y in zip(word_labels, x_coords, y_coords):
plt.annotate(label, xy=(x, y), xytext=(0, 0), textcoords='offset points')
plt.xlim(x_coords.min()+0.00005, x_coords.max()+0.00005)
plt.ylim(y_coords.min()+0.00005, y_coords.max()+0.00005)
plt.show()
示例2: plot_data
def plot_data(data, has_label=True):
import numpy as np
import seaborn as sns
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
if not has_label:
data = data.copy()
data['label'] = np.zeros([len(data),1])
LIMIT = 4000
if data.shape[0] > LIMIT:
dt = data.sample(n=LIMIT, replace=False)
X = dt.ix[:,:-1]
labels = dt.ix[:,-1]
else:
X = data.ix[:,:-1]
labels = data.ix[:,-1]
tsne_model = TSNE(n_components=2, random_state=0)
np.set_printoptions(suppress=True)
points1 = tsne_model.fit_transform(X)
df1 = pd.DataFrame(data=np.column_stack([points1,labels]), columns=["x","y","class"])
sns.lmplot("x", "y", data=df1, hue='class', fit_reg=False, palette=sns.color_palette('colorblind'))
sns.plt.title('TNSE')
pca = PCA(n_components=2)
pca.fit(X)
points2 = pca.transform(X)
df2 = pd.DataFrame(data=np.column_stack([points2,labels]), columns=["x","y","class"])
sns.lmplot("x", "y", data=df2, hue='class', fit_reg=False, palette=sns.color_palette('colorblind'))
sns.plt.title('PCA')
示例3: make_tsne_plot
def make_tsne_plot(model, rel_wds, plot_lims, title):
dim = 30
X, keys = make_data_matrix(model)
# first we actually do PCA to reduce the
# dimensionality to make tSNE easier to calculate
X_std = StandardScaler().fit_transform(X)
sklearn_pca = PCA(n_components=2)
X = sklearn_pca.fit_transform(X_std)[:,:dim]
# do downsample
k = 5000
sample = []
important_words = []
r_wds = [word[0] for word in rel_wds]
for i, key in enumerate(keys):
if key in r_wds:
sample.append(i)
sample = np.concatenate((np.array(sample),
np.random.choice(len(keys), k-10, replace = False),
))
X = X[sample,:]
keys = [keys[i] for i in sample]
# Do tSNE
tsne = TSNE(n_components=2, random_state=0, metric="cosine")
X_transf = tsne.fit_transform(X)
k_means = KMeans(n_clusters=8)
labels = k_means.fit_predict(X_transf)
scatter_plot(X_transf[:,0], X_transf[:,1], rel_wds, labels, title, keys, plot_lims)
示例4: t_sne_view
def t_sne_view(norm_table, subj_cond, cohorts, image_type):
# t-SNE analysis: Use stochastic neighbor embedding to reduce dimensionality of
# data set to two dimensions in a non-linear, distance dependent fashion
# Perform PCA data reduction if dimensionality of feature space is large:
if len(norm_table.columns) > 12:
pca = PCA(n_components = 12)
pca.fit(norm_table.as_matrix())
raw_data = pca.transform(norm_table.as_matrix())
else:
raw_data = norm_table.as_matrix()
# Transform data into a two-dimensional embedded space:
tsne = TSNE(n_components = 2, perplexity = 40.0, early_exaggeration= 2.0,
learning_rate = 100.0, init = 'pca')
tsne_data = tsne.fit_transform(raw_data)
# Prepare for normalization and view:
cols = ['t-SNE', 'Cluster Visualization']
tsne_table = pd.DataFrame(tsne_data, index = norm_table.index, columns = cols)
# The output is no longer centered or normalized, so shift & scale it before display:
tsne_avg = ppmi.data_stats(tsne_table, subj_cond, cohorts)
tsne_norm_table = ppmi.normalize_table(tsne_table, tsne_avg)
# Send out to graphics rendering engine:
if (image_type == 'Gauss'):
return scg.scatter_gauss(tsne_norm_table[cols[0]], tsne_norm_table[cols[1]], subj_cond)
elif (image_type == 'Scatter'):
return scg.scatter_plain(tsne_norm_table[cols[0]], tsne_norm_table[cols[1]], subj_cond)
示例5: perform_AE
def perform_AE(X, dim=2, tsne=False):
y = np.zeros(shape=X.shape[0], dtype=int)
if tsne:
hidden_layers = [X.shape[1], 500, 100, 32]
encoder_weights, decoder_weights = pretrain(X, hidden_layers)
X_32d = ae(X, encoder_weights, decoder_weights, hidden_layers)
ae_tsne = TSNE(n_components=dim, learning_rate=800, verbose=1)
X_2d = ae_tsne.fit_transform(X_32d)
method = 'ae_tsne_scaled'
### END - if tsne
else:
hidden_layers = [X.shape[1], 500, 100, 20, dim]
encoder_weights, decoder_weights = pretrain(X, hidden_layers)
X_2d = ae(X, encoder_weights, decoder_weights, hidden_layers)
method = 'ae_scaled'
### END - else
print('***** ' + method + ' *****')
cluster(X_2d, method)
np.save("{0}_{1}_X_2d".format(species, method), X_2d)
示例6: visualize_latent_rep
def visualize_latent_rep(args, model, x_latent):
print("pca_on=%r pca_comp=%d tsne_comp=%d tsne_perplexity=%f tsne_lr=%f" % (
args.use_pca,
args.pca_components,
args.tsne_components,
args.tsne_perplexity,
args.tsne_lr
))
if args.use_pca:
pca = PCA(n_components = args.pca_components)
x_latent = pca.fit_transform(x_latent)
figure(figsize=(6, 6))
scatter(x_latent[:, 0], x_latent[:, 1], marker='.')
show()
tsne = TSNE(n_components = args.tsne_components,
perplexity = args.tsne_perplexity,
learning_rate = args.tsne_lr,
n_iter = args.tsne_iterations,
verbose = 4)
x_latent_proj = tsne.fit_transform(x_latent)
del x_latent
figure(figsize=(6, 6))
scatter(x_latent_proj[:, 0], x_latent_proj[:, 1], marker='.')
show()
示例7: vizualize_clusters
def vizualize_clusters(X, y, py, hist=None):
""" Using T-SNE to visualize the site clusters.
Plot and save the scatter (and the histogramm).
"""
model = TSNE(n_components=2, random_state=0)
fig = model.fit_transform(X, y)
fig1 = model.fit_transform(X, py)
pyplot.figure(figsize=(16, 8))
pyplot.subplot(121)
classes = list(set(y))
for c, color in zip(classes, plt.colors.cnames.iteritems()):
indeces = [i for i, p in enumerate(y) if p == c]
pyplot.scatter(fig[indeces, 0], fig[indeces, 1], marker="o", c=color[0])
pyplot.subplot(122)
clusters = list(set(py))
for c, color in zip(clusters, plt.colors.cnames.iteritems()):
indeces = [i for i, p in enumerate(py) if p == c]
pyplot.scatter(fig1[indeces, 0], fig1[indeces, 1], marker="o", c=color[0])
# pyplot.show()
pyplot.savefig("clusters" + "_scatter.png")
if hist is not None:
pyplot.figure(figsize=(4, 4))
pyplot.xticks(clusters)
pyplot.bar(clusters, hist, align="center")
# pyplot.show()
pyplot.savefig("clusters" + "_hist.png")
示例8: tsnePlot
def tsnePlot(plotname, modelName, word, dest):
"""Plots a tsne graph of words most similar to the word passed in the argument (as represented in the model previously calculated)"""
model = word2vec.Word2Vec.load(modelName)
words = [model.most_similar(word)[i][0] for i in range(0, len(model.most_similar(word)))]
words.append(word)
#nested list constaining 100 dimensional word vectors of each most-similar word
word_vectors = [model[word] for word in words]
word_vectors = np.array(word_vectors)
tsne_model = TSNE(n_components=2, random_state=0)
Y = tsne_model.fit_transform(word_vectors)
sb.plt.plot(Y[:,0], Y[:,1], 'o')
for word, x, y in zip(words, Y[:,0], Y[:,1]):
sb.plt.annotate(word, (x, y), size=12)
#sb.plt.pause(10)
plotname = plotname + ".png"
if not os.path.exists(dest):
os.makedirs(dest)
path = os.path.join(dest, plotname)
sb.plt.savefig(path)
示例9: reduce_dimentionality
def reduce_dimentionality(self):
self.vectors = []
for key in self.selected_words:
self.vectors.append(self.model[key])
tnse_model = TSNE(n_components=2, random_state=0)
np.set_printoptions(suppress=True)
self.reduced_vectors = tnse_model.fit_transform(self.vectors)
示例10: plotTSNEDecisionBoundaries
def plotTSNEDecisionBoundaries():
tsne = TSNE()
tsne_data = tsne.fit_transform(feature_set)
x_min,x_max = tsne_data[:,0].min()-1, tsne_data[:,0].max() + 1
y_min,y_max = tsne_data[:,1].min()-1, tsne_data[:,1].max() + 1
step_size = 2.0
xx,yy = np.meshgrid(np.arange(x_min,x_max,step_size),np.arange(y_min,y_max,step_size))
for index,classifier in enumerate(classifiers):
plt.subplot(2,3,index+1)
plt.subplots_adjust(wspace=0.5,hspace=0.5)
classifier.fit(tsne_data,class_labels)
Z = classifier.predict(zip(xx.ravel(),yy.ravel()))
Z = Z.reshape(xx.shape)
plt.contourf(xx,yy,Z,cmap=plt.cm.Paired,alpha=0.7)
plt.scatter(tsne_data[:,0],tsne_data[:,1],c=class_labels,cmap=plt.cm.rainbow,alpha=0.6)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.xlim(x_min,x_max)
plt.ylim(y_min,y_max)
plt.xticks(())
plt.yticks(())
plt.title(classifier_names[index])
plt.show()
示例11: tsne_plot
def tsne_plot(model):
#"Creates and TSNE model and plots it"
labels = []
tokens = []
for word in model.wv.vocab:
tokens.append(model[word])
labels.append(word)
tsne_model = TSNE(perplexity=40, n_components=2, init='pca', n_iter=2500, random_state=23)
new_values = tsne_model.fit_transform(tokens)
x = []
y = []
for value in new_values:
x.append(value[0])
y.append(value[1])
plt.figure(figsize=(16, 16))
for i in range(len(x)):
plt.scatter(x[i], y[i])
plt.annotate(labels[i],
xy=(x[i], y[i]),
xytext=(5, 2),
textcoords='offset points',
ha='right',
va='bottom')
plt.show()
示例12: visualization
def visualization(result, word_dict):
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
plot_only = 500
low_dim_embs = tsne.fit_transform(result[0:500])
labels = [ word_dict[i] for i in range(500) ]
plot_with_labels(low_dim_embs, labels)
示例13: plotly_js_viz
def plotly_js_viz(word_2_vec_model):
tsne_model=TSNE(n_components=2,random_state=5)
data=tsne_model.fit_transform(word_2_vec_model.syn0)
xd=list(data[:,0])
yd=list(data[:,1])
names_our=word_2_vec_model.index2word
plot([Scatter(x=xd,y=yd,mode="markers",text=names_our)])
示例14: PlotTSNE
def PlotTSNE (data, labels): #Takes the data and the labels
# Visualize the results on TSNE reduced data
print "BUSY IN TSNE"
model = TSNE(n_components=2, random_state=0)
reduced_data = model.fit_transform(data)
print "DATA REDUCED"
# Plot the decision boundary. For that, we will assign a color to each
x_min, x_max = reduced_data[:, 0].min(), reduced_data[:, 0].max()
y_min, y_max = reduced_data[:, 1].min(), reduced_data[:, 1].max()
plt.plot(reduced_data[:, 0], reduced_data[:, 1], 'k.', markersize=2)
#Adds labels to the plot
for label, x, y in zip(labels, reduced_data[:, 0], reduced_data[:, 1]):
plt.annotate(
label,
xy = (x, y), xytext = (-20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'green', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.title('TSNE Plot')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
示例15: labtest_TSNE
def labtest_TSNE(PID):
data = [patients[pid]['tests'] for pid in PID]
X = pp.scale(data)
tsne = TSNE(n_components=2, perplexity=30.0, learning_rate=1000.0, n_iter=1000, n_iter_without_progress=30, min_grad_norm=1e-07, angle=0.5)
pos = tsne.fit(X).embedding_
return pos