本笔记来源于B站Up主: 有Li 的影像组学系列教学视频
本节(38)主要介绍: 机器学习模型的保存和调用,这个操作的作用就是:不用每次都要训练一遍模型啦~~
视频里,李博士主要介绍了2种实现方法:
方法一:
# from sklearn.externals import joblib #注意有坑!如果报错就执行下行代码:
import joblib
modelPath = '/Mac/Documents/data/featureTable/rf.model'
joblib.dump(model_rf, modelPath)
model_rf_load = joblib.load(modelPath)
score_rf = model_rf_load.score(X_test, y_test)
print(score_rf)
方法二:
import pickle
modelPath = '/Mac/Documents/data/featureTable/rf.model'
with open(modelPath, 'wb') as f:
pickle.dump(model_rf,f)
with open(modelPath, 'rb') as f:
model_rf_load2 = pickle.load(f)
score_rf = model_rf_load2.score(X_test, y_test)
print(score_rf)