Issue
I have trained a sklearn tree with default parameters on a dataset in which all data are categorical. 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. 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)
. The question is how can I draw and visualize the confusion matrix for x_test
and y_test
?
Thanks.
Solution
You can use plot_confusion_matrix
to visually represent a 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
). 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 e.g. by providing labels other than the ones in y_test
. For more information, have a look at the documentation.
Answered By - afsharov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.