使用cpickle加载kmeans模型,质心和标签

时间:2017-11-19 14:10:29

标签: python-3.x pickle k-means

我坚持加载我的kmeans模型参数。我用pickle来保存和加载我的模型如下:

kmeans = KMeans(n_clusters=2000).fit(examples)
distances = np.column_stack([np.sum((examples - center)**2, axis=1)**0.5 for center in kmeans.cluster_centers_])
np.savetxt('/data/distances.csv',distances,delimiter=",")
filename ='/model/k-2000.sav'
pickle.dump(KMeans, open(filename, 'wb'))

并按如下方式加载模型:

loaded_model = pickle.load(open(filename, 'rb'))

现在我想获得质心和标签如下:

loaded_model.cluster_centers_ 

我收到以下错误:

AttributeError: type object 'KMeans' has no attribute 'cluster_centers_'

1 个答案:

答案 0 :(得分:0)

您要保存的是类而不是实例     pickle.dump(KMeans, open(filename, 'wb')) 应该    pickle.dump(kmeans, open(filename, 'wb'))