本文整理汇总了Python中sklearn.manifold.TSNE属性的典型用法代码示例。如果您正苦于以下问题:Python manifold.TSNE属性的具体用法?Python manifold.TSNE怎么用?Python manifold.TSNE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类sklearn.manifold
的用法示例。
在下文中一共展示了manifold.TSNE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plot_tsne
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def plot_tsne(self, save_eps=False):
''' Plot TSNE figure. Set save_eps=True if you want to save a .eps file.
'''
tsne = TSNE(n_components=2, init='pca', random_state=0)
features = tsne.fit_transform(self.features)
x_min, x_max = np.min(features, 0), np.max(features, 0)
data = (features - x_min) / (x_max - x_min)
del features
for i in range(data.shape[0]):
plt.text(data[i, 0], data[i, 1], str(self.labels[i]),
color=plt.cm.Set1(self.labels[i] / 10.),
fontdict={'weight': 'bold', 'size': 9})
plt.xticks([])
plt.yticks([])
plt.title('T-SNE')
if save_eps:
plt.savefig('tsne.eps', dpi=600, format='eps')
plt.show()
示例2: learn_manifold
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def learn_manifold(manifold_type, feats, n_components=2):
if manifold_type == 'tsne':
feats_fitted = manifold.TSNE(n_components=n_components, random_state=0).fit_transform(feats)
elif manifold_type == 'isomap':
feats_fitted = manifold.Isomap(n_components=n_components).fit_transform(feats)
elif manifold_type == 'mds':
feats_fitted = manifold.MDS(n_components=n_components).fit_transform(feats)
elif manifold_type == 'spectral':
feats_fitted = manifold.SpectralEmbedding(n_components=n_components).fit_transform(feats)
else:
raise Exception('wrong maniford type!')
# methods = ['standard', 'ltsa', 'hessian', 'modified']
# feats_fitted = manifold.LocallyLinearEmbedding(n_components=n_components, method=methods[0]).fit_transform(pred)
return feats_fitted
示例3: plot
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def plot(self, words, num_points=None):
if not num_points:
num_points = len(words)
embeddings = self.get_words_embeddings(words)
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
two_d_embeddings = tsne.fit_transform(embeddings[:num_points, :])
assert two_d_embeddings.shape[0] >= len(words), 'More labels than embeddings'
pylab.figure(figsize=(15, 15)) # in inches
for i, label in enumerate(words[:num_points]):
x, y = two_d_embeddings[i, :]
pylab.scatter(x, y)
pylab.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points',
ha='right', va='bottom')
pylab.show()
示例4: visualize_embeddings
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def visualize_embeddings(self):
#get most common words
print "getting common words"
allwords = [word for sent in self.allsents for word in sent]
counts = collections.Counter(allwords).most_common(500)
#reduce embeddings to 2d using tsne
print "reducing embeddings to 2D"
embeddings = np.empty((500,embedding_size))
for i in range(500):
embeddings[i,:] = model[counts[i][0]]
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=7500)
embeddings = tsne.fit_transform(embeddings)
#plot embeddings
print "plotting most common words"
fig, ax = plt.subplots(figsize=(30, 30))
for i in range(500):
ax.scatter(embeddings[i,0],embeddings[i,1])
ax.annotate(counts[i][0], (embeddings[i,0],embeddings[i,1]))
plt.show()
示例5: plot_embeddings
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def plot_embeddings(embeddings,):
X, Y = read_node_label('../data/wiki/wiki_labels.txt')
emb_list = []
for k in X:
emb_list.append(embeddings[k])
emb_list = np.array(emb_list)
model = TSNE(n_components=2)
node_pos = model.fit_transform(emb_list)
color_idx = {}
for i in range(len(X)):
color_idx.setdefault(Y[i][0], [])
color_idx[Y[i][0]].append(i)
for c, idx in color_idx.items():
plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c)
plt.legend()
plt.show()
示例6: plot_embeddings
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def plot_embeddings(embeddings,):
X, Y = read_node_label('../data/wiki/wiki_labels.txt')
emb_list = []
for k in X:
emb_list.append(embeddings[k])
emb_list = np.array(emb_list)
model = TSNE(n_components=2)
node_pos = model.fit_transform(emb_list)
color_idx = {}
for i in range(len(X)):
color_idx.setdefault(Y[i][0], [])
color_idx[Y[i][0]].append(i)
for c, idx in color_idx.items():
plt.scatter(node_pos[idx, 0], node_pos[idx, 1],
label=c) # c=node_colors)
plt.legend()
plt.show()
示例7: tsne_plot
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def tsne_plot(xs, xt, xs_label, xt_label, subset=True, title=None, pname=None):
num_test=100
if subset:
combined_imgs = np.vstack([xs[0:num_test, :], xt[0:num_test, :]])
combined_labels = np.vstack([xs_label[0:num_test, :],xt_label[0:num_test, :]])
combined_labels = combined_labels.astype('int')
from sklearn.manifold import TSNE
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=3000)
source_only_tsne = tsne.fit_transform(combined_imgs)
plt.figure(figsize=(10, 10))
plt.scatter(source_only_tsne[:num_test,0], source_only_tsne[:num_test,1],
c=combined_labels[:num_test].argmax(1), s=75, marker='o', alpha=0.5, label='source train data')
plt.scatter(source_only_tsne[num_test:,0], source_only_tsne[num_test:,1],
c=combined_labels[num_test:].argmax(1),s=50,marker='x',alpha=0.5,label='target train data')
plt.legend(loc='best')
plt.title(title)
#%% TSNE plots of source model and target model
示例8: tsne_plot
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def tsne_plot(xs, xt, xs_label, xt_label, map_xs=None, title=None, pname=None):
num_test=1000
if map_xs is not None:
combined_imgs = np.vstack([xs[0:num_test, :], xt[0:num_test, :], map_xs[0:num_test,:]])
combined_labels = np.vstack([xs_label[0:num_test, :],xt_label[0:num_test, :], xs_label[0:num_test,:]])
combined_labels = combined_labels.astype('int')
combined_domain = np.vstack([np.zeros((num_test,1)),np.ones((num_test,1)),np.ones((num_test,1))*2])
from sklearn.manifold import TSNE
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=3000)
source_only_tsne = tsne.fit_transform(combined_imgs)
plot_embedding(source_only_tsne, combined_labels.argmax(1), combined_domain,
title, save_fig=1, pname=pname)
示例9: tsne
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def tsne(features, n_components=2):
"""
Returns the embedded points for TSNE.
Parameters
----------
features: numpy.ndarray
contains the input feature vectors.
n_components: int
number of components to transform the features into
Returns
-------
embedding: numpy.ndarray
x,y(z) points that the feature vectors have been transformed into
"""
embedding = TSNE(n_components=n_components).fit_transform(features)
return embedding
示例10: get_classer
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def get_classer(self, algo_name, classer, algo_dir):
if not os.path.exists(algo_dir):
os.mkdir(algo_dir)
classer_fn = '{}_classer.npy'.format(os.path.join(algo_dir, algo_name))
trafoed_fn = '{}_trafoed.npy'.format(os.path.join(algo_dir, algo_name))
if os.path.isfile(classer_fn):
return pickle.load(open(classer_fn, mode='rb'))
else:
if algo_name == 'DBSCAN':
self.loop_estimate_bandwidth()
logger.info('clustering all speech with {}'.format(algo_name))
if hasattr(classer, 'fit') and hasattr(classer, 'predict'):
classer.fit(self.sdc_all_speech)
elif hasattr(classer, 'fit_transform'): # TSNE
all_speech_trafoed = classer.fit_transform(self.sdc_all_speech)
np.save(open(trafoed_fn, mode='wb'), all_speech_trafoed)
else: # DBSCAN
classer.fit_predict(self.sdc_all_speech)
logger.info(classer.get_params())
logger.info('dumping classifier')
pickle.dump(classer, open(classer_fn, mode='wb'))
return classer
示例11: cal_tsne_embeds
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def cal_tsne_embeds(X, y, n_components=2, text=None, save_path=None):
"""
Plot using tSNE
:param X: embedding
:param y: label
:param n_components: number of components
:param text: text for plot
:param save_path: save path
:return:
"""
X = X[: 500]
y = y[: 500]
tsne = manifold.TSNE(n_components=n_components)
X_tsne = tsne.fit_transform(X, y)
plot_2d_embeds(X_tsne, y, text, save_path)
示例12: cal_tsne_embeds_src_tgt
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def cal_tsne_embeds_src_tgt(Xs, ys, Xt, yt, n_components=2, text=None, save_path=None, n_samples=1000, names=None):
"""
Plot embedding for both source and target domain using tSNE
:param Xs:
:param ys:
:param Xt:
:param yt:
:param n_components:
:param text:
:param save_path:
:return:
"""
Xs = Xs[: min(len(Xs), n_samples)]
ys = ys[: min(len(ys), n_samples)]
Xt = Xt[: min(len(Xt), n_samples)]
yt = yt[: min(len(Xt), n_samples)]
X = np.concatenate((Xs, Xt), axis=0)
tsne = manifold.TSNE(n_components=n_components)
X = tsne.fit_transform(X)
Xs = X[: len(Xs)]
Xt = X[len(Xs):]
plot_embedding_src_tgt(Xs, ys, Xt, yt, text, save_path, names=names)
示例13: main
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def main():
args = parse_args()
X, labels = np.loadtxt(args.embeddings_path), np.loadtxt(args.labels_path, dtype=np.str)
tsne = TSNE(n_components=2, n_iter=10000, perplexity=5, init='pca', learning_rate=200, verbose=1)
transformed = tsne.fit_transform(X)
y = set(labels)
labels = np.array(labels)
plt.figure(figsize=(20, 14))
colors = cm.rainbow(np.linspace(0, 1, len(y)))
for label, color in zip(y, colors):
points = transformed[labels == label, :]
plt.scatter(points[:, 0], points[:, 1], c=[color], label=label, s=200, alpha=0.5)
for p1, p2 in random.sample(list(zip(points[:, 0], points[:, 1])), k=min(1, len(points))):
plt.annotate(label, (p1, p2), fontsize=30)
plt.savefig('tsne_visualization.png', transparent=True, bbox_inches='tight', pad_inches=0)
plt.show()
示例14: plot
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def plot(args):
wc = pickle.load(open(os.path.join(args.data_dir, 'wc.dat'), 'rb'))
words = sorted(wc, key=wc.get, reverse=True)[:args.top_k]
if args.model == 'pca':
model = PCA(n_components=2)
elif args.model == 'tsne':
model = TSNE(n_components=2, perplexity=30, init='pca', method='exact', n_iter=5000)
word2idx = pickle.load(open('data/word2idx.dat', 'rb'))
idx2vec = pickle.load(open('data/idx2vec.dat', 'rb'))
X = [idx2vec[word2idx[word]] for word in words]
X = model.fit_transform(X)
plt.figure(figsize=(18, 18))
for i in range(len(X)):
plt.text(X[i, 0], X[i, 1], words[i], bbox=dict(facecolor='blue', alpha=0.1))
plt.xlim((np.min(X[:, 0]), np.max(X[:, 0])))
plt.ylim((np.min(X[:, 1]), np.max(X[:, 1])))
if not os.path.isdir(args.result_dir):
os.mkdir(args.result_dir)
plt.savefig(os.path.join(args.result_dir, args.model) + '.png')
示例15: plot_embedding
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import TSNE [as 别名]
def plot_embedding(embedding, annotation=None, filename='outputs/embedding.png'):
reduced = TSNE(n_components=2).fit_transform(embedding)
plt.figure(figsize=(20, 20))
max_x = np.amax(reduced, axis=0)[0]
max_y = np.amax(reduced, axis=0)[1]
plt.xlim((-max_x, max_x))
plt.ylim((-max_y, max_y))
plt.scatter(reduced[:, 0], reduced[:, 1], s=20, c=["r"] + ["b"] * (len(reduced) - 1))
# Annotation
if annotation:
for i in range(embedding.shape[0]):
target = annotation[i]
x = reduced[i, 0]
y = reduced[i, 1]
plt.annotate(target, (x, y))
plt.savefig(filename)
# plt.show()