【问题标题】:TSNE - 3D graphTSNE - 3D 图
【发布时间】:2019-02-09 02:14:15
【问题描述】:

将数据 ncollwcoll 想象为 4000 个随机数。

我想通过 TSNE 运行它们,然后创建一个 3 维绘图​​。

如果我绘制它,我最终会得到一个 2D 图形,所以出了点问题,但我不完全确定是什么。

最终我想在同一个 3D 图表上用红色绘制前半部分,用蓝色绘制第二部分。

print(__doc__)

from time import time

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import NullFormatter

from sklearn import manifold
from sklearn.utils import check_random_state

c = 1000
 # Open File
ncoll_fn = "C:/Users/xxlassi/Downloads/trajectory_demo/trajectory_270252769939974_run__uid_-1808183947_tag_collision_0.0.txt"
wcoll_fn = "C:/Users/xxlassi/Downloads/trajectory_demo/trajectory_271551342150600_run__uid_-918721219_tag_collision_0.01.txt"

ncoll = []
wcoll = []

with open( ncoll_fn ) as f:
    ncoll = [ np.array([ float(el) for el in line.strip().split(',') ]) for line in f.readlines() ]
    ncoll = np.array( ncoll )

with open( wcoll_fn ) as f:
    wcoll = [ np.array([ float(el) for el in line.strip().split(',') ]) for line in f.readlines() ]
    wcoll = np.array( wcoll )

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

arr = np.concatenate((wcoll, ncoll), axis=0)
mid = int(len(arr)/2)
print (mid)

tsne = manifold.TSNE(n_components=3, init='pca',random_state=0, perplexity= 30, n_iter=5000)
trans_data = tsne.fit_transform(arr)

ax.scatter(arr[:,0][0:mid], arr[:,1][0:mid], c= 'r')
ax.scatter(arr[:,0][mid:], arr[:,1][mid:], c= 'b')

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.title("t-SNE")
plt.axis('tight')
plt.show()

【问题讨论】:

    标签: python python-3.x numpy matplotlib scikit-learn


    【解决方案1】:

    您需要使用 3d 散点图绘制 trans_data,而不是绘制 t-SNE 转换的数据:

    trans_data = tsne.fit_transform(arr)
    
    ax.scatter(trans_data[:,0][0:mid], trans_data[:,1][0:mid], trans_data[:,2][0:mid], c= 'r', s = 100, marker='+')
    ax.scatter(trans_data[:,0][mid:], trans_data[:,1][mid:], trans_data[:,2][mid:], c= 'b', s = 100, marker='.')
    

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2016-08-20
      • 2019-12-12
      • 2020-05-25
      • 1970-01-01
      相关资源
      最近更新 更多