from sklearn.tree import DecisionTreeClassifier
import pandas as pd
from sklearn.metrics import accuracy_score
## training data (20%)
data = pd.read_csv("train.csv", usecols=[1,2,9])
X_train = pd.read_csv("train.csv", usecols=[2,9])
y_train = pd.read_csv("train.csv", usecols=[1])
dt = DecisionTreeClassifier(max_depth=6)
dt.fit(X_train, y_train)
y_predict = dt.predict(X_test)
accuracy = dt(y_test, y_predict)
i get "TypeError: 'DecisionTreeClassifier' object is not callable" even though i (mostly followed a datacamp tutorial).
dt(...)
is attempting to "call" dt
which you can't do because dt
is not a function. You need a function that calculates accuracy from true and predicted labels.
Try something like this
def calculate_accuracy(y_true, y_predicted):
num_correct = sum(map(lambda t, p: t==p, y_true, y_predicted))
return num_correct / len(y_true)
accuracy = calculate_accuracy(y_test, y_predict)
EDIT:
The more beginner friendly version of my calculate_accuracy()
would look like
def calculate_accuracy(y_true, y_predicted):
num_correct = 0
for i in range(len(y_true)):
if (y_true[i] == y_predicted[i]):
num_correct = num_correct + 1
return num_correct / len(y_true)