在python中使用kmeans sklearn对数据点进行集群
我使用以下python代码使用kmeans对数据点进行集群在python中使用kmeans sklearn对数据点进行集群,python,matplotlib,machine-learning,k-means,Python,Matplotlib,Machine Learning,K Means,我使用以下python代码使用kmeans对数据点进行集群 data = np.array([[30, 17, 10, 32, 32], [18, 20, 6, 20, 15], [10, 8, 10, 20, 21], [3, 16, 20, 10, 17], [3, 15, 21, 17, 20]]) kmeans_clustering = KMeans( n_clusters = 3 ) idx = kmeans_clustering.fit_predict( data ) #use
data = np.array([[30, 17, 10, 32, 32], [18, 20, 6, 20, 15], [10, 8, 10, 20, 21], [3, 16, 20, 10, 17], [3, 15, 21, 17, 20]])
kmeans_clustering = KMeans( n_clusters = 3 )
idx = kmeans_clustering.fit_predict( data )
#use t-sne
X = TSNE(n_components=2).fit_transform( data )
fig = plt.figure(1)
plt.clf()
#plot graph
colors = np.array([x for x in 'bgrcmykbgrcmykbgrcmykbgrcmyk'])
plt.scatter(X[:,0], X[:,1], c=colors[kmeans_clustering.labels_])
plt.title('K-Means (t-SNE)')
plt.show()
然而,我得到的聚类图是错误的,因为我在一个点上得到了所有的东西。
因此,请让我知道我的代码哪里出错了?我想在散点图中分别查看kmeans群集
编辑
我得到的t-sne山谷如下
[[ 1.12758575e-04 9.30458337e-05]
[ -1.82559784e-04 -1.06657936e-04]
[ -9.56485652e-05 -2.38951623e-04]
[ 5.56515580e-05 -4.42453191e-07]
[ -1.42039677e-04 -5.62548119e-05]]
使用
TSNE
的complexity
参数。complexity
的默认值是30,这对于您的情况来说似乎太多了,尽管文档中指出TSNE
对该参数非常不敏感
这种困惑与其他流形学习算法中使用的最近邻数有关。更大的数据集通常需要更大的困惑。考虑选择一个介于5和50之间的值。选择不是非常关键,因为t-SNE对该参数非常不敏感
使用
TSNE
的complexity
参数。complexity
的默认值是30,这对于您的情况来说似乎太多了,尽管文档中指出TSNE
对该参数非常不敏感
这种困惑与其他流形学习算法中使用的最近邻数有关。更大的数据集通常需要更大的困惑。考虑选择一个介于5和50之间的值。选择不是非常关键,因为t-SNE对该参数非常不敏感
您也可以使用PCA(主成分分析)而不是t-SNE来绘制聚类:
import numpy as np
import pandas as pd
from sklearn.cluster import Kmeans
from sklearn.decomposition import PCA
data = np.array([[30, 17, 10, 32, 32], [18, 20, 6, 20, 15], [10, 8, 10, 20,
21], [3, 16, 20, 10, 17], [3, 15, 21, 17, 20]])
kmeans = KMeans(n_clusters = 3)
labels = kmeans.fit_predict(data)
pca = PCA(n_components=2)
data_reduced = pca.fit_transform(data)
data_reduced = pd.DataFrame(data_reduced)
ax = data_reduced.plot(kind='scatter', x=0, y=1, c=labels, cmap='rainbow')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_title('Projection of the clustering on a the axis of the PCA')
for x, y, label in zip(data_reduced[0], data_reduced[1], kmeans.labels_):
ax.annotate('Cluster {0}'.format(label), (x,y))
您也可以使用PCA(主成分分析)而不是t-SNE来绘制聚类:
import numpy as np
import pandas as pd
from sklearn.cluster import Kmeans
from sklearn.decomposition import PCA
data = np.array([[30, 17, 10, 32, 32], [18, 20, 6, 20, 15], [10, 8, 10, 20,
21], [3, 16, 20, 10, 17], [3, 15, 21, 17, 20]])
kmeans = KMeans(n_clusters = 3)
labels = kmeans.fit_predict(data)
pca = PCA(n_components=2)
data_reduced = pca.fit_transform(data)
data_reduced = pd.DataFrame(data_reduced)
ax = data_reduced.plot(kind='scatter', x=0, y=1, c=labels, cmap='rainbow')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_title('Projection of the clustering on a the axis of the PCA')
for x, y, label in zip(data_reduced[0], data_reduced[1], kmeans.labels_):
ax.annotate('Cluster {0}'.format(label), (x,y))
您看过t-SNE变换后的
X
值了吗?我无法重现您的问题。在给出的示例数据中,我得到了5个点,x和y值大致介于-200和200之间。您使用的是什么sklearn和numpy版本?@Evert非常感谢您的评论。我的numpy版本是1.13.0,sklearn版本是0.18.1。此外,我还更新了问题中的tsne值。你能告诉我我在哪里把代码弄错了吗?无法使用numpy 1.13.3和sklearn 0.19复制在版本0.19中,manifold.TSNE
有很多错误修复。尝试升级你的sklearn
版本。你看过t-SNE转换后X
的值吗?我无法重现你的问题。在给出的示例数据中,我得到了5个点,x和y值大致介于-200和200之间。您使用的是什么sklearn和numpy版本?@Evert非常感谢您的评论。我的numpy版本是1.13.0,sklearn版本是0.18.1。此外,我还更新了问题中的tsne值。你能告诉我我在哪里把代码弄错了吗?无法使用numpy 1.13.3和sklearn 0.19复制在版本0.19中,manifold.TSNE
有很多错误修复。请尝试升级您的sklearn
版本。非常感谢。你看到我的代码中还有其他缺陷吗?:)我不知道,其他部分都可以。非常感谢。你看到我的代码中还有其他缺陷吗?:)我不知道,其他部分还可以。