Skip to content

Instantly share code, notes, and snippets.

@larsmans
Created July 14, 2013 21:12
Show Gist options
  • Save larsmans/5996074 to your computer and use it in GitHub Desktop.
Save larsmans/5996074 to your computer and use it in GitHub Desktop.
k-means feature mapper for scikit-learn
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics.pairwise import rbf_kernel
class KMeansTransformer(BaseEstimator, TransformerMixin):
def __init__(self, centroids):
self.centroids = centroids
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return rbf_kernel(X, self.centroids)
@mblondel
Copy link

I'd be nicer to learn the centroids in fit.

@larsmans
Copy link
Author

@mblondel I learn the centroids in a separate pass over a large unlabeled dataset using MiniBatchKMeans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment