开发者社区> 问答> 正文

如何使用pickle进行k-means聚类

现在我正在学习k-means聚类我想要使用pickle来转储和加载我训练过的模型如何做到这一点。

我的代码是:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pickle
from sklearn.cluster import KMeans
from sklearn.externals import joblib

# importing our dataset
dataset = pd.read_csv("Mall_Customers.csv")
X = dataset.iloc[:, [3,4]].values

# Applying k-means to the mall dataset
kmeans = KMeans(n_clusters=5, init='k-means++',random_state=0)
y_kmeans = kmeans.fit_predict(X)

# Visualising the clusters
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Cluster 3')
plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4')
plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 300, c = 'yellow', label = 'Centroids')
plt.title('Clusters of customers')
plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()
我的问题:

如何使用泡菜进行转储和装载?
如何使用pickle预测新的聚类值。这意味着我要传递两个整数值一个=>工资,两个=>得分取决于此我需要新的输出像这两个是在哪个集群像tha

展开
收起
一码平川MACHEL 2019-02-28 14:28:21 2846 0
1 条回答
写回答
取消 提交回答
  • 保存:

    pickle.dump(kmeans, open("save.p", "wb"))
    加载:

    kmeans = pickle.load(open("save.p", "rb"))

    2019-07-17 23:29:46
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Streaming Outlier Analysis for Fun and Scalability 立即下载
Streaming Outlier Analysis for 立即下载
低代码开发师(初级)实战教程 立即下载