首页 > 解决方案 > 需要使用 Python 进行决策树可视化 GraphViz 使用 10-fold

问题描述

我有一个代码,我需要在 Python 中使用 GraphViz 可视化决策树的输出。采用 10 折交叉验证来获得相当充分的评估指标。只需要graphviz的指南

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import cross_val_score
from sklearn.metrics import make_scorer
from sklearn.model_selection import cross_validate
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
from sklearn import tree
data = pd.read_csv("goddess_2.csv")

label_Label = LabelEncoder()

data["Label"] = label_Label.fit_transform(data['Label'])

X = data.drop("Label", axis = 1)
y = data['Label']

#Decision Tree Binary Class Classifier Model Building

model = DecisionTreeClassifier()

scoring = ['accuracy','precision_weighted', 'recall_weighted','f1_weighted']
scores = cross_validate(model,  X, y, cv=10,scoring=scoring)

Accuracy = scores['test_accuracy'].mean()
Precision = scores['test_precision_weighted'].mean()
Recall = scores['test_recall_weighted'].mean()
F1Score= scores['test_f1_weighted'].mean()

print("********** Decision Tree *********")
print("\n")
print("Accuracy,",round(Accuracy * 100,3))
print("\n")
print("Precision,",round(Precision * 100,4))
print("\n")
print("Recall,",round(Recall * 100,4))
print("\n")
print("F1-Score,",round(F1Score * 100,4))
print("\n")

clf = DecisionTreeClassifier(random_state=1234)
model = clf.fit(X, y)
text_representation = tree.export_text(clf)
print(text_representation)

标签: pythonvisualizationgraphvizcross-validationdecision-tree

解决方案


推荐阅读