核心创新:
分裂公式:
关键技术:
生长策略:
class LeafWiseGrower:
def split(self):
while depth < max_depth:
find_best_split(current_leaves)
split(max_gain_leaf)
核心创新:
目标编码公式:
参数类型 | 参数名 | 推荐范围 | 作用说明 |
---|---|---|---|
基础参数 | booster | gbtree/dart | 基模型类型选择 |
树结构 | max_depth | 3-10 | 控制树复杂度 |
正则化 | reg_lambda | 0-5 | L2正则化系数 |
采样 | subsample | 0.6-1.0 | 样本采样比例 |
学习控制 | learning_rate | 0.01-0.3 | 学习率 |
xgb_model = XGBClassifier(
n_estimators=500,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
reg_lambda=1.5
)
参数类型 | 参数名 | 推荐范围 | 特殊说明 |
---|---|---|---|
树结构 | num_leaves | < 2^max_depth | 控制叶子数量 |
采样 | feature_fraction | 0.6-1.0 | 特征采样比例 |
优化 | min_data_in_leaf | 20-200 | 防止过拟合 |
加速 | max_bin | 64-255 | 直方图分桶数 |
lgb_model = LGBMClassifier(
num_leaves=31,
min_child_samples=20,
feature_fraction=0.7,
bagging_freq=5
)
参数类型 | 参数名 | 推荐值 | 功能说明 |
---|---|---|---|
类别处理 | cat_features | 自动检测 | 指定类别特征列 |
过拟合 | l2_leaf_reg | 3-10 | 正则化系数 |
速度优化 | thread_count | -1 | 使用全部CPU核心 |
训练控制 | early_stopping_rounds | 50 | 早停轮数 |
cat_model = CatBoostClassifier(
iterations=1000,
depth=8,
cat_features=cat_cols,
learning_rate=0.05
)
from sklearn.model_selection import train_test_split
data = pd.read_csv('titanic.csv')
X = data.drop('Survived', axis=1)
y = data['Survived']
# 自动处理类别特征
cat_cols = ['Sex', 'Embarked', 'Pclass']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# XGBoost需要手动编码
xgb_model.fit(onehot_x_train, y_train)
# LightGBM自动处理类别
lgb_model.fit(X_train, y_train, categorical_feature=cat_cols)
# CatBoost自动检测类别
cat_model.fit(X_train, y_train, verbose=100)
模型 | 训练时间 | 测试集F1 | 内存消耗 |
---|---|---|---|
XGBoost | 2.1s | 0.812 | 850MB |
LightGBM | 1.3s | 0.826 | 420MB |
CatBoost | 3.8s | 0.819 | 1.2GB |
param_grid = {
'learning_rate': [0.05, 0.1, 0.2],
'max_depth': [4, 6, 8],
'subsample': [0.6, 0.8, 1.0]
}
grid_search = HalvingGridSearchCV(
estimator=lgb_model,
param_grid=param_grid,
factor=2,
min_resources=500
)
def dynamic_rate(epoch):
base_rate = 0.1
decay = 0.98
return base_rate * (decay ** epoch)
xgb_model.set_params(learning_rate=dynamic_rate)
plot_importance(cat_model,
title='CatBoost Feature Importance',
max_num_features=15,
height=0.8)
from onnxmltools import convert_lightgbm
onnx_model = convert_lightgbm(lgb_model,
initial_types=[('input', FloatTensorType([None, 12]))]
# 启动CatBoost REST服务
catboost serve --model-file model.cbm --port 8080
# 量化LightGBM模型
from m2cgen import export_to_c
c_code = export_to_c(lgb_model)
graph TD
A[数据规模] -->|>1M样本|
B(LightGBM) A -->|<100K样本|
C{特征类型} C -->|连续特征为主|
D(XGBoost) C -->|类别特征多|
E(CatBoost) B --> F{是否需要快速迭代}
F -->|是| G(LightGBM+直方图) F -->|否| H(考虑CatBoost)
三大框架各有千秋,实际应用中建议:
最新基准测试显示,在Kaggle结构化数据比赛中:
建议持续关注各框架的更新动态,根据具体场景选择最优工具。