MaverickEyedea
MaverickEyedea

Reputation: 51

Accuracy score of a Decision Tree Classifier

import sys
from class_vis import prettyPicture
from prep_terrain_data import makeTerrainData
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

import numpy as np
import pylab as pl

features_train, labels_train, features_test, labels_test = makeTerrainData()
X = features_train
Y = labels_train
clf = DecisionTreeClassifier()
clf = clf.fit(X,Y)
labels_test = clf.predict(features_test)

acc = accuracy_score(labels_test, labels_train)

I can't calculate the accuracy of a DecisionTreeClassifier using the above code. Can somebody help me with this?

Upvotes: 4

Views: 20028

Answers (2)

MMF
MMF

Reputation: 5921

The problem is that you are mixing up things. It doesn't mean anything to compute the accuracy comparing the train and test labels.

Do the following instead:

features_train, labels_train, features_test, labels_test = makeTerrainData()
X = features_train
Y = labels_train
clf = DecisionTreeClassifier()
clf = clf.fit(X,Y)
# Here call it somehing else!
yhat_test = clf.predict(features_test)
# Compute accuracy based on test samples
acc = accuracy_score(labels_test, yhat_test)

Upvotes: 3

Saideshwar Kotha
Saideshwar Kotha

Reputation: 1

Make this change

predicted = clf.predict(features_test)

acc = accuracy_score(labels_test, predicted)

Upvotes: 0

Related Questions