tsne可视化

文章介绍了如何使用Python中的sklearn库进行TSNE降维,以及如何结合PyTorch处理多模态数据(如文本和视觉特征),并通过matplotlib绘制二维和三维的散点图,展示不同特征之间的分布情况。
摘要由CSDN通过智能技术生成
import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold
from glob import glob
import nibabel as nib
import torch
from torch import nn
# 降成二维或三维,即61*768变成61*2或61*3,然后绘制出来,归一化看是否需要

# brats_tra = []
# brats_label = []
# brats_array = np.random.rand(60, 768) # [6200, 57600]
# brats_label_array = np.array([0, 1, 3] * 20)   # [6200] 标签01
# # 降维数据
# tsne = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(brats_array)
# # 归一化
# x_min, x_max = tsne.min(0), tsne.max(0)
# tsne_norm = (tsne - x_min) / (x_max - x_min)
# # 标签
# normal_idxs = (brats_label_array == 0)
# abnorm_idxs = (brats_label_array == 1)
# abnorm_idxs2 = (brats_label_array == 2)
# #
# tsne_normal = tsne_norm[normal_idxs]
# tsne_abnormal = tsne_norm[abnorm_idxs]
# tsne_abnormal2 = tsne_norm[abnorm_idxs2]
#
# # 绘图
# plt.figure(figsize=(8, 8))
# plt.scatter(tsne_normal[:, 0], tsne_normal[:, 1], 10, color='red', label='Healthy slices')
# # tsne_normal[i, 0]为横坐标,X_norm[i, 1]为纵坐标,1为散点图的面积, color给每个类别设定颜色
# plt.scatter(tsne_abnormal[:, 0], tsne_abnormal[:, 1], 10, color='green', label='Anomalous slices')
# plt.scatter(tsne_abnormal2[:, 0], tsne_abnormal2[:, 1], 10, color='blue', label='Anomalous slices')
# plt.legend(loc='upper left')
# plt.show()
# 提取
a=0
t= torch.load('/home/huang/代码/DeepKE/example/re/multimodal/utterance_t.txt').cpu().detach().numpy()
v= torch.load('/home/huang/代码/DeepKE/example/re/multimodal/utterance_v.txt').cpu().detach().numpy()
c_t= torch.load('/home/huang/代码/DeepKE/example/re/multimodal/utt_shared_t.txt').cpu().detach().numpy()
c_v= torch.load('/home/huang/代码/DeepKE/example/re/multimodal/utt_shared_v.txt').cpu().detach().numpy()
s_t= torch.load('/home/huang/代码/DeepKE/example/re/multimodal/utt_private_t.txt').cpu().detach().numpy()
s_v= torch.load('/home/huang/代码/DeepKE/example/re/multimodal/utt_private_v.txt').cpu().detach().numpy()
# 2
tsne = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(t[a])
tsne1 = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(v[a])
tsne2 = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(c_t[a])
tsne3 = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(c_v[a])
tsne4 = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(s_t[a])
tsne5 = manifold.TSNE(n_components=2, init='pca', random_state=42).fit_transform(s_v[a])
# 3
_tsne = manifold.TSNE(n_components=3, init='pca', random_state=42).fit_transform(t[a])
_tsne1 = manifold.TSNE(n_components=3, init='pca', random_state=42).fit_transform(v[a])
_tsne2 = manifold.TSNE(n_components=3, init='pca', random_state=42).fit_transform(c_t[a])
_tsne3 = manifold.TSNE(n_components=3, init='pca', random_state=42).fit_transform(c_v[a])
_tsne4 = manifold.TSNE(n_components=3, init='pca', random_state=42).fit_transform(s_t[a])
_tsne5 = manifold.TSNE(n_components=3, init='pca', random_state=42).fit_transform(s_v[a])
# 绘制2d
plt.figure(figsize=(8, 8))
# plt.scatter(tsne[:, 0], tsne[:, 1], 10, color='red', label='t')
# # tsne_normal[i, 0]为横坐标,X_norm[i, 1]为纵坐标,1为散点图的面积, color给每个类别设定颜色
plt.scatter(tsne1[:, 0], tsne1[:, 1], 5, color='black', label='v')
plt.scatter(tsne2[:, 0], tsne2[:, 1], 5, color='blue', label='c_t')
plt.scatter(tsne3[:, 0], tsne3[:, 1], 5, color='green', label='c_v')
plt.scatter(tsne4[:, 0], tsne4[:, 1], 5, color='orange', label='s_t')
plt.scatter(tsne5[:, 0], tsne5[:, 1], 5, color='purple', label='s_v')
plt.legend(loc='upper left')
plt.show()

# 绘制3d
# 创建一个3D散点图对象
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 在3D散点图中绘制数据点
# ax.scatter(_tsne[:, 0], _tsne[:, 1],_tsne[:, 2],s=10, color='red', label='Healthy slices')
ax.scatter(_tsne1[:, 0], _tsne1[:, 1],_tsne1[:, 2],s=10, color='black', label='Healthy slices')
ax.scatter(_tsne2[:, 0], _tsne2[:, 1],_tsne2[:, 2],s=10, color='blue', label='Healthy slices')
ax.scatter(_tsne3[:, 0], _tsne3[:, 1],_tsne3[:, 2],s=10, color='green', label='Healthy slices')
ax.scatter(_tsne4[:, 0], _tsne4[:, 1],_tsne4[:, 2],s=10, color='orange', label='Healthy slices')
ax.scatter(_tsne5[:, 0], _tsne5[:, 1],_tsne5[:, 2],s=10, color='purple', label='Healthy slices')

# 显示2d图形
ax.legend(loc='upper left')
plt.show()
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值