【GridSearch】 简单实现并记录运行效果

记录了使用for循环实现网格搜索的简单框架。
使用df_search记录每种超参数组合下的运行结果。
lgb_model.best_score返回模型的最佳得分
lgb_model.best_iteration_返回模型的最佳iteration也就是最佳n_extimator

import numpy as np
import pandas as pd
import lightgbm as lgb

df = pd.read_csv("this_is_train.csv")
df_search_columns = ['learning_rate', 'num_leaves', 'max_depth','subsample','colsample_bytree','best_iteration','best_score']
df_search =  pd.DataFrame(columns=df_search_columns )
# colsample_bytree :0.9, learning_rate : 0.001
lgb_params = {
    "objective": "mae", # "mae"
    "n_estimators": 6000,
    "num_leaves": 256, # 256
    "subsample": 0.6,
    "colsample_bytree": 0.8,
    "learning_rate": 0.00571, # 0.00871
    'max_depth': 11, # 11
    "n_jobs": 4,
    "device": "gpu",
    "verbosity": -1,
    "importance_type": "gain",
}
for learning_rate in [0.001,0.005,0.01,0.015,0.05]:
    for num_leaves in [300,256,200,150]:
        for max_depth in [15,13,11,9,7]:
            for subsample in [0.8,0.6,0.5]:
                for colsample_bytree in [0.9,0.8,0.7]:
                    print(f"learning_rate : {learning_rate}, num_leaves : {num_leaves}, max_depth:{max_depth}, subsample : {subsample}, colsample_bytree : {colsample_bytree}")
                    lgb_params['learning_rate'] = learning_rate
                    lgb_params['num_leaves'] = num_leaves
                    lgb_params['max_depth'] = max_depth
                    lgb_params['subsample'] = subsample
                    lgb_params['colsample_bytree'] = colsample_bytree
                    # Train a LightGBM model for the current fold
                    lgb_model = lgb.LGBMRegressor(**lgb_params)
                    lgb_model.fit(
                        train_feats,
                        train_target,
                        eval_set=[(valid_feats, valid_target)],
                        callbacks=[
                            lgb.callback.early_stopping(stopping_rounds=100),
                            lgb.callback.log_evaluation(period=100),
                        ],
                    )
                    best_iteration = lgb_model.best_iteration_
                    best_score = lgb_model.best_score_
                    
                    cache = pd.DataFrame([[learning_rate,num_leaves,max_depth,subsample,colsample_bytree,best_iteration,best_score]],columns=['learning_rate', 'num_leaves', 'max_depth','subsample','colsample_bytree','best_iteration','best_score'])
                    df_search = pd.concat([df_search, cache], ignore_index=True, axis=0)
df_search.to_csv('grid_search.csv',index=False)

使用该框架,需要调整训练数据df部分,以及进行网格的备选数据和lightgbm的超参数。
每次运行的数据通过一下代码进行记录

cache = pd.DataFrame([[learning_rate,num_leaves,max_depth,subsample,colsample_bytree,best_iteration,best_score]],\
	 columns=df_search_columns )
df_search = pd.concat([df_search, cache], ignore_index=True, axis=0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值