Python - 肘部图表问题
Python - Elbow Chart Issue
我正在尝试绘制 Elbow 图表以获得理想的簇数,我有这个:
data = dataset[['A','B','C','D','E','F','G','H']]
distortions = []
K = range(1, 10)
for k in K:
kmeanModel = KMeans(n_clusters = k).fit(data)
kmeanModel.fit(data)
distortions.append(sum(np.min(cdist(data, kmeanModel.cluster_centers_, 'euclidean'), axis=1)) / data.shape[0])
plt.plot(data, distortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Disortotion')
plt.title('Elbow')
plt.show()
当我 运行 此代码时,出现以下错误:
File "C:\Users\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\matplotlib\axes\_base.py", line 243, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
builtins.ValueError: x and y must have same first dimension, but have shapes (122607, 8) and (9,)
这个错误是什么意思?我该如何解决?
谢谢!
在 plt.plot(data, distortions, 'bx-')
中,您将 x
指定为 data
,这是 k-means 的原始数据帧。它实际上应该是簇数:
plt.plot(K, distortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Disortotion')
plt.title('Elbow')
plt.show()
我正在尝试绘制 Elbow 图表以获得理想的簇数,我有这个:
data = dataset[['A','B','C','D','E','F','G','H']]
distortions = []
K = range(1, 10)
for k in K:
kmeanModel = KMeans(n_clusters = k).fit(data)
kmeanModel.fit(data)
distortions.append(sum(np.min(cdist(data, kmeanModel.cluster_centers_, 'euclidean'), axis=1)) / data.shape[0])
plt.plot(data, distortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Disortotion')
plt.title('Elbow')
plt.show()
当我 运行 此代码时,出现以下错误:
File "C:\Users\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\matplotlib\axes\_base.py", line 243, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
builtins.ValueError: x and y must have same first dimension, but have shapes (122607, 8) and (9,)
这个错误是什么意思?我该如何解决?
谢谢!
在 plt.plot(data, distortions, 'bx-')
中,您将 x
指定为 data
,这是 k-means 的原始数据帧。它实际上应该是簇数:
plt.plot(K, distortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Disortotion')
plt.title('Elbow')
plt.show()