TypeError:无法散列的类型:'numpy.ndarray'(k表示集群)

时间:2019-02-27 19:25:17

标签: python-3.x numpy cluster-analysis k-means

我的数据集是一个140 * 140的相关矩阵,显示了相同的140个变量之间的关系。我使用了弯头法来找到最大的簇数。我收到的错误是

  

res = cache.get(item)

     

TypeError:不可哈希类型:'numpy.ndarray'

 # K-means clustering
 # Using the elbow method to find the maximum number of clusters

from sklearn.cluster import KMeans
        wcss = []

for i in range(1,11):

    kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 40)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)

plt.plot(range(1,11),wcss)

plt.title('The Elbow Method')

plt.xlabel('Number of clusters')

plt.ylabel('wcss')

plt.show()
#Applying K-means to my Data set


kmeans = KMeans(n_clusters = 4, init = 'k-means++', random_state = 40)
y_kmeans = kmeans.fit_predict(X)

# Visualizing the clusters

plt.scatter(X[y_kmeans == 0,0], X[y_kmeans == 0,1], s = 10, c = 'red', label = 'Cluster 1')

plt.scatter(X[y_kmeans == 1,0], X[y_kmeans == 1,1], s = 10, c = 'blue', label = 'Cluster 2')

plt.scatter(X[y_kmeans == 2,0], X[y_kmeans == 2,1], s = 10, c = 'green', label = 'Cluster 3')

plt.scatter(X[y_kmeans == 3,0], X[y_kmeans == 3,1], s = 10, c = 'green', label = 'Cluster 4')


plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 30, c ='yellow', label = 'Centroids')



 plt.title('K-Means Clustering of Inbreds')


    plt.xlabel('X')

    plt.ylabel('Y')

    plt.legend()

    plt.show()

0 个答案:

没有答案