unstuck
unstuck

Reputation: 646

Import error. Can't import kmeans_plusplus

I'm trying to replicate an example of a clustering model with scikit-learn:

import sklearn
sklearn.__version__

Returns:

'0.23.2'

And:

from sklearn.cluster import kmeans_plusplus

Returns the Error message:

ImportError: cannot import name 'kmeans_plusplus' from 'sklearn.cluster' (C:\Users\sddss\anaconda3\lib\site-packages\sklearn\cluster\__init__.py)

Upvotes: 1

Views: 7520

Answers (2)

Harpal
Harpal

Reputation: 12577

There is no kmeans_plusplus class or module for version 0.23.2. You need to import KMeans and set the init key word argument to kmeans++ to obtain the behaviour you want

from sklearn.cluster import KMeans
kmeans = KMeans(init='k-means++')

Upvotes: 2

desertnaut
desertnaut

Reputation: 60321

According to the documentation, kmeans_plusplus is

New in version 0.24.

so it is not available for the version 0.23.2 you are using.

Nevertheless, this should not be a real issue; the only difference between the "good old" K-Means already available in scikit-learn is the initialization of the cluster centers according to the kmeans++ algorithm; and this is already available in the standard KMeans. From the standard KMeans documentation regarding the init argument:

'k-means++' : selects initial cluster centers for k-mean clustering in a smart way to speed up convergence

So, what you need to do instead is simply to use the "vanilla" KMeans of scikit-learn with the argument init='kmeans++':

from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=n_clusters, init='kmeans++')

Upvotes: 3

Related Questions