WXQIfMw

2020-12-25   阅读量: 1221

【求助!】使用graphviz画决策树时,提示无法识别png如何解决?

扫码加入数据分析学习群

from sklearn.model_selection import train_test_split

from sklearn.datasets.california_housing import fetch_california_housing

import pydotplus

from IPython.display import Image

import graphviz

import pandas as pd

from sklearn.preprocessing import LabelEncoder

import os

os.environ["PATH"] += os.pathsep +"D:\anaconda\Library\bin"

import pandas as pd


loan = pd.read_csv('../data/small_loan_processing.csv',encoding='utf-8')

broadband = pd.read_csv('../data/broadband_processing.csv',encoding='utf-8')


#data = loan.copy()

data = broadband.copy()


print(data.head())

from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split


x_train, x_test, y_train, y_test = train_test_split(data.drop('y',axis=1), data['y'], test_size=0.2, random_state=0)


# 建模

# criterion{"gini", "entropy"}, default="gini"

# min_samples_split, default=2 (The minimum number of samples required to split an internal node)

dtc = DecisionTreeClassifier(max_depth=9, criterion='gini', random_state=0)

dtc.fit(x_train, y_train)

import matplotlib.pyplot as plt

from sklearn.metrics import confusion_matrix,classification_report,plot_roc_curve


y_pred = dtc.predict(x_train)


print('train\nconfusion_matrix\n', confusion_matrix(y_true=y_train, y_pred=y_pred))

print('classification report\n',classification_report(y_true=y_train, y_pred=y_pred))


y_pred = dtc.predict(x_test)


print('test\nconfusion_matrix\n', confusion_matrix(y_true=y_test, y_pred=y_pred))

print('classification report\n',classification_report(y_true=y_test, y_pred=y_pred))

plot_roc_curve(dtc, x_train, y_train, color='blue')

ax = plt.gca()

plot_roc_curve(dtc, x_test, y_test, ax=ax, color='red')

plt.show()

from sklearn.tree import export_graphviz

from IPython.display import display,Image

import pydotplus

import numpy as np

import os

os.environ["PATH"] += os.pathsep + '../D:\anaconda\Library\bin'dot_tree = export_graphviz(dtc, out_file=None, feature_names=x_train.columns, class_names=str(np.unique(y_train)),filled=True, rounded=True, special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_tree)

graph.write_png("dtc.png")

image.png

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
100.4651 2 1 关注作者 收藏

评论(1)

WXQIfMw
2020-12-25

运行到最后一步提示报错

0.0000 0 0 回复

推荐课程