如何保存 scikit-learn k-means 聚类模型?

Dan*_*Dan 2 python cluster-analysis machine-learning scikit-learn coreml

目前 K-means CLustring 代码在方法中是这样写的:

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)
Run Code Online (Sandbox Code Playgroud)

如何将其保存到模型中,以便将其转换为 Core-ML 并在我的应用程序中使用它?

小智 5

节省:

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

加载:

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