【发布时间】:2020-10-04 01:23:07
【问题描述】:
我在尝试在 matplotlib 上绘制集群时遇到了这个问题。
# Training the K-Means model on the dataset
kmeans = KMeans(n_clusters = 4, init = 'k-means++', random_state = 42)
y_kmeans = kmeans.fit_predict(X)
#X.shape is (767135, 37) (It has undergone One Hot Encoding)
# Visualising the clusters
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 50, c = 'red',alpha = 0.3, label = 'Cluster 1')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 50, c = 'blue',alpha = 0.3, label = 'Cluster 2')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 50, c = 'green',alpha = 0.3, label = 'Cluster 3')
plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 50, c = 'cyan',alpha = 0.3, label = 'Cluster 4')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 300, c = 'yellow', label = 'Centroids')
plt.title('Clusters of customers')
plt.xlabel('Nationality')
plt.ylabel('Total Spending')
plt.legend()
plt.show()
运行代码时遇到这个错误:
TypeError: float() argument must be a string or a number, not 'csr_matrix'
ValueError: setting an array element with a sequence.
【问题讨论】:
-
您可以包含您的导入语句吗?
标签: python matplotlib machine-learning data-science k-means