简体   繁体   English

具有分类数据的 sklearn 树的混淆矩阵

[英]Confusion matrix for sklearn tree with categorical data

I have trained a sklearn tree with default parameters on a dataset in which all data are categorical.我已经在所有数据都是分类的数据集上训练了一个带有默认参数的 sklearn 树。 The dataset is about cars.数据集是关于汽车的。

数据集示例

The rightmost column is the label associated with the attributes.最右边的列是与属性关联的标签。 Each row is an input instance.每行都是一个输入实例。 Because sklearn cannot handle categorical data, I used a dictionary to map each possible input for a feature into a dictionary and mapped them to positive integers so the dataset is no longer categorical and now consists of only integers where each has a meaning for each feature.由于 sklearn 无法处理分类数据,我使用字典将特征的每个可能输入映射到字典中,并将它们映射到正整数,因此数据集不再是分类数据,现在仅由整数组成,其中每个对每个特征都有意义。 I used the new training set to train the classifier.我使用新的训练集来训练分类器。

#x_train= training dat
#y_train= training data label
from sklearn.tree import DecisionTreeClassifier
clf=DecisionTreeClassifier()
clf.fit(x_train,y_train)

I'm able to calculate accuracy for an arbitrary x_test and y_test input with score(x_test, y_test) .我能够使用score(x_test, y_test)计算任意x_testy_test输入的score(x_test, y_test) The question is how can I draw and visualize the confusion matrix for x_test and y_test ?问题是如何绘制和可视化x_testy_test的混淆矩阵?

Thanks.谢谢。

You can use plot_confusion_matrix to visually represent a confusion matrix.您可以使用plot_confusion_matrix来直观地表示混淆矩阵。 You only need to pass the fitted estimator ( clf in your case) along with the input ( X_test ) and the true target values ( y_test ).您只需要传递拟合的估计量(在您的情况下为clf )以及输入( X_test )和真实目标值( y_test )。 Here is an example:下面是一个例子:

import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier


clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)

plot_confusion_matrix(clf, X_test, y_test)
plt.show()

The plot can further be customized eg by providing labels other than the ones in y_test .该图可以进一步定制,例如通过提供y_test标签以外的标签。 For more information, have a look at the documentation .有关更多信息,请查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM