xgboost:エラー編
RandomForestのグリッドサーチと同じようにxgboostでもやろうとした。
下記はmax_depthとmin_child_weightのみを使ったグリッドサーチ。
X_train, X_test, y_train, y_testはpandas。
GridSearchCVのパラメーターは今回の話に関係ないから無視してよい。
from xgboost.sklearn import XGBClassifier from sklearn.grid_search import GridSearchCV from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split( variables, targets, random_state=666) param_test2 = { 'max_depth':[5,7,9], 'min_child_weight':[1,3,5] } gsearch2 = GridSearchCV(estimator = XGBClassifier(learning_rate=0.1, n_estimators=1000, max_depth=5, min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8, objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), param_grid = param_test2, scoring='roc_auc',n_jobs=-1,iid=False, cv=5) gsearch2.fit(X_train,y_train)
するとgsearch2.fit(X_train,y_train)部分で下記エラー。
IndexError: too many indices for array
どうもy_trainの次元が多いようだ。
ということはpandasでダメってことだろうから、一次元配列にすればいいのだろうなというのがわかるので、下記のように変えてると動く。
gsearch2.fit(X_train,y_train.as_matrix().reshape(-1,))
というか、なんでRandom Forestで動いてるのにxgboostで動かないのだ。
実装違うのかよ、と思ったが、
from xgboost.sklearn import XGBClassifier
とあるので勘違いしていたが、そもそもxgboostはsklearnではないので当然実装は違う。
tree系はターゲットが一次元配列でなくてもよいようだ。
https://github.com/scikit-learn/scikit-learn/blob/f0ab589f/sklearn/ensemble/forest.py#L230
y : array-like, shape = [n_samples] or [n_samples, n_outputs]
一方、xgboostはソースの記述だけではわからないw
https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py#L500
y : array_like