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

python保存加载机器学习模型实例

程序员文章站 2022-07-13 09:14:53
...

尽量用pickle 不要用joblib,joblib好像后续不支持了。
import pickle
datafile2 =’…/data/model.xls’
datafile3=’…/data/model3.xls’ data2=pd.read_excel(datafile2)
print(“data2:”,data2)
data3=pd.read_excel(datafile3)
print(“data3:”,data3)
data = data2.append(data3)
print(“data:”,data)
data = data._values
print(“data_values:”,data) np.random.shuffle(data)
print(“after shuffle data:”,data)
p = 0.8
train=data[:int(len(data)*p),:]
test = data[int(len(data)*p):,:]
models_path = ‘…/models/’
print(“6. dt 分类”)
tree =DecisionTreeClassifier()
tree.fit(train[:,:3], train[:,3])
tree.predict(test[:,:3]) print(“predict:”,tree.predict(test[:,:3]))
print(“true :”,test[:,3])

dt_models_path = models_path +"tree.sav" 
pickle.dump(tree, open(dt_models_path, 'wb'))
 loaded_model = pickle.load(open(dt_models_path, 'rb'))  print("predict:",loaded_model.predict(test[:,:3]))
相关标签: 笔记