将数据ncoll
和wcoll
想象成4000随机数。
我想通过TSNE运行它们,然后创建一个3D图。
如果我对此进行绘图,则最终得到的是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()
答案 0 :(得分:0)