sklearn训练模型、保存模型文件(文本、pkl)、模型文件转换(pkl2onnx)以及模型可视化

1.使用环境

IDE:Jupyter Lab,使用Python2 kernel实现

模型可视化:GraphViz,可以直接在jupyter中使用;Netron    window版本

模型转化:在onnx/onnx-ecosystem容器中进行

2.代码

创建并训练模型

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd


from sklearn.datasets import load_iris
from sklearn import tree

iris = load_iris()

# 训练模型
clf =  tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
with open("iris.dot", 'w') as f:
    f = tree.export_graphviz(clf, out_file=f)


from IPython.display import Image  
import pydotplus

dot_data = tree.export_graphviz(clf, out_file=None, 
                         feature_names=iris.feature_names,  
                         class_names=iris.target_names,  
                         filled=True, rounded=True,  
                         special_characters=True)


graph = pydotplus.graph_from_dot_data(dot_data)

# 模型可视化
Image(graph.create_png())


 

将图片保存为pdf

#设置环境变量,解决调用graph时“InvocationException: GraphViz's executables not found”的错误。

import os
os.environ["PATH"] += os.pathsep + 'D:/Anaconda2/Library/bin/graphviz/' 

dot_data = tree.export_graphviz(clf, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data) 
graph.write_pdf("iris.pdf")

使用joblib保存模型为pkl格式,并读取pkl格式的模型文件进行预测

from sklearn.externals import joblib
joblib.dump(clf, "DecisionTreeClassifier.pkl")

f1=joblib.load('DecisionTreeClassifier.pkl')

f1.score(iris.data, iris.target)


使用pickle保存模型为文本格式并读取通过pickle保存的模型文件进行预测

import pickle
s=pickle.dumps(clf)
f=open('DecisionTreeClassifier.txt','w')
f.write(s)
f.close()

f2=open('DecisionTreeClassifier.txt','r')
s2=f2.read()
clf2=pickle.loads(s2)
clf2.score(iris.data, iris.target)

模型格式转换

在onnx/onnx-ecosystem容器执行如下代码:

将pkl格式的模型文件转换为onnx:DecisionTreeClassifier.pkl  ----> model.onnx

from sklearn.externals import joblib
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import *
import onnxmltools

# Update the input name and path for your sklearn model
input_skl_model = 'DecisionTreeClassifier.pkl'

# input data type for your sklearn model
input_data_type = [('float_input', FloatTensorType([1, 4]))]

# Change this path to the output name and path for the ONNX model
output_onnx_model = 'model.onnx'

# Load your sklearn model
skl_model = joblib.load(input_skl_model)

# Convert the sklearn model into ONNX
onnx_model = onnxmltools.convert_sklearn(skl_model, initial_types=input_data_type)

# Save as protobuf
onnxmltools.utils.save_model(onnx_model, output_onnx_model)

3.使用Netron查看pkl模型和onnx模型

查看pkl格式的模型

 

查看onnx格式的模型

 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汀桦坞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值