2016-01-28 38 views
1

我正在使用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 

这对我读两个测试误差,这是相当不错的。但随着其从训练组得出所返回我还跑我自己的测试训练的模型,在抵抗组:

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实现做了一些我不明白的事情。

回答

0

原来我是个混蛋。 我正在创建培训并单独进行设置,因此他们对不同的代币有不同的指数,这意味着它比随机机会做得好得多。我认为这让我感到困惑 - 即使使用完全不同的功能索引,它的精确度也远远优于50%。

相关问题