Python 高维数据降维聚类显示

先用 t-SNE 将数据进行降维,然后再依据聚类,做完聚类的结果在降维数据展示就可以了。

from sklearn import manifold
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np

# 这个数据就是处理的数据,一共33118个,每个是20维数据
np.array(x).shape
(33118, 20)

# 将数据降维,降维出来就是X_tsne
tSNE = manifold.TSNE(n_components=2, init='pca', random_state=0)
X_tsne = tSNE.fit_transform(x)

# 将原来的数据进行聚类,斌知道是哪一类的
kmeans = KMeans(n_clusters=18, max_iter=100, init="k-means++").fit(x)
result = kmeans.predict(x)

# 画一下
plt.figure(figsize=(8,8))
plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=result[:], s=1)

就觉在这个图还蛮好看的
在这里插入图片描述

posted @ 2022-10-12 16:26  赫凯  阅读(70)  评论(0)    收藏  举报