我从csv文件读取数据,第一行是字符串,其余都是小数。我不得不将此文件中的数据从字符串转换为十进制,现在我正在尝试对此数据运行决策树分类器。我可以很好地训练数据但是当我调用DecisionTreeClassifier.score()时,我收到错误消息:"不支持unknown"
这是我的代码:
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
我添加了print语句来检查输入参数的尺寸,这是它打印的内容:
obfA_test.shape: (48L, 12L)
tTime_test.shape: (48L,)
我很困惑为什么错误报告显示得分()的3个必需参数但是文档只有2.什么是&#34; self&#34;参数?任何人都可以帮我解决这个错误吗?
答案 0 :(得分:2)
这似乎让人联想到错误discussed here。问题似乎源于您用来拟合和评分模型的数据类型。在填写输入数据数组时,请尝试Decimal
,而不是float
。而且我不会有一个不准确的答案 - 你不能为DecisionTreeClassifiers使用浮点数/连续值。如果要使用浮点数,请使用DecisionTreeRegressor。否则,尝试使用整数或字符串(但这可能会转移您正在尝试完成的任务)。
至于最后的自我问题,这是Python的语法特质。当你做model.score(...)时,Python有点像得分(模型,...)。我担心我现在对此的了解不多,但回答原来的问题并不是必要的。 Here's an answer that better addresses that particular question.
答案 1 :(得分:1)
我意识到我遇到的问题是因为我试图使用DecisionTreeClassifier来预测连续值,因为它们只能用于预测离散值。我将不得不转而使用回归模型。