【问题标题】:What does these lines of codes in K_means clustering means?K_means聚类中的这些代码行是什么意思?
【发布时间】:2021-07-09 15:46:12
【问题描述】:

我正在学习 K-means 聚类。并且对plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')的工作感到很困惑X[y_kmeans == 0, 0], X[y_kmeans == 0, 1]在代码中的作用是什么?

这里有完整的代码

#k-means

#importing libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#importing the dataset
dataset = pd.read_csv("mall_customers.csv")
X = dataset.iloc[:,[3,4]].values

#using the elbow method to find the optimal number of clusters
from sklearn.cluster import KMeans
wcss = [] #Within-Cluster Sum of Square

for i in range(1,11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++',max_iter = 300,n_init=10,random_state = 0)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)

plt.plot(range(1,11),wcss)
plt.title("The elbow method")
plt.xlabel("Number of cluster")
plt.ylabel('Wcss') 
plt.show()    

#applying kmeans to all dataset
kmeans = KMeans(n_clusters = 5,init = 'k-means++', max_iter=300,n_init=10,random_state=0)
y_kmeans = kmeans.fit_predict(X)

#Visualising the cluster
plt.scatter(X[y_kmeans == 0,0],X[y_kmeans == 0,1],s=100,c = 'red' ,label='Cluster1')
plt.scatter(X[y_kmeans == 1,0],X[y_kmeans == 1,1],s=100,c='blue', label='Cluster2')
plt.scatter(X[y_kmeans == 2,0],X[y_kmeans == 2,1],s=100,c='green',label='Cluster3')
plt.scatter(X[y_kmeans == 3,0],X[y_kmeans == 3,1],s=100, c ='cyan',label = 'CLuster4')
plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')
plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],s=300, c = 'yellow', label ='Centroids')

plt.title('Clusters of customers')
plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()

我已添加输出图像以供参考 elbow graph, Final cluster image

【问题讨论】:

  • 这是一个过滤器。 y_kmeans == 0 选择 y_kmeans[i] 等于 0 的那些元素。X[y_kmeans == 0, 0] 选择 X 中对应的 y_kmeans 值为 0 且第二维为 0 的元素。
  • X[y_kmeans == 0,1] 是什么意思?

标签: python machine-learning scikit-learn k-means


【解决方案1】:

这是一个过滤器。 y_kmeans == 0 选择 y_kmeans[i] 等于 0 的那些元素。X[y_kmeans == 0, 0] 选择 X 中对应的 y_kmeans 值为 0 且第二维为 0 的元素。

最初由tim roberts回答

X[y_hc ==1,0] 这里 0 表示模型在 x 平面 X[y_hc == 0,1] 表示模型在 y 平面。 其中 1 指的是 [i] 的值或集群值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多