我从csv文件中读取数据,第一行是字符串,其余都是小数。我不得不将该文件中的数据从字符串转换为十进制,现在正尝试对这些数据运行决策树分类器。我可以很好地训练数据,但是当我调用DecisionTreeClassifier.score()时,出现错误消息:“不支持未知”
这是我的代码:
cVal = KFold(len(file)-1, n_folds=10, shuffle=True);
for train_index, test_index in cVal:
obfA_train, obfA_test = np.array(obfA)[train_index], np.array(obfA)[test_index]
tTime_train, tTime_test = np.array(tTime)[train_index], np.array(tTime)[test_index]
model = tree.DecisionTreeClassifier()
model = model.fit(obfA_train.tolist(), tTime_train.tolist())
print model.score(obfA_test.tolist(), tTime_test.tolist())
我之前用以下几行填充了obfA和tTime:
tTime.append(Decimal(file[i][11].strip('"')))
obfA[i-1][j-1] = Decimal(file[i][j].strip('"'))
所以obfA是2D数组,tTime是1D。以前,我尝试删除上述代码中的“tolist()”,但它不会影响该错误。这是它打印的错误报告:
in <module>()
---> print model.score(obfA_test.tolist(), tTime_test.tolist())
in score(self, X, y, sample_weight)
"""
from .metrics import accuracy_score
-->return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
in accuracy_score(y_true, y_pred, normalize, sample_weight)
# Compute accuracy for each possible representation
->y_type, y_true, y_pred = _check_clf_targets(y_true, y_pred)
if y_type == 'multilabel-indicator':
score = (y_pred != y_true).sum(axis=1) == 0
in _check_clf_targets(y_true, y_pred)
if (y_type not in ["binary", "multiclass", "multilabel-indicator", "multilabel-sequences"]):
-->raise ValueError("{0} is not supported".format(y_type))
if y_type in ["binary", "multiclass"]:
ValueError: unknown is not supported
我添加了打印语句来检查输入参数的尺寸,这就是它的打印内容:
obfA_test.shape: (48L, 12L)
tTime_test.shape: (48L,)
我很困惑,为什么错误报告显示3个必需的score()参数,但文档中只有2个。“self”参数是什么?谁能帮我解决这个错误?
最佳答案
这似乎让人联想到错误discussed here。问题似乎源于您用来拟合模型并为其打分的数据类型。填充输入数据数组时,请使用Decimal
代替float
。就是这样,我没有一个不正确的答案-您不能对DecisionTreeClassifiers使用浮点数/连续值。如果要使用浮点数,请使用DecisionTreeRegressor。否则,请尝试使用整数或字符串(但这可能会避开您要完成的任务)。
至于最后的自我问题,这是Python的语法特质。当您执行model.score(...)时,Python会将其像score(model,...)一样对待。恐怕我现在不了解更多,但是没有必要回答您的原始问题。 Here's an answer that better addresses that particular question.
关于python - 在Scikit中测试DecisionTreeClassifier时出错,请使用Python学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931036/