size = 24
n = size ** 2
for num in range(10):
x_data_n, y_data_n = shuffle(data[target==num], target[target==num], n_samples=int(n/2))
x_data_ntst, y_data_ntst = shuffle(Test_data[Test_target==num+10], Test_target[Test_target==num+10], n_samples=int(n/2))
x_data_np = np.concatenate([x_data_n, x_data_ntst])
y_data_np = np.concatenate([y_data_n, y_data_ntst])
x_pca_n = PCA(n_components=50).fit_transform(x_data_np/255)
embeddings_n = TSNE(perplexity=50, random_state=24680, verbose=0).fit_transform(x_pca_n)
embeddings_n = minmax_scale(embeddings_n)
#ここからくずし字散布図
fig, ax = plt.subplots(figsize=(16,16))
for pos, d, i in zip(embeddings_n, x_data_np.reshape((-1,28,28)), y_data_np):
img = colorize(d, colors[i//10], 0.7)
ab = offsetbox.AnnotationBbox(offsetbox.OffsetImage(img),0.03 + pos * 0.94,frameon=False)
ax.add_artist(ab)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
handles = [mlines.Line2D([0], [0], label=labels[i],
linewidth=0, marker='o', alpha=0.5,
markersize=7,markerfacecolor=colors[i//10],markeredgewidth=0)
for i in [num,num+10]]
ax.legend(loc='lower right',handles=handles)
plt.show()
#ここからグリッド表示部
grid_n = np.dstack(np.meshgrid(np.linspace(0, 1, size), np.linspace(0, 1, size))).reshape(-1, 2)
cost_n = cdist(grid_n, embeddings_n, 'sqeuclidean').astype('float32')
cost_n *= 1e7 / cost_n.max()
_, col_asses_n, _ = lapjv(cost_n)
grid_jv_n = grid_n[col_asses_n]
fig, ax = plt.subplots(figsize=(16,16))
for pos, d, i in zip(grid_jv_n, x_data_np.reshape((-1,28,28)), y_data_np):
img = colorize(d, colors[i//10], 0.85)
img = Image.fromarray(img,'RGBA').resize((35, 35), Image.ANTIALIAS)
ab = offsetbox.AnnotationBbox(offsetbox.OffsetImage(img),0.01 + pos * 0.98,frameon=False)
ax.add_artist(ab)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
ax.set_axis_off()
plt.show()