markov zain
markov zain

Reputation: 12905

K-Means clustering [TypeError: __init__() got an unexpected keyword argument 'k']

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

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?

Upvotes: 1

Views: 10329

Answers (1)

agconti
agconti

Reputation: 18093

The argument is n_clusters instead of 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.

Upvotes: 3

Related Questions