T_SNE可视化中间层

解析一下一些画图的代码:

plt.figure(figsize=(14, 14))
for idx, fruit in enumerate(class_list): # 遍历每个类别
    # 获取颜色和点型
    color = palette[idx]
    marker = marker_list[idx%len(marker_list)]

    # 找到所有标注类别为当前类别的图像索引号
    indices = np.where(df[show_feature]==fruit)
    plt.scatter(X_tsne_2d[indices, 0], X_tsne_2d[indices, 1], color=color, marker=marker, label=fruit, s=150)

plt.legend(fontsize=16, markerscale=1, bbox_to_anchor=(1, 1))
plt.xticks([])
plt.yticks([])
plt.savefig('语义特征t-SNE二维降维可视化.pdf', dpi=300) # 保存图像
plt.show()
这段代码是用来绘制一个散点图的。它使用了 `matplotlib` 库中的 `pyplot` 模块,简称 `plt`。

- `plt.figure(figsize=(14, 14))`:创建一个大小为 14 x 14 的画布。
- `for idx, fruit in enumerate(class_list):`:遍历 `class_list` 中的每个类别。
- `color = palette[idx]`:获取当前类别对应的颜色。
- `marker = marker_list[idx%len(marker_list)]`:获取当前类别对应的点型。
- `indices = np.where(df[show_feature]==fruit)`:找到所有标注类别为当前类别的图像索引号。
- `plt.scatter(X_tsne_2d[indices, 0], X_tsne_2d[indices, 1], color=color, marker=marker, label=fruit, s=150)`:
在画布上绘制散点图,其中横坐标为 `X_tsne_2d[indices, 0]`,纵坐标为 `X_tsne_2d[indices, 1]`,颜色为 `color`,点型为 `marker`,标签为 `fruit`,点的大小为 150- `plt.legend(fontsize=16, markerscale=1, bbox_to_anchor=(1, 1))`:添加图例,字体大小为 16,图例中点的大小与原图相同,图例位置在画布右上角。 - `plt.xticks([])` 和 `plt.yticks([])`:去掉 x 轴和 y 轴的刻度。 - `plt.savefig('语义特征t-SNE二维降维可视化.pdf', dpi=300)`:保存图像为 PDF 格式,分辨率为 300 dpi。 - `plt.show()`:显示图像。
for idx, fruit in enumerate(class_list):
`enumerate()` 是一个内置函数,它可以将一个可迭代对象(如列表)转换为一个枚举对象。这个枚举对象中的每个元素都是一个元组,包含两个元素:索引号和原可迭代对象中对应的元素。

在这一行代码中,`for idx, fruit in enumerate(class_list):` 遍历 `class_list` 中的每个元素。`idx` 是当前元素在 `class_list` 中的索引号,`fruit` 是当前元素的值。

例如,如果 `class_list = ['apple', 'banana', 'cherry']`,那么第一次循环时,`idx = 0`,`fruit = 'apple'`;第二次循环时,`idx = 1`,`fruit = 'banana'`;第三次循环时,`idx = 2`,`fruit = 'cherry'`。
这一行代码中,`marker = marker_list[idx%len(marker_list)]` 是用来获取当前类别对应的点型的。

`idx` 是当前类别在 `class_list` 中的索引号,`len(marker_list)` 是 `marker_list` 的长度。`idx % len(marker_list)` 是 `idx` 除以 `len(marker_list)` 的余数,它取值范围为 `0` 到 `len(marker_list)-1`。

因此,`marker_list[idx%len(marker_list)]` 就是 `marker_list` 中索引号为 `idx % len(marker_list)` 的元素,也就是当前类别对应的点型。

例如,如果 `marker_list = ['o', 's', '^']`,那么当 `idx = 0` 时,`idx % len(marker_list) = 0`,所以 `marker = marker_list[0] = 'o'`;
当 `idx = 1` 时,`idx % len(marker_list) = 1`,所以 `marker = marker_list[1] = 's'`;当 `idx = 2` 时,`idx % len(marker_list) = 2`,所以 `marker = marker_list[2] = '^'`;
当 `idx = 3` 时,由于 `3 % 3 = 0`,所以又回到了第一个点型 `'o'`。 希望这些信息能够帮助你理解这一行代码。😊
`indices = np.where(df[show_feature]==fruit)` 是用来找到所有标注类别为当前类别的图像索引号的。

`df` 是一个 `DataFrame` 对象,它表示一个二维表格。`show_feature` 是一个字符串,表示 `df` 中的某一列的列名。因此,`df[show_feature]` 就是 `df` 中名为 `show_feature` 的那一列。

`fruit` 是当前类别的值。因此,`df[show_feature]==fruit` 是一个布尔数组,它表示 `df[show_feature]` 中每个元素是否等于 `fruit`。

`np.where()` 是一个函数,它可以返回输入数组中所有非零元素的索引号。在这里,它返回的就是 `df[show_feature]==fruit` 中所有值为 `True` 的元素的索引号,也就是所有标注类别为当前类别的图像索引号。

例如,如果 `df = pd.DataFrame({'A': [1, 2, 3], 'B': ['x', 'y', 'x']})`,那么当 `show_feature = 'B'` 且 `fruit = 'x'` 时,`df[show_feature] == fruit` 的结果为 `[True, False, True]`,
所以 `np.where(df[show_feature] == fruit)` 的结果为 `(array([0, 2]),)`,也就是说,第 0 行和第 2 行的标注类别为 `'x'`。
plt.scatter(X_tsne_2d[indices, 0], X_tsne_2d[indices, 1], color=color, marker=marker, label=fruit, s=150):是用来在画布上绘制散点图的。

`X_tsne_2d` 是一个二维数组,它表示 t-SNE 降维后的数据。`indices` 是一个一维数组,它表示所有标注类别为当前类别的图像索引号。
因此,`X_tsne_2d[indices, 0]` 就是 `X_tsne_2d` 中第 `indices` 行第 0 列的元素,也就是所有标注类别为当前类别的图像的横坐标;
`X_tsne_2d[indices, 1]` 就是 `X_tsne_2d` 中第 `indices` 行第 1 列的元素,也就是所有标注类别为当前类别的图像的纵坐标。 `color` 是当前类别对应的颜色,`marker` 是当前类别对应的点型,`label=fruit` 表示这些点的标签为 `fruit`,也就是当前类别的值,`s=150` 表示点的大小为 150。 例如,如果 `X_tsne_2d = np.array([[1, 2], [3, 4], [5, 6]])`,那么当 `indices = np.array([0, 2])` 时,`X_tsne_2d[indices, 0] = [1, 5]`,所以这些点的横坐标为 1 和 5;
同理,纵坐标为 `X_tsne_2d[indices, 1] = [2, 6]`。
`plt.legend(fontsize=16, markerscale=1, bbox_to_anchor=(1, 1))` 是用来添加图例的。

`fontsize=16` 表示图例中文字的大小为 16,`markerscale=1` 表示图例中点的大小与原图相同,`bbox_to_anchor=(1, 1)` 表示图例的位置在画布右上角。

例如,如果画布上有两种颜色的点,分别表示 `'apple'` 和 `'banana'`,那么添加图例后,画布右上角会出现一个方框,里面有两行文字,分别为 `'apple'` 和 `'banana'`,
每行文字前面都有一个与原图相同大小的点,颜色分别对应 `'apple'` 和 `'banana'`。

 

posted @ 2023-05-14 15:09  灵性  阅读(194)  评论(0)    收藏  举报