学习测试代码
#输入层可视化
input_layer=x_test .reshape(1050,2048)
#-------------------------------tSNE降维分析--------------------------------
tsne = TSNE(n_components=2,init='pca',verbose = 1,random_state=402)
tsne_results = tsne.fit_transform(input_layer)
print(tsne_results.shape)
#-------------------------------可视化--------------------------------
#y_test_cat = np_utils.to_categorical(y_valid, num_classes = 10)# 总的类别
plt.figure()
for cl in range(7):# 总的类别
indices = np.where(color_map==cl)
indices = indices[0]
plt.scatter(tsne_results[indices,0], tsne_results[indices, 1], label=cl)
font = {'size': 10}
plt.legend(ncol=7,loc=(0.005,1.01),prop=font)
plt.xticks(size=12)
plt.yticks(size=12)
plt.xlabel('输入层',size=12)
plt.savefig('输入层.png',dpi=100,bbox_inches='tight')
plt.show()
这部分代码是用来可视化t-SNE降维后的数据点在二维空间的分布,以及不同类别的数据点如何分布的。以下是对代码的解释:
-
plt.figure()
:创建一个新的绘图窗口。 -
for cl in range(7):
:这个循环遍历了7个类别,假设您的数据集有7个不同的类别。 -
indices = np.where(color_map==cl)
:这一行代码找到了标签为cl
的数据点的索引。color_map
中存储了数据点的标签信息。 -
plt.scatter(tsne_results[indices,0], tsne_results[indices, 1], label=cl)
:这是绘制散点图的部分。它使用t-SNE降维后的结果,将属于不同类别的数据点用不同颜色的散点表示,label=cl
用于为每个类别指定标签。 -
font = {'size': 10}
:定义了字体大小。 -
plt.legend(ncol=7,loc=(0.005,1.01),prop=font)
:这是用于创建图例的代码,ncol
指定了列数,loc
指定了图例的位置,prop
定义了图例的属性,包括字体大小。 -
plt.xticks(size=12)
和plt.yticks(size=12)
:这两行代码用于设置x轴和y轴的刻度标签的字体大小。 -
plt.xlabel('输入层',size=12)
:这一行代码设置x轴的标签,即图表的x轴名称。 -
plt.savefig('输入层.png',dpi=100,bbox_inches='tight')
:这一行代码用于保存生成的图像,以PNG格式保存,并指定了分辨率和边界框。 -
plt.show()
:最后一行代码显示了绘制好的图形。
总体来说,这段代码的目的是绘制t-SNE降维后数据点的分布图,以便可视化数据在二维空间中的分布情况,并查看不同类别的数据点在降维后的空间中的分布情况。这有助于理解数据的结构和关系。
代码运行结果分析
_________________________________________________________________
[t-SNE] Computing 91 nearest neighbors...
[t-SNE] Indexed 1050 samples in 0.004s...
[t-SNE] Computed neighbors for 1050 samples in 0.083s...
[t-SNE] Computed conditional probabilities for sample 1000 / 1050
[t-SNE] Computed conditional probabilities for sample 1050 / 1050
[t-SNE] Mean sigma: 15.596957
D:\wangguowei\Anaconda3\envs\pytorch\lib\site-packages\sklearn\manifold\_t_sne.py:986: FutureWarning: The PCA initialization in TSNE will change to have the standard deviation of PC1 equal to 1e-4 in 1.2. This will ensure better convergence.
FutureWarning,
[t-SNE] KL divergence after 250 iterations with early exaggeration: 115.293976
[t-SNE] KL divergence after 1000 iterations: 2.062621
(1050, 2)
这部分代码展示了t-SNE降维的计算过程和结果。t-SNE是一种用于将高维数据降维到二维或三维空间以进行可视化的技术。以下是关于输出的解释:
-
[t-SNE] Computing 91 nearest neighbors...
:t-SNE算法正在计算每个数据点的最近邻。 -
[t-SNE] Indexed 1050 samples in 0.004s...
:算法已经建立了1050个样本的索引,这是t-SNE计算的初始阶段。 -
[t-SNE] Computed neighbors for 1050 samples in 0.083s...
:算法已经计算了1050个样本之间的邻近关系,这通常涉及到确定每个样本的最近邻。 -
[t-SNE] Computed conditional probabilities for sample 1000 / 1050
和[t-SNE] Computed conditional probabilities for sample 1050 / 1050
:t-SNE算法计算了每个样本之间的条件概率,这是t-SNE算法的一个关键步骤。 -
[t-SNE] Mean sigma: 15.596957
:这是计算的条件概率中的平均 sigma 值,sigma 是一个与数据点之间的距离有关的参数。 -
[t-SNE] KL divergence after 250 iterations with early exaggeration: 115.293976
和[t-SNE] KL divergence after 1000 iterations: 2.062621
:这些是 t-SNE 算法在迭代过程中的 KL 散度值,它是一个度量 t-SNE 降维质量的指标。通常,KL 散度值会在迭代过程中逐渐减小,直到趋于稳定。 -
(1050, 2)
:这是最终的 t-SNE 降维结果,表示将原始数据降维到了一个二维空间,其中每个样本用一个二维坐标表示。
在这种情况下,t-SNE成功将原始数据从高维度空间降维到了二维空间,这有助于可视化数据并展示数据点之间的相对位置。这通常用于数据探索和可视化,以便更好地理解数据的结构和关系。