import pandas as pd
#用于数据预处理/特征工程的工具包
from sklearn import tree
from sklearn.datasets import load_iris
import pydotplus
iris = load_iris()
iris_feature_name = iris.feature_names
iris_feature = iris.data
iris_target_name = iris.target_names
iris_target = iris.target
# print(iris_feature_name)
# print(iris_feature)
# print(iris_target_name)
# print(iris_target)
clf = tree.DecisionTreeClassifier(max_depth=4)
clf = clf.fit(iris_feature,iris_target)
# print(clf)
dot_data = tree.export_graphviz(clf,
out_file=None,
feature_names=iris_feature_name,
class_names=iris_target_name,
filled=True,
rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_png('DecisionTree.png')
网友评论