scikit-learn-extra

raw JSON →
0.3.0 verified Mon Apr 27 auth: no python maintenance

A set of additional estimators and tools for scikit-learn, including K-Medoids, EigenPro, and Fastfood. Current version: 0.3.0 (released Oct 2021). Releases are infrequent; the project is in maintenance mode.

pip install scikit-learn-extra
error ModuleNotFoundError: No module named 'sklearn_extra'
cause The package scikit-learn-extra is not installed.
fix
Run 'pip install scikit-learn-extra'.
error ImportError: cannot import name 'KMedoids' from 'sklearn.cluster'
cause KMedoids is not part of scikit-learn core; it lives in sklearn_extra.
fix
Use 'from sklearn_extra.cluster import KMedoids'.
error ValueError: The number of clusters must be less than the number of samples.
cause KMedoids requires n_clusters < n_samples.
fix
Set n_clusters to a value less than the number of data points.
gotcha KMedoids was originally in sklearn_extra.cluster; ensure you import from sklearn_extra, not sklearn.
fix Use 'from sklearn_extra.cluster import KMedoids'.
gotcha The library may not be actively maintained; check for compatibility with newer scikit-learn versions (>=1.0).
fix Pin scikit-learn version to <=1.0.2 if issues arise, or consider alternatives like sklearn.cluster.KMeans.
deprecated Kernel approximation estimators (EigenPro, Fastfood) may behave differently or be removed in future versions.
fix Use scikit-learn's RBFSampler as a more stable alternative.

Fit K-Medoids clustering on a small dataset and print cluster labels.

from sklearn_extra.cluster import KMedoids
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
kmedoids = KMedoids(n_clusters=2, random_state=0).fit(X)
print(kmedoids.labels_)