我正在尝试使用scikit-learn和scikit-learn的数据集编写TSNE,但是在显示结果时,我需要真实的MNIST图像,而不是一些彩色的点/图。我正在使用matplotlib和seaborn

这是我的代码:

import sklearn
import seaborn as sb
import pandas as pd
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import fetch_mldata

mnist = fetch_mldata("MNIST original")
X = mnist.data / 255.0
y = mnist.target
feat_cols = [ 'pixel' + str(i) for i in range(X.shape[1]) ]
df = pd.DataFrame(X,columns=feat_cols)
df['y'] = y
df['label'] = df['y'].apply(lambda i: str(i))
X, y = None, None
np.random.seed(42)
rndperm = np.random.permutation(df.shape[0])

N= 520000
df_subset = df.loc[rndperm[:N],:].copy()
data_subset = df_subset[feat_cols].values
tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300)
tsne_results = tsne.fit_transform(data_subset)

df_subset['tsne-2d-one'] = tsne_results[:,0]
df_subset['tsne-2d-two'] = tsne_results[:,1]
plt.figure(figsize=(16,10))
sb.scatterplot(
     x="tsne-2d-one", y="tsne-2d-two",
     hue="y",
     palette=sb.color_palette("hls", 10),
     data=df_subset,
     legend="full",
     alpha=0.3
)

最佳答案

我不知道您是否可以对此图做出正面或反面的描述,但是如果我正确理解了您的问题,这就是您想要做的事情?

from matplotlib.offsetbox import OffsetImage, AnnotationBbox

pixel_cols = df_subset.columns.str.startswith('pixel')
img_w, img_h = 28,28
zoom = 0.5

fig, ax = plt.subplots(figsize=(16,10))
for i,row in df_subset.iterrows():
    image = row[pixel_cols].values.astype(float).reshape((img_w, img_h))
    im = OffsetImage(image, zoom=zoom)
    ab = AnnotationBbox(im, (row["tsne-2d-one"], row["tsne-2d-two"]), xycoords='data', frameon=False)
    ax.add_artist(ab)
    ax.update_datalim([(row["tsne-2d-one"], row["tsne-2d-two"])])
    ax.autoscale()


python - Matplotlib散布不同的图像(MNIST),而不是TSNE的图-LMLPHP

该代码基于Annotation Box demothis answer on SO

关于python - Matplotlib散布不同的图像(MNIST),而不是TSNE的图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59373626/

10-12 21:49