当前位置:网站首页>sklearn 的模型保存与加载使用
sklearn 的模型保存与加载使用
2022-07-24 17:53:00 【samll-guo】
'''
训练好了模型之后保存到本地,并重新加载模型用于预测的两种方法
'''
# 方法1:pickle
from sklearn import svm
from sklearn import datasets
import pickle
clf = svm.SVC()
iris = datasets.load_iris()
X,y = iris.data, iris.target
clf.fit(X,y)
# 模型的保存
with open('clf.pickle','wb') as f:
pickle.dump(clf,f) #将训练好的模型clf存储在变量f中,且保存到本地
# 模型的重新加载和使用
with open('clf.pickle','rb') as f:
clf_load = pickle.load(f) #将模型存储在变量clf_load中
print(clf_load.predict(X[0:1000])) #调用模型并预测结果# 方法2:joblib方法
from sklearn import svm
from sklearn import datasets
import joblib
# sklearn.externals.joblib函数是用在0.21及以前的版本中,在最新的版本中,该函数应被弃用改为直接导入joblib
# from sklearn.externals import joblib
clf = svm.SVC()
iris = datasets.load_iris()
X,y = iris.data, iris.target
clf.fit(X,y)
# 保存训练好的clf模型
joblib.dump(clf,'clf.pkl',compress=3)
# 重新加载训练好的clf模型
clf3 = joblib.load('clf.pkl')
print(clf3.predict(X[0:1000])) # 打印预测值
# 相对比而言,joblib更高效,但32位python和64位python不能无缝连接使用,可以通过如下代码查看本机的情况
import platform
print(platform.architecture())边栏推荐
- 0629~SaaS平台设计~全局异常处理
- C语言自定义类型讲解 — 联合体
- Pay close attention! List of the latest agenda of 2022 open atom open source Summit
- Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
- C language custom type explanation - structure
- Image information is displayed by browser: data:image/png; Base64, + image content
- 0621~ES&Lucene
- Openlayers: point aggregation effect
- 0615 ~ realize RBAC permission management with user-defined annotations
- Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing
猜你喜欢

Shengxin commonly used analysis graphics drawing 02 -- unlock the essence of volcano map!

Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph

SSM framework learning

二维卷积——torch.nn.conv2d的使用

C语言中的字符与字符串库函数的使用以及模拟实现

DF2NET三维模型部署

Inherit, override, overload

Section 7 Data Dictionary: hash followed by Daewoo redis ------- directory post

Common methods of number and math classes

Hcip day 3
随机推荐
Ship new idea 2022.2 was officially released, and the new features are really fragrant!
List of stringutils and string methods
Scept: consistent and strategy based trajectory prediction for planned scenarios
Use 4D nerf to display occlusion (cvpr2022)
Goodbye Navicat! This open source database management tool has a cooler interface!
Getaverse,走向Web3的远方桥梁
Still reading logs on the command line? Use kibana, visual log analysis yyds!
还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS!
0614~放假自习
Practical application cases of digital Twins - Smart Park
0612~quartz timer frame
Quickly complete the unit test junit4 setting of intelij idea
Brats18 - Multimodal MR image brain tumor segmentation challenge continued
awk从入门到入土(19)awk扩展插件,让awk如虎添翼
0623~ holiday self study
Common questions of testers during interview
Codeforces Round #794 (Div. 2)(A.B.C)
awk从入门到入土(17)awk多行写法
[leetcode] 30. Concatenate substrings of all words
JS & TS learning summary