开发者社区> 问答> 正文

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

一码平川MACHEL 2019-02-28 14:28:21 501

现在我正在学习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

数据挖掘 Python
分享到
取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-07-17 23:29:46

    保存:

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

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

    0 0
+ 订阅

大数据计算实践乐园,近距离学习前沿技术

推荐文章
相似问题