Optuna - 自动调参利器python实例
文章目录
- 介绍
- 核心概念
- 简单demo
- Optuna+LightGBM应用实例
介绍
Optuna 是一个开源的 Python 超参数优化框架,专为机器学习设计,用于自动搜索模型的最优超参数组合。
核心概念
Objective(目标函数):定义要优化的目标,例如验证集上的准确率。
Trial(试验):每次尝试一组超参数组合的过程。
Study(研究):管理所有试验的对象,负责调用优化算法并记录结果。
使用前记得安装一下。
pip install optuna
简单demo
import optunaprint("======================1.Optuna 简单介绍======================")# 定义目标函数
def objective(trial):x = trial.suggest_float('x', -10, 10) # 搜索范围return (x - 2) ** 2 # 最小化目标# 创建 study 对象并开始优化
study = optuna.create_study(direction='minimize')
study.optimize(objective, n_trials=30)# 输出最优参数
print("最优参数:", study.best_params)
print("最优值:", study.best_value)
结果:
(freq) test1@budas-MacBook-Pro qlib_data % python open_source/optuna_LGB_demo.py
======================1.Optuna 简单介绍======================
[I 2025-10-21 17:39:46,669] A new study created in memory with name: no-name-c249048c-a266-491f-80e7-d267f6aaf70f
[I 2025-10-21 17:39:46,695] Trial 0 finished with value: 0.27998809080239234 and parameters: {'x': 2.5291390089592642}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,696] Trial 1 finished with value: 125.98440450310646 and parameters: {'x': -9.224277460180074}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,696] Trial 2 finished with value: 10.387925622493237 and parameters: {'x': 5.223030502879741}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,697] Trial 3 finished with value: 49.54201687322145 and parameters: {'x': -5.038609015510199}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,697] Trial 4 finished with value: 4.181105109194839 and parameters: {'x': -0.04477507545324855}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,697] Trial 5 finished with value: 11.827980383032994 and parameters: {'x': -1.4391830982128582}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,698] Trial 6 finished with value: 36.186151610135 and parameters: {'x': 8.015492632373096}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,698] Trial 7 finished with value: 8.40897371950153 and parameters: {'x': -0.8998230496879511}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,698] Trial 8 finished with value: 52.46010967373563 and parameters: {'x': -5.242935155980318}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,699] Trial 9 finished with value: 9.92697169684248 and parameters: {'x': 5.150709713198358}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,703] Trial 10 finished with value: 58.535592000265794 and parameters: {'x': 9.650855638441088}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,707] Trial 11 finished with value: 0.3991503767560413 and parameters: {'x': 2.6317834888282863}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,709] Trial 12 finished with value: 2.036644368185376 and parameters: {'x': 3.427110496137344}. Best is trial 0 with value: 0.27998809080239234.
[I 2025-10-21 17:39:46,711] Trial 13 finished with value: 0.18818624666616215 and parameters: {'x': 2.4338043875598334}. Best is trial 13 with value: 0.18818624666616215.
[I 2025-10-21 17:39:46,713] Trial 14 finished with value: 0.15235611113979747 and parameters: {'x': 2.3903282095106597}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,717] Trial 15 finished with value: 35.74508236664194 and parameters: {'x': -3.9787191242474282}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,719] Trial 16 finished with value: 13.949387048126138 and parameters: {'x': 5.734887822696438}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,721] Trial 17 finished with value: 1.343467107918743 and parameters: {'x': 0.840919714636323}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,724] Trial 18 finished with value: 18.485802198722393 and parameters: {'x': -2.2995118558648477}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,726] Trial 19 finished with value: 27.98290664301264 and parameters: {'x': 7.289887205131376}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,728] Trial 20 finished with value: 0.28831921612170813 and parameters: {'x': 1.463046355704975}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,729] Trial 21 finished with value: 3.997927581539917 and parameters: {'x': 3.999481828259491}. Best is trial 14 with value: 0.15235611113979747.
[I 2025-10-21 17:39:46,731] Trial 22 finished with value: 0.01712334648370329 and parameters: {'x': 2.1308562053694944}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,733] Trial 23 finished with value: 0.4149523968471933 and parameters: {'x': 1.355832011935401}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,734] Trial 24 finished with value: 24.776169382398887 and parameters: {'x': -2.9775666125526525}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,736] Trial 25 finished with value: 23.073702303716598 and parameters: {'x': 6.803509373751299}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,738] Trial 26 finished with value: 3.8528583080253096 and parameters: {'x': 3.9628699162260625}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,740] Trial 27 finished with value: 4.534397815499067 and parameters: {'x': -0.12941255173793564}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,742] Trial 28 finished with value: 0.033680841845138504 and parameters: {'x': 2.1835234095289713}. Best is trial 22 with value: 0.01712334648370329.
[I 2025-10-21 17:39:46,744] Trial 29 finished with value: 0.0020967516155972446 and parameters: {'x': 2.045790300453232}. Best is trial 29 with value: 0.0020967516155972446.
最优参数: {'x': 2.045790300453232}
最优值: 0.0020967516155972446
可以发现,能自动计算出最有参数以及最优值。
Optuna+LightGBM应用实例
我们接下来用一个更复杂的例子来演示一下。
代码
print("======================2.Optuna+LightGBM应用实例======================")
import optuna
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier
from sklearn.metrics import accuracy_score# 加载数据
X, y = load_breast_cancer(return_X_y=True)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)# 定义目标函数
def objective(trial):params = {'objective': 'binary','metric': 'binary_logloss','boosting_type': 'gbdt','num_leaves': trial.suggest_int('num_leaves', 10, 100),'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3),'feature_fraction': trial.suggest_float('feature_fraction', 0.5, 1.0),'bagging_fraction': trial.suggest_float('bagging_fraction', 0.5, 1.0),'bagging_freq': trial.suggest_int('bagging_freq', 1, 7),'min_child_samples': trial.suggest_int('min_child_samples', 5, 30),}model = LGBMClassifier(**params, verbose=-1)model.fit(X_train, y_train)preds = model.predict(X_val)return accuracy_score(y_val, preds)# 创建 study 并优化
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=30)print("最佳参数:", study.best_params)
print("最佳准确率:", study.best_value)
运行结果:
======================2.Optuna+LightGBM应用实例======================
[I 2025-10-21 17:39:49,592] A new study created in memory with name: no-name-ad1d186b-5880-4911-a6b6-34ebf3eb46b9
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:49,736] Trial 0 finished with value: 0.9649122807017544 and parameters: {'num_leaves': 51, 'learning_rate': 0.10146529871625382, 'feature_fraction': 0.6810955020926299, 'bagging_fraction': 0.9248849739678474, 'bagging_freq': 7, 'min_child_samples': 7}. Best is trial 0 with value: 0.9649122807017544.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:49,787] Trial 1 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 25, 'learning_rate': 0.19000689288456704, 'feature_fraction': 0.9402995039136698, 'bagging_fraction': 0.6427242258404631, 'bagging_freq': 6, 'min_child_samples': 29}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:49,835] Trial 2 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 65, 'learning_rate': 0.17466652524171508, 'feature_fraction': 0.6219225736241943, 'bagging_fraction': 0.8208525703005924, 'bagging_freq': 2, 'min_child_samples': 27}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:49,932] Trial 3 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 83, 'learning_rate': 0.19098915460773408, 'feature_fraction': 0.5225210038168472, 'bagging_fraction': 0.8009703689018921, 'bagging_freq': 4, 'min_child_samples': 5}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:49,986] Trial 4 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 28, 'learning_rate': 0.07994810254597592, 'feature_fraction': 0.8696447502279574, 'bagging_fraction': 0.563949349224018, 'bagging_freq': 1, 'min_child_samples': 25}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,035] Trial 5 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 58, 'learning_rate': 0.23342135963581992, 'feature_fraction': 0.6175671469951229, 'bagging_fraction': 0.6461224727783421, 'bagging_freq': 1, 'min_child_samples': 13}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,086] Trial 6 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 74, 'learning_rate': 0.23960115064603174, 'feature_fraction': 0.589471260788873, 'bagging_fraction': 0.747266033660389, 'bagging_freq': 3, 'min_child_samples': 26}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,165] Trial 7 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 80, 'learning_rate': 0.2307905180075635, 'feature_fraction': 0.9519393622830914, 'bagging_fraction': 0.668378346933991, 'bagging_freq': 2, 'min_child_samples': 6}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,209] Trial 8 finished with value: 0.9649122807017544 and parameters: {'num_leaves': 88, 'learning_rate': 0.08527642314036804, 'feature_fraction': 0.9725356807067478, 'bagging_fraction': 0.6220236801865662, 'bagging_freq': 1, 'min_child_samples': 23}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,255] Trial 9 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 83, 'learning_rate': 0.2956436065747412, 'feature_fraction': 0.7475038213635878, 'bagging_fraction': 0.9303491455799764, 'bagging_freq': 5, 'min_child_samples': 19}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,299] Trial 10 finished with value: 0.956140350877193 and parameters: {'num_leaves': 14, 'learning_rate': 0.026448965131904673, 'feature_fraction': 0.8354621403248779, 'bagging_fraction': 0.5097429408779779, 'bagging_freq': 7, 'min_child_samples': 30}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,351] Trial 11 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 44, 'learning_rate': 0.15516272347603896, 'feature_fraction': 0.7653126929679486, 'bagging_fraction': 0.8218819145047515, 'bagging_freq': 5, 'min_child_samples': 30}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,409] Trial 12 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 34, 'learning_rate': 0.17210791380662516, 'feature_fraction': 0.6872212211607364, 'bagging_fraction': 0.7450231677863568, 'bagging_freq': 6, 'min_child_samples': 21}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,504] Trial 13 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 66, 'learning_rate': 0.12701640611087575, 'feature_fraction': 0.8937447336510852, 'bagging_fraction': 0.8572031783733666, 'bagging_freq': 3, 'min_child_samples': 15}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,549] Trial 14 finished with value: 0.9649122807017544 and parameters: {'num_leaves': 10, 'learning_rate': 0.19735490107082176, 'feature_fraction': 0.7807927557747516, 'bagging_fraction': 0.999355190595854, 'bagging_freq': 5, 'min_child_samples': 27}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,595] Trial 15 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 100, 'learning_rate': 0.28569226403767434, 'feature_fraction': 0.5379892228421306, 'bagging_fraction': 0.7130767908046247, 'bagging_freq': 3, 'min_child_samples': 30}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,645] Trial 16 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 28, 'learning_rate': 0.1316521115943812, 'feature_fraction': 0.6361828744137547, 'bagging_fraction': 0.5807067915562575, 'bagging_freq': 6, 'min_child_samples': 23}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,708] Trial 17 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 41, 'learning_rate': 0.20536440444030563, 'feature_fraction': 0.7011148381538741, 'bagging_fraction': 0.8772250913395302, 'bagging_freq': 4, 'min_child_samples': 17}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,770] Trial 18 finished with value: 0.9649122807017544 and parameters: {'num_leaves': 61, 'learning_rate': 0.26191953569455795, 'feature_fraction': 0.9142819507516637, 'bagging_fraction': 0.7042592361715225, 'bagging_freq': 2, 'min_child_samples': 11}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,824] Trial 19 finished with value: 0.956140350877193 and parameters: {'num_leaves': 21, 'learning_rate': 0.022814914133082648, 'feature_fraction': 0.8228011464331846, 'bagging_fraction': 0.8082318249459327, 'bagging_freq': 6, 'min_child_samples': 27}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,876] Trial 20 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 49, 'learning_rate': 0.16280323671592928, 'feature_fraction': 0.9937678827000949, 'bagging_fraction': 0.5087152529534642, 'bagging_freq': 4, 'min_child_samples': 20}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:50,956] Trial 21 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 70, 'learning_rate': 0.19677062303190243, 'feature_fraction': 0.5016578737721127, 'bagging_fraction': 0.8024291907002417, 'bagging_freq': 2, 'min_child_samples': 10}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,040] Trial 22 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 96, 'learning_rate': 0.17879349995784838, 'feature_fraction': 0.5625437589329721, 'bagging_fraction': 0.7744429815194476, 'bagging_freq': 4, 'min_child_samples': 9}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,109] Trial 23 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 88, 'learning_rate': 0.12633266756680206, 'feature_fraction': 0.5038696404236698, 'bagging_fraction': 0.8630742792879055, 'bagging_freq': 5, 'min_child_samples': 17}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,167] Trial 24 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 77, 'learning_rate': 0.21283332264919333, 'feature_fraction': 0.6412244384344534, 'bagging_fraction': 0.7071577029026193, 'bagging_freq': 3, 'min_child_samples': 24}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,216] Trial 25 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 64, 'learning_rate': 0.26042378529769145, 'feature_fraction': 0.5596662710955554, 'bagging_fraction': 0.907446037501596, 'bagging_freq': 6, 'min_child_samples': 28}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,283] Trial 26 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 56, 'learning_rate': 0.18076572628653886, 'feature_fraction': 0.5966753247972406, 'bagging_fraction': 0.7759633005652709, 'bagging_freq': 2, 'min_child_samples': 13}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,348] Trial 27 finished with value: 0.9649122807017544 and parameters: {'num_leaves': 70, 'learning_rate': 0.14442906691243748, 'feature_fraction': 0.7174701928653857, 'bagging_fraction': 0.8341762685444376, 'bagging_freq': 7, 'min_child_samples': 22}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,394] Trial 28 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 88, 'learning_rate': 0.21766367750285082, 'feature_fraction': 0.6506797487619478, 'bagging_fraction': 0.5918461311166756, 'bagging_freq': 4, 'min_child_samples': 28}. Best is trial 1 with value: 0.9736842105263158.
/Users/test1/py_env/freq/lib/python3.11/site-packages/sklearn/utils/validation.py:2749: UserWarning: X does not have valid feature names, but LGBMClassifier was fitted with feature nameswarnings.warn(
[I 2025-10-21 17:39:51,524] Trial 29 finished with value: 0.9736842105263158 and parameters: {'num_leaves': 51, 'learning_rate': 0.10649802988972856, 'feature_fraction': 0.8166349764469313, 'bagging_fraction': 0.9562720109081992, 'bagging_freq': 4, 'min_child_samples': 8}. Best is trial 1 with value: 0.9736842105263158.
最佳参数: {'num_leaves': 25, 'learning_rate': 0.19000689288456704, 'feature_fraction': 0.9402995039136698, 'bagging_fraction': 0.6427242258404631, 'bagging_freq': 6, 'min_child_samples': 29}
最佳准确率: 0.9736842105263158
我们可以进行多个参数的调参,运行30次,最终能得到一个不错的lightGBM结果。
相比于网格搜索
,或者手动for循环的话,Optuna的遍历次数明显要少很多,效果也差不多,是一种比较高效的调参方法。
代码放在:https://github.com/JizhiXiang/Quant-Strategy