Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
MaverickEyedea Avatar asked Sep 12 '25 13:09

MaverickEyedea


1 Answers

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)
like image 115
MMF Avatar answered Sep 14 '25 04:09

MMF