KMeans聚类的轮廓分析

时间:2017-08-13 15:17:31

标签: python numpy cluster-analysis k-means silhouette

如何解决此错误?为什么代码无法可视化情节?

TypeError: 'numpy.ndarray' object is not callable

输入

data_sillet = np.loadtxt('clustering_sillet.csv', delimiter=',')
data = data_sillet.astype(int)
data

输出

array([ 2,  3,  8, 29, 32, 15, 23,  0,  4, 16, 14, 15,  1,  5,  1,  8,  0,
       30, 14,  0,  1,  4,  5,  2, 12,  1,  7,  2,  0,  3, 22,  7,  1,  5,
        0,  6,  0, 15,  9, 24, 24,  5,  0,  0, 24, 11,  9,  0,  0,  1, 18,
       11,  4,  8, 27,  1,  3,  4,  7, 17,  5,  0,  1, 16])

输入

data =data(n_samples=64,n_features=1,centers=3,cluster_std=0.5,random_state=101)    
plt.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap='rainbow')

输出

TypeError                                 Traceback (most recent call last)
<ipython-input-89-0b73209258a0> in <module>()
----> 1 data =data(n_samples=64,n_features=1,centers=3,cluster_std=0.5,random_state=101)
      2 
      3 plt.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap='rainbow')

TypeError: 'numpy.ndarray' object is not callable

1 个答案:

答案 0 :(得分:0)

您似乎在此行中调用了“数据”而不是“KMeans”:

data =data(n_samples=64,n_features=1,centers=3,cluster_std=0.5,ra‌​ndom_state=101)

修正:

from sklearn.cluster import KMeans 
data =KMeans(n_samples=64,n_features=1,centers=3,cluster_std=0.5,ra‌​ndom_state=101)
#remaining code follows

代码失败,因为它试图调用(调用后面的圆括号检测到)data参数为n_samples = 64等。因为numpy.ndarray不是可调用类型,这意味着没有为它定义调用函数,此代码在调用scatterplot并显示plot之前引发TypeError。这就是你没看到情节的原因。