用T-SNE算法和Scatter方法使高维数据可视化
import random
from mpmath import arange
import numpy as np
import pickle as Pickle
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
zx = np.array(pd.read_csv("dplb_zx.csv"))
zy = np.array(pd.read_csv("dplb_zy.csv"))
Y = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, \
10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, \
17, 17, 18, 18, 19, 19]
def tsenVisual(big,end):
arr = np.empty(shape=(40, 80))
i = 0
for item in range(big, end):
arr[i] = zx[item]
i += 1
arr[i] = zy[item]
i += 1
return arr
arr = tsenVisual(20,40)
X_embedded = TSNE(n_components=2,init="pca").fit_transform(arr)
#为40个点配置颜色 两两一组,需要二十种颜色
def get_color(labels):
#colors=["r","b","g","y","o"]
colors=["dimgray","darkorange","tan","silver","forestgreen",\
"darkgreen","royalblue","navy","red","darksalmon",\
"peru","olive",\
"yellow","cyan","mediumaquamarine","skyblue","purple","fuchsia",\
"indigo","khaki"]
color=[]
for i in range(len(labels)):
color.append(colors[labels[i]])
return color
figure=plt.figure(figsize=(5,5),dpi=1200)
color=get_color(Y)#为40个点配置颜色
x=X_embedded[:,0]#横坐标
y=X_embedded[:,1]#纵坐标
#关闭坐标刻度
plt.xticks([])
plt.yticks([])
#关闭坐标轴
plt.axis('on')
#显示子图边框
plt.box(on=True)
#绘制散点图。
plt.scatter(x,y,color=color)
plt.savefig("dplb20-40.png")
plt.show()
引用:https://blog.csdn.net/qq_43391414/article/details/120666927