【问题标题】:Load kmeans model, centroids and labels with cpickle使用 cpickle 加载 kmeans 模型、质心和标签
【发布时间】:2018-05-02 19:58:52
【问题描述】:

我坚持加载我的 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_'

【问题讨论】:

  • n_clusters 对我来说看起来很大。 KMeans 是否有可能只是未能产生结果,从而导致 cluster_centers_ 未定义?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 2020-08-04
    • 2014-10-29
    • 2021-10-01
    • 2020-05-15
    • 2021-08-20
    • 1970-01-01
    • 2020-12-13
    • 2021-10-09
    相关资源
    最近更新 更多