GridSearch调参xgb

本文通过使用XGBoost算法进行参数调优,详细介绍了如何利用GridSearchCV进行超参数搜索,以提高模型的预测准确率。文章涵盖了数据读取、预处理、模型训练、验证集划分及评估指标计算等关键步骤。
部署运行你感兴趣的模型镜像
import xgboost as xgb
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score

train_data = pd.read_csv(‘train.csv’) # 读取数据
y = train_data.pop(‘30’).values # 用pop方式将训练数据中的标签值y取出来,作为训练目标,这里的‘30’是标签的列名
col = train_data.columns
x = train_data[col].values # 剩下的列作为训练数据
train_x, valid_x, train_y, valid_y = train_test_split(x, y, test_size=0.333, random_state=0) # 分训练集和验证集

parameters = {
‘max_depth’: [5, 10, 15, 20, 25],
‘learning_rate’: [0.01, 0.02, 0.05, 0.1, 0.15],
‘n_estimators’: [500, 1000, 2000, 3000, 5000],
‘min_child_weight’: [0, 2, 5, 10, 20],
‘max_delta_step’: [0, 0.2, 0.6, 1, 2],
‘subsample’: [0.6, 0.7, 0.8, 0.85, 0.95],
‘colsample_bytree’: [0.5, 0.6, 0.7, 0.8, 0.9],
‘reg_alpha’: [0, 0.25, 0.5, 0.75, 1],
‘reg_lambda’: [0.2, 0.4, 0.6, 0.8, 1],
‘scale_pos_weight’: [0.2, 0.4, 0.6, 0.8, 1]

}

xlf = xgb.XGBClassifier(max_depth=10,
learning_rate=0.01,
n_estimators=2000,
silent=True,
objective=‘binary:logistic’,
nthread=-1,
gamma=0,
min_child_weight=1,
max_delta_step=0,
subsample=0.85,
colsample_bytree=0.7,
colsample_bylevel=1,
reg_alpha=0,
reg_lambda=1,
scale_pos_weight=1,
seed=1440,
missing=None)

gsearch = GridSearchCV(xlf, param_grid=parameters, scoring=‘accuracy’, cv=3)
gsearch.fit(train_x, train_y)

print(“Best score: %0.3f” % gsearch.best_score_)
print(“Best parameters set:”)
best_parameters = gsearch.best_estimator_.get_params()
for param_name in sorted(parameters.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))

数据下载地址

您可能感兴趣的与本文相关的镜像

Yolo-v5

Yolo-v5

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值