我想在數據集上做一個分類器。我第一次用XGBoost:爲什麼xgboost.cv和sklearn.cross_val_score會給出不同的結果?
import xgboost as xgb
import pandas as pd
import numpy as np
train = pd.read_csv("train_users_processed_onehot.csv")
labels = train["Buy"].map({"Y":1, "N":0})
features = train.drop("Buy", axis=1)
data_dmat = xgb.DMatrix(data=features, label=labels)
params={"max_depth":5, "min_child_weight":2, "eta": 0.1, "subsamples":0.9, "colsample_bytree":0.8, "objective" : "binary:logistic", "eval_metric": "logloss"}
rounds = 180
result = xgb.cv(params=params, dtrain=data_dmat, num_boost_round=rounds, early_stopping_rounds=50, as_pandas=True, seed=23333)
print result
,其結果是:
test-logloss-mean test-logloss-std train-logloss-mean
0 0.683539 0.000141 0.683407
179 0.622302 0.001504 0.606452
我們可以看到它大約是0.622;
但是當我切換到sklearn
使用完全相同的參數(我認爲),結果是完全不同的。下面是我的代碼:
from sklearn.model_selection import cross_val_score
from xgboost.sklearn import XGBClassifier
import pandas as pd
train_dataframe = pd.read_csv("train_users_processed_onehot.csv")
train_labels = train_dataframe["Buy"].map({"Y":1, "N":0})
train_features = train_dataframe.drop("Buy", axis=1)
estimator = XGBClassifier(learning_rate=0.1, n_estimators=190, max_depth=5, min_child_weight=2, objective="binary:logistic", subsample=0.9, colsample_bytree=0.8, seed=23333)
print cross_val_score(estimator, X=train_features, y=train_labels, scoring="neg_log_loss")
,其結果是:[-4.11429976 -2.08675843 -3.27346662]
,扭轉這一局面還遠遠0.622之後。
我把一個斷點轉換爲cross_val_score
,並且看到分類器正在通過嘗試預測測試集中的每個元組爲負值,並以0.99的概率進行瘋狂的預測。
我想知道我哪裏出錯了。有人能幫助我嗎?