how to save a scikit-learn k-means clustering model?

5.4k Views Asked by At

Currently The K-means CLustring code is written like this in a method:

def predict(image_path):
image = cv2.imread(image_path)
image = image.reshape((image.shape[0] * image.shape[1], 3))
clt = KMeans(n_clusters = 3,  random_state=2, n_jobs=1)
clt.fit(image)

How can I save This to a model so I can convert it to Core-ML and use it in my application?

3

There are 3 best solutions below

0
On

Core ML does not support K-means clustering at the moment.

You can find a simple implementation of K-means at the Swift Algorithm Club.

0
On

Save:

pickle.dump(clt, open("save.pkl", "wb"))

Load:

clt = pickle.load(open("save.pkl", "rb"))

0
On
import joblib 
joblib.dump(reducer, umap_model_path)
loaded_reducer = joblib.load(your_model_path)
embedding = loaded_reducer.fit_transform(data_to_predict)

using joblib allow you to set the prediction accuracy also