紧接上一个博客中将文件信息转化为.csv文件并用于观察特征的可视化,这里列举一个代码用于将特征进行可视化。这里是一个T-SNE方法,具体原理代码最后会给链接,自行学习。
mport matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.manifold import TSNE
import seaborn as sns
""" feature
"""
encoding_array = np.load("ext-CUB.npy", allow_pickle=True)
array = np.load("ext-CUB-proto.npy", allow_pickle=True)
array = np.concatenate((encoding_array, array), axis=0)
""" prediction
"""
df = pd.read_csv("CUB.csv")
marker_list = ["1", "o", "2", "s", "3", "p", "4", "*", "+", "X", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
class_list = np.unique(df["picture_names"])
n_class = len(class_list)
print(class_list)
trans = TSNE(n_components=2, n_iter=2000, random_state=3000)
X_trans_2d = trans.fit_transform(array)
print(X_trans_2d.shape)
show_feature = "picture_names"
plt.figure(figsize=(14, 14))
for idx, fruit in enumerate(class_list):
marker = marker_list[idx % len(marker_list)]
indices = np.where(df["picture_names"] == fruit)
if idx % 2 == 0:
s = 150
else:
s = 700
if idx == 0:
color = "red"
if idx == 1:
color = "red"
if idx == 2:
color = "blue"
if idx == 3:
color = "blue"
if idx == 4:
color = "y"
if idx == 5:
color = "y"
if idx == 6:
color = "m"
if idx == 7:
color = "m"
if idx == 8:
color = "c"
if idx == 9:
color = "c"
plt.scatter(X_trans_2d[indices, 0], X_trans_2d[indices, 1], color=color, marker=marker, label=fruit, s=s)
plt.legend(fontsize=16, markerscale=1, bbox_to_anchor=(1, 1))
plt.xticks([])
plt.yticks([])
plt.savefig("ext-CUB.pdf", dpi=300)
print("it is over !")
T-SNE算法解释.
不同点颜色的表示