为什么xgboost交叉验证表现如此之好而火车/预测表现如此糟糕?

时间:2016-01-28 04:00:29

标签: python xgboost

我正在使用xgboost,我正在尝试训练模型。这是我的一些代码:

def trainModel(training_data_filepath):

    training_data = loadDataFromFile(training_data_filepath)

    algorithm_parameters = {'max_depth': 2, 'eta': 1, 'silent': 1, 'objective': 'binary:logistic'}
    num_rounds = 1

    print xgb.cv(algorithm_parameters, training_data, num_rounds, nfold=2, metrics={'error'}, seed=0)
    return xgb.train(algorithm_parameters, training_data)

交叉验证打印出来:

test-error-mean  test-error-std  train-error-mean  train-error-std
       0.020742               0          0.019866         0.000292

对我来说,读取2%的测试错误是非常好的。但是,通过训练有素的模型返回,我也会对自己进行测试,在训练集中绘制一个保持集:

def testModel(classifier, test_data_filepath):

    test_data = loadDataFromFile(test_data_filepath)
    predictions = classifier.predict(test_data)
    labels = test_data.get_label()

    test_error = sum([1 for i in range(len(predictions)) if int(predictions[i]>0.5) != labels[i]]) / float(len(predictions))
    print 'Classifier test error: ' + `test_error`

哪个出现

Classifier test error: 0.2786214953271028

这是27%,这更糟糕。为什么会这样?当训练集上的交叉验证表现良好时,如何训练所有训练数据的模型在保持集上失败?我必须想象我的逻辑有问题,但我没有看到任何东西。那个或CV的xgboost实现做了我不明白的事情。

1 个答案:

答案 0 :(得分:0)

原来我是个傻瓜。 我正在创建训练并单独设置集合,因此他们对不同的标记有不同的索引,这意味着它比随机机会做得更好。我认为这让我很困惑 - 即使使用完全不同的功能索引,它也能比50%的准确率好得多。

相关问题