scikit learn - How to assign an new observation to existing Kmeans clusters based on nearest cluster centriod logic in python? -
i used below code create k-means clusters using scikit learn.
kmean = kmeans(n_clusters=nclusters,n_jobs=-1,random_state=2376,max_iter=1000,n_init=1000,algorithm='full',init='k-means++') kmean_fit = kmean.fit(clus_data) i have saved centroids using kmean_fit.cluster_centers_
i pickled k means object.
filename = pickle_path+'\\'+'_kmean_fit.sav' pickle.dump(kmean_fit, open(filename, 'wb')) so can load same kmeans pickle object , apply new data when comes, using kmean_fit.predict().
questions :
will approach of loading kmeans pickle object , applying
kmean_fit.predict()allow me assign new observation existing clusters based on centroid of existing clusters? approach recluster scratch on new data?if method wont work how assign new observation existing clusters given have saved cluster centriods using efficent python code?
ps: know building classifer using existing clusters dependent variable way dont want because of time crunch.
yes. whether sklearn.cluster.kmeans object pickled or not (if un-pickle correctly, you'll dealing "same" original object) not affect can use predict method cluster new observation.
an example:
from sklearn.cluster import kmeans sklearn.externals import joblib model = kmeans(n_clusters = 2, random_state = 100) x = [[0,0,1,0], [1,0,0,1], [0,0,0,1],[1,1,1,0],[0,0,0,0]] model.fit(x) out:
kmeans(copy_x=true, init='k-means++', max_iter=300, n_clusters=2, n_init=10, n_jobs=1, precompute_distances='auto', random_state=100, tol=0.0001, verbose=0) continue:
joblib.dump(model, 'model.pkl') model_loaded = joblib.load('model.pkl') model_loaded out:
kmeans(copy_x=true, init='k-means++', max_iter=300, n_clusters=2, n_init=10, n_jobs=1, precompute_distances='auto', random_state=100, tol=0.0001, verbose=0) see how n_clusters , random_state parameters same between model , model_new objects? you're go.
predict "new" model:
model_loaded.predict([0,0,0,0]) out[64]: array([0])
Comments
Post a Comment