本文整理汇总了Python中sklearn.manifold.TSNE.fit_transform方法的典型用法代码示例。如果您正苦于以下问题:Python TSNE.fit_transform方法的具体用法?Python TSNE.fit_transform怎么用?Python TSNE.fit_transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.manifold.TSNE
的用法示例。
在下文中一共展示了TSNE.fit_transform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def main():
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
dbn = DBN([1000, 750, 500], UnsupervisedModel=AutoEncoder)
# dbn = DBN([1000, 750, 500, 10])
output = dbn.fit(Xtrain, pretrain_epochs=2)
print "output.shape", output.shape
# sample before using t-SNE because it requires lots of RAM
sample_size = 600
tsne = TSNE()
reduced = tsne.fit_transform(output[:sample_size])
plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain[:sample_size], alpha=0.5)
plt.title("t-SNE visualization")
plt.show()
# t-SNE on raw data
reduced = tsne.fit_transform(Xtrain[:sample_size])
plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain[:sample_size], alpha=0.5)
plt.title("t-SNE visualization")
plt.show()
pca = PCA()
reduced = pca.fit_transform(output)
plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain, alpha=0.5)
plt.title("PCA visualization")
plt.show()
示例2: vizualize_clusters
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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")
示例3: tsne
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def tsne(similarity, euclid=False, perplexity=30):
if euclid:
model = TSNE(learning_rate=100, perplexity=perplexity, n_iter=200000)
result = model.fit_transform(similarity)
else:
model = TSNE(learning_rate=100, n_iter=100000, init='random', metric='precomputed')
result = model.fit_transform(1 - similarity)
return result.T
示例4: MyTSNE
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def MyTSNE(train,test):
#MyTSNE(train.iloc[:100,:],test.iloc[:20,:])
model = TSNE(n_components=2, random_state=0)
a = np.vstack(
[train.values,
test.values]
)
model.fit_transform(a)
return
示例5: programmer_5
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def programmer_5(data_zs, r):
# 进行数据降维
tsne = TSNE()
tsne.fit_transform(data_zs)
tsne = pd.DataFrame(tsne.embedding_, index=data_zs.index)
# 不同类别用不同颜色和样式绘图
d = tsne[r[u'聚类类别'] == 0]
plt.plot(d[0], d[1], 'r.')
d = tsne[r[u'聚类类别'] == 1]
plt.plot(d[0], d[1], 'go')
d = tsne[r[u'聚类类别'] == 2]
plt.plot(d[0], d[1], 'b*')
plt.show()
示例6: learn_embedding
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def learn_embedding(precompute_metric=False, use_saved=False):
base = 'datasets/dspace_topics'
new_base = 'datasets/dspace_embeddings'
# Delete previous saved embedding
if os.path.exists(new_base):
shutil.rmtree(new_base)
os.makedirs(new_base)
print 'Embedding: Extracting topics'
# choose a random subset of documents
filename_vec = os.listdir(base)
subsample = 5000
filename_vec = np.random.choice(filename_vec, subsample)
topic_vec = []
for filename in tqdm(filename_vec):
path = os.path.join(base, filename)
with open(path) as f:
d = json.load(f)
topics = d['topics']
topic_vec.append(topics)
print 'Embedding: Computing pairwise distances'
if precompute_metric:
if use_saved:
with open('metric.npy') as f:
metric = np.load(f)
else:
metric = pairwise_distances(np.array(topic_vec), metric=KL, n_jobs=-1)
with open('metric.npy', 'w') as f:
np.save(f, metric)
print 'Embedding: Learning embedding'
tsne = TSNE(n_iter=1000, verbose=10, metric='precomputed')
y = tsne.fit_transform(metric)
else:
print 'Embedding: Learning embedding'
tsne = TSNE(n_iter=1000, verbose=10)
y = tsne.fit_transform(topic_vec)
print 'Embedding: Saving embedding'
for (index, filename) in tqdm(enumerate(filename_vec), total=len(filename_vec)):
path = os.path.join(base, filename)
with open(path, 'r') as f:
d = json.load(f)
d['embedding'] = list(y[index])
new_path = os.path.join(new_base, filename)
with open(new_path, 'w') as new_f:
json.dump(d, new_f)
示例7: main
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def main():
embedding = WordEmbedding(embeddingpath(default_embeddingconfig))
for old, new in spelling_changes:
print(old, '--', new)
print(embedding.nearest_words([old]))
print()
print()
war, ist = tense_changes[0]
tensediff = embedding[ist] - embedding[war]
for past, present in tense_changes[1 : ]:
print(past, '+ tensediff:', *embedding.nearest_words([embedding[past] + tensediff]))
print('Should be:', present)
print()
# word_diffs = [embedding[new] - embedding[old] for (old, new) in word_changes]
spelling_diffs = [embedding[new] - embedding[old] for (old, new) in spelling_changes[10 : 20]]
tense_diffs = [embedding[present] - embedding[past] for (past, present) in tense_changes]
def metric(u, v):
return max(distance.cosine(u, v), 0)
while True:
try:
model = TSNE(n_components=2, metric=metric)
reduced = model.fit_transform(spelling_diffs + tense_diffs)
print(reduced)
return
except Exception:
pass
示例8: visualize_latent_rep
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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()
示例9: plotly_js_viz
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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)])
示例10: make_tsne_plot
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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)
示例11: plot_data
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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')
示例12: perform_AE
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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)
示例13: t_sne_view
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
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)
示例14: topic_dimen_reduce
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def topic_dimen_reduce(words, word2vec):
dictionary, matrix = terms_analysis.get_words_matrix(words, word2vec)
pca = PCA(n_components=50)
pca_matrix = pca.fit_transform(matrix)
tsne = TSNE(n_components=2)
t_matrix = tsne.fit_transform(pca_matrix)
return dictionary, t_matrix
示例15: apply_tSNE30
# 需要导入模块: from sklearn.manifold import TSNE [as 别名]
# 或者: from sklearn.manifold.TSNE import fit_transform [as 别名]
def apply_tSNE30(proj_data, proj_weights=None):
model = TSNE(n_components=2, perplexity=30.0, metric="euclidean",
learning_rate=100, early_exaggeration=4.0,
random_state=RANDOM_SEED);
norm_data = normalize_columns(proj_data);
result = model.fit_transform(norm_data.T);
return result;