2026-03-11 22:54:52 +08:00
|
|
|
# %% md
|
2026-03-24 23:35:31 +08:00
|
|
|
# # LightGBM 回归训练流程(模块化版本)
|
|
|
|
|
#
|
|
|
|
|
# 使用新的模块化 Trainer 架构,代码更简洁、可维护性更高。
|
|
|
|
|
# %% md
|
2026-03-11 22:54:52 +08:00
|
|
|
# ## 1. 导入依赖
|
|
|
|
|
# %%
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from src.factors import FactorEngine
|
|
|
|
|
from src.training import (
|
2026-03-24 23:35:31 +08:00
|
|
|
FactorManager,
|
|
|
|
|
DataPipeline,
|
|
|
|
|
RegressionTask,
|
2026-03-11 22:54:52 +08:00
|
|
|
NullFiller,
|
2026-03-24 23:35:31 +08:00
|
|
|
Winsorizer,
|
|
|
|
|
StandardScaler,
|
2026-03-11 22:54:52 +08:00
|
|
|
)
|
2026-03-26 00:15:30 +08:00
|
|
|
from src.training.core.trainer_v2 import Trainer
|
2026-03-24 23:35:31 +08:00
|
|
|
from src.training.components.filters import STFilter
|
2026-03-15 05:46:19 +08:00
|
|
|
from src.experiment.common import (
|
|
|
|
|
SELECTED_FACTORS,
|
|
|
|
|
FACTOR_DEFINITIONS,
|
|
|
|
|
get_label_factor,
|
|
|
|
|
TRAIN_START,
|
|
|
|
|
TRAIN_END,
|
|
|
|
|
VAL_START,
|
|
|
|
|
VAL_END,
|
|
|
|
|
TEST_START,
|
|
|
|
|
TEST_END,
|
|
|
|
|
stock_pool_filter,
|
|
|
|
|
STOCK_FILTER_REQUIRED_COLUMNS,
|
|
|
|
|
OUTPUT_DIR,
|
|
|
|
|
SAVE_PREDICTIONS,
|
2026-03-16 22:50:47 +08:00
|
|
|
SAVE_MODEL,
|
|
|
|
|
get_model_save_path,
|
|
|
|
|
save_model_with_factors,
|
2026-03-15 05:46:19 +08:00
|
|
|
TOP_N,
|
|
|
|
|
)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-16 22:50:47 +08:00
|
|
|
# 训练类型标识
|
|
|
|
|
TRAINING_TYPE = "regression"
|
|
|
|
|
|
2026-03-11 22:54:52 +08:00
|
|
|
# %% md
|
2026-03-24 23:35:31 +08:00
|
|
|
# ## 2. 训练特定配置
|
2026-03-11 22:54:52 +08:00
|
|
|
# %%
|
2026-03-24 23:35:31 +08:00
|
|
|
# Label 配置
|
2026-03-11 22:54:52 +08:00
|
|
|
LABEL_NAME = "future_return_5"
|
2026-03-15 05:46:19 +08:00
|
|
|
LABEL_FACTOR = get_label_factor(LABEL_NAME)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 排除的因子列表
|
|
|
|
|
EXCLUDED_FACTORS = [
|
|
|
|
|
"GTJA_alpha010",
|
|
|
|
|
"GTJA_alpha005",
|
|
|
|
|
"GTJA_alpha036",
|
|
|
|
|
"GTJA_alpha027",
|
|
|
|
|
"GTJA_alpha044",
|
|
|
|
|
"GTJA_alpha073",
|
|
|
|
|
"GTJA_alpha104",
|
|
|
|
|
"GTJA_alpha103",
|
|
|
|
|
"GTJA_alpha105",
|
|
|
|
|
"GTJA_alpha092",
|
|
|
|
|
"GTJA_alpha087",
|
|
|
|
|
"GTJA_alpha085",
|
|
|
|
|
"GTJA_alpha062",
|
|
|
|
|
"GTJA_alpha124",
|
|
|
|
|
"GTJA_alpha133",
|
|
|
|
|
"GTJA_alpha131",
|
|
|
|
|
"GTJA_alpha117",
|
|
|
|
|
"GTJA_alpha157",
|
|
|
|
|
"GTJA_alpha162",
|
|
|
|
|
"GTJA_alpha177",
|
|
|
|
|
"GTJA_alpha180",
|
|
|
|
|
"GTJA_alpha191",
|
|
|
|
|
]
|
|
|
|
|
|
2026-03-11 22:54:52 +08:00
|
|
|
# 模型参数配置
|
|
|
|
|
MODEL_PARAMS = {
|
2026-03-18 20:57:02 +08:00
|
|
|
# 基础设置
|
2026-03-24 23:35:31 +08:00
|
|
|
"objective": "regression_l1",
|
2026-03-18 20:57:02 +08:00
|
|
|
"metric": "mae",
|
2026-03-24 23:35:31 +08:00
|
|
|
# 树结构约束
|
2026-03-18 20:57:02 +08:00
|
|
|
"max_depth": 5,
|
2026-03-24 23:35:31 +08:00
|
|
|
"num_leaves": 24,
|
|
|
|
|
"min_data_in_leaf": 100,
|
|
|
|
|
# 学习参数
|
2026-03-18 20:57:02 +08:00
|
|
|
"learning_rate": 0.01,
|
2026-03-24 23:35:31 +08:00
|
|
|
"n_estimators": 1500,
|
|
|
|
|
# 随机采样
|
2026-03-18 20:57:02 +08:00
|
|
|
"subsample": 0.8,
|
2026-03-24 23:35:31 +08:00
|
|
|
"subsample_freq": 1,
|
2026-03-18 20:57:02 +08:00
|
|
|
"colsample_bytree": 0.8,
|
2026-03-24 23:35:31 +08:00
|
|
|
# 正则化
|
|
|
|
|
"reg_alpha": 0.5,
|
2026-03-18 20:57:02 +08:00
|
|
|
"reg_lambda": 1.0,
|
|
|
|
|
# 杂项
|
2026-03-11 22:54:52 +08:00
|
|
|
"verbose": -1,
|
|
|
|
|
"random_state": 42,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 日期范围配置
|
|
|
|
|
date_range = {
|
|
|
|
|
"train": (TRAIN_START, TRAIN_END),
|
|
|
|
|
"val": (VAL_START, VAL_END),
|
|
|
|
|
"test": (TEST_START, TEST_END),
|
|
|
|
|
}
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 输出配置
|
|
|
|
|
output_config = {
|
|
|
|
|
"output_dir": OUTPUT_DIR,
|
|
|
|
|
"output_filename": "regression_output.csv",
|
|
|
|
|
"save_predictions": SAVE_PREDICTIONS,
|
|
|
|
|
"save_model": SAVE_MODEL,
|
|
|
|
|
"model_save_path": get_model_save_path(TRAINING_TYPE),
|
|
|
|
|
"top_n": TOP_N,
|
|
|
|
|
}
|
2026-03-11 22:54:52 +08:00
|
|
|
|
|
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
def main():
|
|
|
|
|
"""主函数"""
|
|
|
|
|
print("\n" + "=" * 80)
|
|
|
|
|
print("LightGBM 回归模型训练(模块化版本)")
|
|
|
|
|
print("=" * 80)
|
2026-03-23 21:10:15 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 1. 创建 FactorEngine
|
|
|
|
|
print("\n[1] 创建 FactorEngine")
|
|
|
|
|
engine = FactorEngine()
|
|
|
|
|
|
|
|
|
|
# 2. 创建 FactorManager
|
|
|
|
|
print("\n[2] 创建 FactorManager")
|
|
|
|
|
factor_manager = FactorManager(
|
|
|
|
|
selected_factors=SELECTED_FACTORS,
|
|
|
|
|
factor_definitions=FACTOR_DEFINITIONS,
|
|
|
|
|
label_factor=LABEL_FACTOR,
|
|
|
|
|
excluded_factors=EXCLUDED_FACTORS,
|
2026-03-11 22:54:52 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 3. 创建 DataPipeline
|
|
|
|
|
print("\n[3] 创建 DataPipeline")
|
|
|
|
|
pipeline = DataPipeline(
|
|
|
|
|
factor_manager=factor_manager,
|
|
|
|
|
processor_configs=[
|
|
|
|
|
(NullFiller, {"strategy": "mean"}),
|
|
|
|
|
(Winsorizer, {"lower": 0.01, "upper": 0.99}),
|
|
|
|
|
(StandardScaler, {}),
|
|
|
|
|
],
|
|
|
|
|
filters=[STFilter(data_router=engine.router)],
|
|
|
|
|
stock_pool_filter_func=stock_pool_filter,
|
|
|
|
|
stock_pool_required_columns=STOCK_FILTER_REQUIRED_COLUMNS,
|
|
|
|
|
)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 4. 创建 RegressionTask
|
|
|
|
|
print("\n[4] 创建 RegressionTask")
|
|
|
|
|
task = RegressionTask(
|
|
|
|
|
model_params=MODEL_PARAMS,
|
|
|
|
|
label_name=LABEL_NAME,
|
|
|
|
|
)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 5. 创建 Trainer
|
|
|
|
|
print("\n[5] 创建 Trainer")
|
|
|
|
|
trainer = Trainer(
|
|
|
|
|
data_pipeline=pipeline,
|
|
|
|
|
task=task,
|
|
|
|
|
output_config=output_config,
|
|
|
|
|
verbose=True,
|
|
|
|
|
)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 6. 执行训练
|
|
|
|
|
print("\n[6] 执行训练")
|
|
|
|
|
results = trainer.run(engine=engine, date_range=date_range)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
# 7. 保存模型和因子信息(如果启用)
|
|
|
|
|
if SAVE_MODEL:
|
|
|
|
|
print("\n[7] 保存模型和因子信息")
|
|
|
|
|
save_model_with_factors(
|
|
|
|
|
model=task.get_model(),
|
|
|
|
|
model_path=output_config["model_save_path"],
|
|
|
|
|
selected_factors=SELECTED_FACTORS,
|
|
|
|
|
factor_definitions=FACTOR_DEFINITIONS,
|
|
|
|
|
fitted_processors=pipeline.get_fitted_processors(),
|
2026-03-11 22:54:52 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
print("\n" + "=" * 80)
|
|
|
|
|
print("训练流程完成!")
|
|
|
|
|
print(f"结果保存路径: {os.path.join(OUTPUT_DIR, 'regression_output.csv')}")
|
|
|
|
|
print("=" * 80)
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
return results
|
2026-03-11 22:54:52 +08:00
|
|
|
|
2026-03-16 22:50:47 +08:00
|
|
|
|
2026-03-24 23:35:31 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|