简单模型网格调参
from xgboost import XGBClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.model_selection import train_test_split,GridSearchCV
param_test1 = {'max_depth':range(3,10,2),'min_child_weight':range(1,6,2)}
model = XGBClassifier(eval_metric= 'mlogloss',
use_label_encoder=False,
learning_rate =0.1,
n_estimators=100,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
nthread=4,
scale_pos_weight=1,
seed=27,
verbose=True)
gsearch1 = GridSearchCV(model,param_grid = param_test1,scoring='roc_auc',n_jobs=20, cv=5,verbose=2)
gsearch1.fit(X_train, y_train)
print("最佳参数\n",gsearch1.best_params_)
print("最佳得分",gsearch1.best_score_)
使用OneVsRestClassifier的调参
需要在每个参数面前加上estimator__
param_test1 = {'estimator__max_depth':range(3,10,2),'estimator__min_child_weight':range(1,6,2)}
model = OneVsRestClassifier(XGBClassifier(eval_metric= 'mlogloss',
use_label_encoder=False,
learning_rate =0.1,
n_estimators=100,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
nthread=4,
scale_pos_weight=1,
seed=27,
verbose=True))
gsearch1 = GridSearchCV(model,param_grid = param_test1,scoring='roc_auc',n_jobs=20, cv=5,verbose=2)
gsearch1.fit(X_train, y_train)
print("最佳参数\n",gsearch1.best_params_)
print("最佳得分",gsearch1.best_score_)