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

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)