【问题标题】:Fix parameters of Gaussian mixture model, instead of learning修复高斯混合模型的参数,而不是学习
【发布时间】:2020-11-24 09:38:07
【问题描述】:

假设我有一个数据集 data 用于拟合高斯混合模型:

from sklearn.mixture import GaussianMixture
model = GaussianMixture(n_components=4, covariance_type="full")
fit_model = model.fit(data)

我现在存储学习到的协方差fit_model.covariances_,意味着fit_model.means_ 和权重fit_model.weights_。从不同的脚本中,我想读入学习的参数并使用它们定义一个高斯混合模型。如何在不再次执行fit方法的情况下修复参数?

【问题讨论】:

  • 如何存储协方差、均值和权重?
  • 泡菜适合你吗?
  • numpy.save('./covariances.npy', fit_model.covariances_)
  • 由于 np.save 保存为二进制,你不妨使用 joblib 或 pickle 将整个拟合模型保存为二进制。

标签: python scikit-learn


【解决方案1】:

如果要再次执行模型,最简单的方法是将其保存为可序列化的pickle,这是一个示例:

from sklearn.mixture import GaussianMixture
import numpy as np
import pickle


X = np.random.rand(10, 2)

# Fit the model on data
model = GaussianMixture(n_components=4, covariance_type="full")
model.fit(X)

# Serialize and save the model
with open('model.pkl', 'wb') as file:
    pickle.dump(model, file)
    
# Load the model again for later use
with open('model.pkl', 'rb') as file:
    model = pickle.load(file)

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 2021-09-30
    • 2014-08-02
    • 1970-01-01
    • 2023-03-24
    • 2014-01-09
    • 2012-05-08
    • 2014-10-08
    相关资源
    最近更新 更多