from sklearn.tree import DecisionTreeClassifierimport pandas as pdfrom 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)我得到“TypeError:'DecisionTreeClassifier'对象不可调用”,即使我(主要遵循datacamp教程)。
1 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
dt(...)试图“调用”,你不能这样做,因为不是一个函数。您需要一个从真实标签和预测标签计算准确性的函数。dtdt
试试这样的东西
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)
编辑:
我的更初学者友好的版本看起来像calculate_accuracy()
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)
添加回答
举报
0/150
提交
取消