我正在使用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
对我来说,这是百分之二的测试误差,这很好。但是对于返回的训练模型,我还在从训练集中提取的维持集上运行了自己的测试:
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 实现做了一些我不明白的事情。
原来我是个傻瓜。我分别创建了训练集和保持集,因此它们对不同的代币有不同的索引,这意味着它比随机机会做得好得多是一个奇迹。我认为这就是让我感到困惑的地方——即使使用完全不同的功能索引,它的准确性也比 50% 好得多。