Accuracy score of a Decision Tree Classifier

13,530

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)
Share:
13,530

Related videos on Youtube

MaverickEyedea
Author by

MaverickEyedea

Updated on June 04, 2022

Comments

  • MaverickEyedea
    MaverickEyedea almost 2 years
    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?

    • Shridhar R Kulkarni
      Shridhar R Kulkarni about 6 years
      Mentioning error here will help.
    • MMF
      MMF about 6 years
      Well, it doesn't mean anything to compute the accuracy by comparing the labels of the test and the train, first, they are not related and second you most probably don't even have the same length for both! Your problem is that you overwrite the name labels_test, call it something else
  • user269867
    user269867 almost 6 years
    global name 'accuracy_score' is not defined
  • taga
    taga almost 5 years
    I get the error ValueError: labels_test and yhat_test have different number of output (2!=1)
  • taga
    taga almost 5 years
    Is there any way that you can help me with this? stackoverflow.com/questions/56622349/…