sklearn-决策树

训练树模型
from
sklearn.tree import DecisionTreeClassifier #导入决策树分类库 from sklearn.datasets import load_breast_cancer #导入数据集 cancer=load_breast_cancer() #实例化 from sklearn.model_selection import train_test_split

以上因为叶节点是纯的,所以精度为1

以上深度设为 4,测试集上表现比上面的好

分析树模型:

首先可视化树模型

from sklearn.tree import export_graphviz
export_graphviz(treeMaxD4,out_file='E:\temp\tree.dot',class_names=['maligant','benign'],feature_names=cancer['feature_names'],impurity=False,filled=True)
with open('E:\temp\tree.dot') as f:
    dot_graph=f.read()
    graphviz.Source(dot_graph)

在文件下就存在了pdf

 特征重要性表

原文地址:https://www.cnblogs.com/vivianzy1985/p/9228898.html