如何在训练和测试集上显示分类树和混淆矩阵

时间:2020-10-18 16:26:03

标签: python jupyter-notebook

如何在训练和测试集上显示分类树和混淆矩阵

出于讨论的目的,使单独执行此操作变得困难!

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

from sklearn.metrics import confusion_matrix
cm_train = confusion_matrix(y_train, y_train_pred)
cm_test = confusion_matrix(y_test, y_test_pred)

然后,您可以只打印cm_traincm_test。另一种方法是“绘制”混淆矩阵。在这种情况下,您可以参考以下文档:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html

对于决策树,您将需要graphviz,joblib,os,StringIO和pydotplus。您可以执行以下操作:

import joblib, os
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
import pydotplus

outputPath = 'treeModel.joblib'
clf.fit(X_train, y_train)
joblib.dump(clf, outputPath)
y_pred = clf.predict(X_test)
graphviz_path = 'C:/Users/../bin/graphviz' #this should be your graphviz path
os.environ["PATH"] += os.pathsep + graphviz_path
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,filled=True, rounded=True,special_characters=True, feature_names=features)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()).write_png('Decision_Tree.png')

答案 1 :(得分:0)

您可以尝试一下!

import numpy as np
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
sb.set()

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

dectree = DecisionTreeClassifier(max_depth = 4)
dectree.fit(X_train, y_train)

y_train_pred = dectree.predict(X_train)
y_test_pred = dectree.predict(X_test)

print("Classification Acc \t Train:", dectree.score(X_train, y_train))
print("Classification Acc \t Test:", dectree.score(X_test, y_test))

f, axes = plt.subplots(1,2,figsize=(12,4))
sb.heatmap(confusion_matrix(y_train, y_train_pred),
          annot = True, fmt=".0f", annot_kws={"size":18}, ax=axes[0])
sb.heatmap(confusion_matrix(y_test, y_test_pred),
          annot = True, fmt=".0f", annot_kws={"size":18}, ax=axes[1])