Machine Learning (3) - Save and load trained model in 2 ways

模型的意义在于一次训练, 后面可以多次复用. 这里介绍两种方法用于保存和加载模型:

  1. pickle
  2. sklearn joblib
// 引入数据
df = pd.read_csv('/Users/rachel/Downloads/py-master/ML/1_linear_reg/homeprices.csv')

Machine Learning (3) - Save and load trained model in 2 ways

// 训练模型
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(df[['area']], df.price)
// 预测
model.predict([[3500]]) // 输出 array([655873.28767123])

方法一: pickle

import pickle
with open('model_pickle', 'wb') as f:
    pickle.dump(model, f)
with open('model_pickle', 'rb') as f:
    mp = pickle.load(f)

mp.predict([[3500]]) // 输出 array([655873.28767123])

方法二: sklearn joblib

from sklearn.externals import joblib

joblib.dump(model, 'model_joblib')

mj = joblib.load('model_joblib')

mj.predict([[3500]]) // 输出 array([655873.28767123])
ml
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!