2016-09-08 7 views
4

目的関数 'multi:softmax'を使って、xgb.XGBClassifierにクラス数または評価メトリックの数を渡す方法がわかりません。xgboost(python)のマルチクラス分類

私は多くの文書を見ましたが、n_class/num_classを受け入れるsklearnラッパーについての唯一の話です。

私の現在の設定は、あなたがXGBoostの分類のためのAPIを学ぶscikitにnum_classを設定する必要はありません

kf = cross_validation.KFold(y_data.shape[0], \ 
    n_folds=10, shuffle=True, random_state=30) 
err = [] # to hold cross val errors 
# xgb instance 
xgb_model = xgb.XGBClassifier(n_estimators=_params['n_estimators'], \ 
    max_depth=params['max_depth'], learning_rate=_params['learning_rate'], \ 
    min_child_weight=_params['min_child_weight'], \ 
    subsample=_params['subsample'], \ 
    colsample_bytree=_params['colsample_bytree'], \ 
    objective='multi:softmax', nthread=4) 

# cv 
for train_index, test_index in kf: 
    xgb_model.fit(x_data[train_index], y_data[train_index], eval_metric='mlogloss') 
    predictions = xgb_model.predict(x_data[test_index]) 
    actuals = y_data[test_index] 
    err.append(metrics.accuracy_score(actuals, predictions)) 

答えて

8

のように見えます。 fitが呼び出されると自動的に実行されます。 fitメソッドの冒頭のxgboost/sklearn.pyXGBClassifierと見てください。

evals_result = {} 
    self.classes_ = np.unique(y) 
    self.n_classes_ = len(self.classes_) 

    xgb_options = self.get_xgb_params() 

    if callable(self.objective): 
     obj = _objective_decorator(self.objective) 
     # Use default value. Is it really not used ? 
     xgb_options["objective"] = "binary:logistic" 
    else: 
     obj = None 

    if self.n_classes_ > 2: 
     # Switch to using a multiclass objective in the underlying XGB instance 
     xgb_options["objective"] = "multi:softprob" 
     xgb_options['num_class'] = self.n_classes_ 
関連する問題