Python机器学习:随机搜索调参,优化AUC值,代码示例
导入必要的库
from sklearn.model_selection import RandomizedSearchCV from sklearn.metrics import roc_auc_score from scipy.stats import uniform, randint
定义参数空间
param_dist = { 'n_estimators': randint(100, 1000), 'learning_rate': uniform(0.01, 0.5), 'subsample': uniform(0.1, 0.9), 'max_depth': [3, 4, 5, 6, 7, 8, 9], 'colsample_bytree': uniform(0.5, 0.4), 'min_child_weight': [1, 2, 3, 4] }
定义模型
xgb_model = XGBClassifier()
定义随机搜索
random_search = RandomizedSearchCV( xgb_model, param_distributions=param_dist, n_iter=100, # 迭代次数 cv=5, # 交叉验证次数 n_jobs=-1 # 并行处理 )
进行随机搜索
while True: random_search.fit(X_train, y_train) y_pred = random_search.predict_proba(X_val)[:, 1] auc = roc_auc_score(y_val, y_pred) if auc >= 0.75 and abs(auc - 0.75) < 0.1: break
输出最佳参数
print('Best parameters: ', random_search.best_params_)
原文地址: https://www.cveoy.top/t/topic/oJWT 著作权归作者所有。请勿转载和采集!