t-sne根据数据样本标签画三维图

在做数据训练分析时,用t-sne来做数据降维及可视化,当数据样本具有标签时,如何画图?
1、将读取的数据的形式转换为数组
2、根据t-sne对数据进行降维,根据需求确定维数。
3、三维数据,将降维后的数据连同标签转换到dataframe中
4、依据dataframe,画出三维散点图
样本有八类,根据每类的颜色不同确定数据的可视化

df = pd.DataFrame(t_sne.embedding_, columns=['x1', 'x2', 'x3'])
df['t'] = label

colors = ['b', 'r', 'g', 'c', 'm', 'y', 'k', 'orange']
Label_Com = ['0', '1', '2', '3', '4', '5', '6', '7']

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

for index in range(8):
    x1 = 10*df.loc[df['t'] == index]['x1']
    x2 = 10*df.loc[df['t'] == index]['x2']
    x3 = 10*df.loc[df['t'] == index]['x3']
    ax.scatter(x1, x2, x3, c=colors[index], s=60, cmap='brg', marker='o', depthshade=False)
ax.set_title('Scatter Plot for data')
ax.set_xlabel('X_label')
ax.set_xlim(-200, 200)
ax.set_ylabel('Y_label')
ax.set_ylim(-400, 400)
ax.set_zlabel('Z_label')
ax.set_zlim(-200, 200)
plt.savefig('fig.tif', bbox_inches='tight')
plt.show()
发布了5 篇原创文章 · 获赞 0 · 访问量 62

猜你喜欢

转载自blog.csdn.net/weixin_43448905/article/details/105501122