TypeError:“类型”对象在聚类期间不可下标

时间:2019-07-20 05:12:44

标签: python-3.x jupyter-notebook

我正在实现KMeans聚类算法,但遇到了这个问题,它在jupyter平台中不起作用。我正在应用弯头法找到最佳的簇数。

#Now find the optimal number of clusters using elbow method
from sklearn.cluster import KMeans
wcss = []
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 Clusters')
plt.ylabel('WCSS')
plt.show()

---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)
    <ipython-input-31-ebfededa579e> in <module>()
          2 from sklearn.cluster import KMeans
          3 wcss = []
    ----> 4 for i in range[1,11]:
          5     kmeans = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
          6     kmeans.fit(X)

    TypeError: 'type' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

该错误表明(或试图说)range是一种方法。因此,您需要这样称呼它:range(1, 11)而不是range[1, 11]

如果您在第四行中更改了它,它应该可以工作(至少在此部分中)。