k - means集群[TypeError: __init__()得到一個意外的關鍵字參數'k'] - K-Means clustering [TypeError: __init__() got an unexpected keywor

k - means集群[TypeError: __init__()得到一個意外的關鍵字參數'k'] - K-Means clustering [TypeError: __init__() got an unexpected keywor,第1张

I have tried the sample of k means clustering algorithm from this.

我已經嘗試過k均值聚類算法的樣本。

The code is following below:

代碼如下:

In [11]:
print __doc__
from time import time
import numpy as np
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.datasets import load_digits
from sklearn.preprocessing import scale
np.random.seed(42)
digits = load_digits()
data = scale(digits.data)
n_samples, n_features = data.shape
n_digits = len(np.unique(digits.target))
labels = digits.target
print "n_digits: %d" % n_digits
print "n_features: %d" % n_features
print "n_samples: %d" % n_samples
print "Raw k-means with k-means   init..."
km = KMeans(init='k-means  ', k=n_digits, n_init=10).fit(data)
print "done in %0.3fs" % (time() - t0)
print "Inertia: %f" % km.inertia_
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels, km.labels_)
print "Completeness: %0.3f" % metrics.completeness_score(labels, km.labels_)
print "V-measure: %0.3f" % metrics.v_measure_score(labels, km.labels_)
Automatically created module for IPython interactive environment
n_digits: 10
n_features: 64
n_samples: 1797
Raw k-means with k-means   init...
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-7cfc5c34beb6> in <module>()
     27 print "Raw k-means with k-means   init..."
     28 t0 = time()
---> 29 km = KMeans(init='k-means  ', k=n_digits, n_init=10).fit(data)
     30 print "done in %0.3fs" % (time() - t0)
     31 print "Inertia: %f" % km.inertia_

TypeError: __init__() got an unexpected keyword argument 'k'

I faced an error TypeError: __init__() got an unexpected keyword argument 'k', in here the k represents the number of clusters, why it is detected as an error?

我遇到了一個錯誤類型錯誤:__init__()得到了一個意料之外的關鍵字參數“k”,這里的k表示集群的數量,為什么它被檢測為錯誤?

1 个解决方案

#1


3  

The argument is n_clusters instead of k:

參數是n_clusters而不是k:

km = KMeans(init='k-means  ', n_clusters=n_digits, n_init=10).fit(data)

Check out the expected arguments in the scikit-learn docs.

在scikit-learn文檔中查看預期的參數。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复