2016-01-28 33 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%。

相關問題