将测试集分成子组,然后分别对每个子组进行预测

Divide the testing set into subgroup, then make prediction on each subgroup separately

我有一个类似于以下的数据集table:

预测目标将是 'score' 列。我想知道如何将测试集分成不同的子组,例如 1 到 3 之间的分数,或者然后检查每个子组的准确性。

现在我的情况如下:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
model = tree.DecisionTreeRegressor()
model.fit(X_train, y_train)
for i in (0,1,2,3,4):
    y_new=y_test[(y_test>=i) & (y_test<=i+1)]
    y_new_pred=model.predict(X_test)
    print metrics.r2_score(y_new, y_new_pred)

但是,我的代码不起作用,这是我得到的回溯:

Found input variables with inconsistent numbers of samples: [14279, 55955]

我已经尝试了下面提供的解决方案,但看起来对于整个分数范围 (0-5),r^2 是 0.67。但是子分数范围例如 (0-1,1-2,2-3,3-4,4-5) r^2s 明显低于整个范围。不应该有些单项分数r^2高于0.67有些低于0.67吗?

谁能告诉我我哪里做错了?非常感谢您的帮助。

当您计算指标时,您必须过滤预测值(基于您的子集条件)。

基本上你正在尝试计算

metrics.r2_score([1,3],[1,2,3,4,5])

这会产生错误,

ValueError: Found input variables with inconsistent numbers of samples: [2, 5]

因此,我建议的解决方案是

model.fit(X_train, y_train)
#compute the prediction only once. 
y_pred = model.predict(X_test)

for i in (0,1,2,3,4):
    #COMPUTE THE CONDITION FOR SUBSET HERE
    subset = (y_test>=i) & (y_test<=i+1)
    print metrics.r2_score(y_test [subset], y_pred[subset])