欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

机器学习与深度学习:模型的保存和加载

程序员文章站 2022-07-13 08:57:16
...

我们再建模完毕以后,并不需要每次都把数据重新执行,而是将数据模型给保留下来

from sklearn.externals import joblib
#保存:
joblib.dump(estimator, 'test.m')
#加载:
estimator = joblib.load('test.m')

详细案例:

import numpy as np
import pandas as pd
import pickle as pk
from sklearn.externals import joblib
cancer=pd.read_csv('cancer.csv',sep='\t')
fea = cancer.select_dtypes(include=['float64'])
lab = cancer.select_dtypes(include=['object'])
#加载模型
with open('best_knn.m','rb') as fp:
    best_knn = pk.load(fp)
    
with open('std_model.m','rb') as fp:
    STD = pk.load(fp)
STD=joblib.load(filename='std_model.m')
best_knn=joblib.load(filename='best_knn.m')
fea_std = STD.transform(fea)
best_knn.score(fea_std,lab)

机器学习与深度学习:模型的保存和加载