2017-05-12 5 views
0

私は、最も予測的な属性を得るためのXGBClassifierに最適なハイパーパラメータを取得しようとしています。私はRandomizedSearchCVを使用してKFoldを反復して検証しようとしています。XGBClassifierのRandomizedSearchCVからの最善のエスティメータが、別々に実行されたときに異なる結果をもたらすのはなぜですか?

私は最良適合推定値を得て、テストサブサンプルデータに対して予測関数を実行します。それから私は私の混乱マトリクスを見て、私の目標がなくても完璧な結果を得ているのを見ます。

次に、私は最高のエスティメータを得て、XGBClassifierを直接実行します。私の混乱行列の結果は大きく変化します。私はRandomizedSearchCVと外部の中で実行されている場合、一番合う見積もりを一貫させたいので、何が間違っているのか分かりません。なぜ私は完璧な得点を得続けますか?

from scipy import stats 
from scipy.stats import randint 
from sklearn.model_selection import RandomizedSearchCV 
from sklearn.metrics import precision_score,recall_score,accuracy_score,f1_score,roc_auc_score 

y = np.asarray(df_comb_clean[target]) 
df_comb_X = df_comb_clean.drop([target],1) 
X = np.asarray(df_comb_X) 

clf_xgb = xgb.XGBClassifier(objective = 'binary:logistic') 
param_dist = {'n_estimators': stats.randint(150, 1000), 
       'learning_rate': stats.uniform(0.01, 0.6), 
       'subsample': stats.uniform(0.3, 0.9), 
       'max_depth': [3, 4, 5, 6, 7, 8, 9], 
       'colsample_bytree': stats.uniform(0.5, 0.9), 
       'min_child_weight': [1, 2, 3, 4] 
      } 

numFolds = 5 
kfold_5 = cross_validation.KFold(n = len(X), shuffle = True, n_folds = numFolds) 

clf = RandomizedSearchCV(clf_xgb, 
         param_distributions = param_dist, 
         cv = kfold_5, 
         n_iter = 5, # you want 5 here not 25 if I understand you correctly 
         scoring = 'roc_auc', 
         error_score = 0, 
         verbose = 3, 
         n_jobs = -1) 

clf.fit(X, y) 

次のコマンドを実行すると、平均列車とテストの得点が得られます。また、私は私の最高の推定を引き出したい:

print "mean_train_score", clf.cv_results_['mean_train_score'] 
print "mean_test_score", clf.cv_results_['mean_test_score'] 
print clf.best_estimator_ 

出力:私は最高の推定がに私のサブサンプル集団と出力結果に実行されているかを確認したい

mean_train_score [ 0. 0. 1. 1. 1.] 
mean_test_score [ 0.   0.   0.76425856 0.77198744 0.74225311] 
XGBClassifier(base_score=0.5, colsample_bylevel=1, 
     colsample_bytree=0.76920759422068707, gamma=0, 
     learning_rate=0.13626591956991532, max_delta_step=0, max_depth=7, 
     min_child_weight=1, missing=None, n_estimators=880, nthread=-1, 
     objective='binary:logistic', reg_alpha=0, reg_lambda=1, 
     scale_pos_weight=1, seed=0, silent=True, 
     subsample=0.59412792468572662) 

ので、次のステップ混同行列:

y_train = np.asarray(df_train[target]) 
df_train_X = df_train.drop([target],1) 
X_train = np.asarray(df_train_X) 

dtrain_predictions = clf.best_estimator_.predict(X_train) 
cnf_matrix_train = confusion_matrix(y_train, dtrain_predictions) 
print "train: \n" , cnf_matrix_train 

y_test = np.asarray(df_test[target]) 
df_test_X = df_test.drop([target],1) 
X_test = np.asarray(df_test_X)  

dtest_predictions = clf.best_estimator_.predict(X_test) 
xpred = pd.DataFrame(dtest_predictions) 
cnf_matrix_test = confusion_matrix(y_test, dtest_predictions) 
print "test: \n" , cnf_matrix_test 

これは私に非常に奇妙な出力を与え、私は(私は上記のセクションでは、ターゲットを削除し、でも、インデックスをリセットする)理由はわからない。

train: 
[[3840 0] 
[ 0 354]] 
test: 
[[1644 0] 
[ 0 150]] 

私はRandomizedSearchCVの外にそれを予測/私の最高の推定や改装を引き出し、今、私は同じ結果を得る次のステップ:テストの

clf_best = XGBClassifier(base_score=0.5, colsample_bylevel=1, 
     colsample_bytree=0.76920759422068707, gamma=0, 
     learning_rate=0.13626591956991532, max_delta_step=0, max_depth=7, 
     min_child_weight=1, missing=None, n_estimators=880, nthread=-1, 
     objective='binary:logistic', reg_alpha=0, reg_lambda=1, 
     scale_pos_weight=1, seed=0, silent=True, 
     subsample=0.59412792468572662) 

df_comb_X = df_comb_clean.drop([target],1) 
clf_best.fit(df_comb_X, df_comb_clean[target],eval_metric='auc') 

clf_test_best= clf_best.predict(df_test_X) 

cnf_best_test = confusion_matrix(y_test, clf_test_best) 
print "test: \n" , cnf_best_test 

feat_imp = pd.Series(clf_best.booster().get_fscore()).sort_values(ascending=False) 

出力:

test: 
[[1644 0] 
[ 0 150]] 

私はそれを考え出し私は総人口に見積もりを当てていました。サブサンプルは全人口の一部でした。愚かな間違い。

答えて

0

私は母集団全体にXGBClassifierを適合させてから、同じ集団からランダムにサブサンプルに行きました。この結果、同じ結果につながりました。

関連する問題