python学习,模型存储与再加载,决策树可视化

≡放荡痞女 提交于 2020-02-24 13:29:00

Python自学 (9)
文章编号:Python学【2020】009号
日期:2019-2-22


文章为日常学习记录,以备温习使用,同时作为像我一样的新人学习使用,内容属于学习记录,版权归视频学习提供方。以每天追无聊小说的心态,每天坚持学习码代码。


训练模型并将模型保存到joblib文件

存储文件“music-recommender.joblib”
在这里插入图片描述

在实操训练中,如文档所示在原来的模型中,利用DecisionTreeClassifer 建立模型,并进行训练,训练后存储到“music-recommender.joblib”文件
之后再新的程序过程中,应用已存储的joblib文件,利用joblib函数直接建立加载模型
应用model.predict进行预测

jupyter录入以下代码实现可视化文件存储为dot文件

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
#import data set
music_data = pd.read_csv('music.csv')
X = music_data.drop("genre",axis=1)
y = music_data["genre"]
#X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)
#Create a model
#old model
model = DecisionTreeClassifier()
#train model
model.fit(X,y)
#visualize 数据可视化,决策树模型可视化
tree.export_graphviz(model,out_file='music-recommender2.dot',
                    feature_names=['age','gender'],
#names,常出错项,容易将names输为name,少“s”
                    class_names=sorted(y.unique()),
                    label='all',
                    rounded= True,
                    filled=True)

vs-code 窗口转换的代码如下

digraph Tree {
node [shape=box, style="filled, rounded", color="green", fontname=helvetica] ;
edge [fontname=helvetica] ;
0 [label="age <= 30.5\ngini = 0.778\nsamples = 18\nvalue = [3, 6, 3, 3, 3]\nclass = Classical", fillcolor="#7be53933"] ;
1 [label="gender <= 0.5\ngini = 0.75\nsamples = 12\nvalue = [3, 0, 3, 3, 3]\nclass = Acoustic", fillcolor="#e5813900"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="age <= 25.5\ngini = 0.5\nsamples = 6\nvalue = [3, 0, 3, 0, 0]\nclass = Acoustic", fillcolor="#e5813900"] ;
1 -> 2 ;
3 [label="gini = 0.0\nsamples = 3\nvalue = [0, 0, 3, 0, 0]\nclass = Dance", fillcolor="#39e5c5ff"] ;
2 -> 3 ;
4 [label="gini = 0.0\nsamples = 3\nvalue = [3, 0, 0, 0, 0]\nclass = Acoustic", fillcolor="#e58139ff"] ;
2 -> 4 ;
5 [label="age <= 25.5\ngini = 0.5\nsamples = 6\nvalue = [0, 0, 0, 3, 3]\nclass = HipHop", fillcolor="#3c39e500"] ;
1 -> 5 ;
6 [label="gini = 0.0\nsamples = 3\nvalue = [0, 0, 0, 3, 0]\nclass = HipHop", fillcolor="#3c39e5ff"] ;
5 -> 6 ;
7 [label="gini = 0.0\nsamples = 3\nvalue = [0, 0, 0, 0, 3]\nclass = Jazz", fillcolor="#e539c0ff"] ;
5 -> 7 ;
8 [label="gini = 0.0\nsamples = 6\nvalue = [0, 6, 0, 0, 0]\nclass = Classical", fillcolor="#7be539ff"] ;
0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
}

数据可视化,在 vs-code 程序下加载dot文件,preview 结果

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!