XGBoost、LightGBM、Catboost对比

本文主要参考Battle of the Boosting Algos: LGB, XGB, Catboost,结果与原文有出入。

1. 对比标准

1.1 数据集

  • 分类Fashion MNIST(60000条数据784个特征)
  • 回归NYC Taxi fares(60000条数据7个特征)
  • 大规模数据集:NYC Taxi fares(2百万条数据7个特征)

PS:本文只进行了分类的对比

1.2 规则

  1. 使用基准模型
  2. 使用相同参数训练并利用GridSearchCV调参
  3. 比较训练和预测耗时、预测分数、可解释性

1.3 版本

xgboost==0.90
lightgbm==2.3.1
catboost==0.21

2. 结果

2.1 准确率

LightGBM>XGBoost>CatBoost
在这里插入图片描述

2.2 训练时间和预测时间

CatBoost<LightGBM<XGBoost
在这里插入图片描述
在这里插入图片描述

2.3 可解释性

XGBoost=LightGBM>Catboost

2.3.1 特征重要性

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.3.2 SHAP值

类别含义
0T-shirt/top
1Trouser
2Pullover
3Dress
4Coat
5Sandal
6Shirt
7Sneaker
8Bag
9Ankle boot

XGBoost
在这里插入图片描述

LightGBM
在这里插入图片描述
CatBoost无法开箱即用

2.3.3 可视化二叉树

XGBoost
在这里插入图片描述

LightGBM
在这里插入图片描述

CatBoost绘制树函数

3. 总结

比赛选LightGBM,工业选Catboost

4. 代码

https://download.csdn.net/download/lly1122334/12171980

参考文献

  1. Battle of the Boosting Algos: LGB, XGB, Catboost
  2. Battle of the Boosting Algorithms
  3. mlxtend: A library of extension and helper modules for Python’s data analysis and machine learning libraries
  4. shap: A game theoretic approach to explain the output of any machine learning model
  5. http://www.picnet.com.au/blogs/guido/post/2016/09/22/xgboost-windows-x64-binaries-for-download/
  6. Graphviz - Graph Visualization Software Windows Packages

备份

# Feature Engineering
# this cell was adapted from https://www.kaggle.com/mahtieu/nyc-taxi-fare-prediction-data-expl-xgboost
def feature_engineering(df):
    df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'])
    #Drop rows with null values
    df = df.dropna(how = 'any', axis = 'rows')
    #Free rides, negative fares and passenger count filtering
    df = df[df.eval('(fare_amount > 0) & (passenger_count <= 6)')]
    # Coordinates filtering - Pickup and dropoff locations should be within the limits of NYC
    df = df[(df.pickup_longitude >= -77) &
                  (df.pickup_longitude <= -70) &
                  (df.dropoff_longitude >= -77) &
                  (df.dropoff_longitude <= 70) &
                  (df.pickup_latitude >= 35) &
                  (df.pickup_latitude <= 45) &
                  (df.dropoff_latitude >= 35) &
                  (df.dropoff_latitude <= 45)]

    df.pickup_datetime = df.pickup_datetime.dt.tz_convert('UTC')
    df.pickup_datetime = df.pickup_datetime.dt.tz_convert('America/New_York')

    # Fares may change every year
    df['year'] = df.pickup_datetime.dt.year

    # Different fares during weekdays and weekends
    df['dayofweek'] = df.pickup_datetime.dt.dayofweek

    # Different fares during public holidays
    df['dayofyear'] = df.pickup_datetime.dt.dayofyear

    # Different fares in peak periods and off-peak periods
    df['hourofday'] = df.pickup_datetime.dt.hour

    df = df.drop('pickup_datetime', axis=1)

    # Computes the distance (in miles) between the pickup and the dropoff locations
    df['distance'] = df.apply(
        lambda x: distance.distance((x.pickup_latitude, x.pickup_longitude), (x.dropoff_latitude, x.dropoff_longitude)).miles,
        axis = 1)

    df = df[df.eval('(distance > 0) & (distance < 150)')]
    fare_distance_ratio = (df.fare_amount/df.distance)
    fare_distance_ratio.describe()

    (fare_distance_ratio[fare_distance_ratio < 45]).hist()

    # Drop incoherent fares
    df = df[fare_distance_ratio < 45]
    del fare_distance_ratio

    # Coordinates of the 3 airpots of NYC
    airports = {'jfk': [40.6441666, -73.7822222],
                'laguardia': [40.7747222, -73.8719444],
                'newark': [40.6897222, -74.175]}

    # Computes the distance between the pickup location and the airport
    pickup = df.apply(lambda x: distance.distance((x.pickup_latitude, x.pickup_longitude), (airports.get('jfk'))).miles, axis=1)
    # Computes the distance between the dropoff location and the airport
    dropoff = df.apply(lambda x: distance.distance((x.dropoff_latitude, x.dropoff_longitude), (airports.get('jfk'))).miles, axis=1)
    # Selects the shortest distance
    df['to_jfk'] = pd.concat((pickup, dropoff), axis=1).min(axis=1)

    pickup = df.apply(lambda x: distance.distance((x.pickup_latitude, x.pickup_longitude), (airports.get('laguardia'))).miles, axis=1)
    dropoff = df.apply(lambda x: distance.distance((x.dropoff_latitude, x.dropoff_longitude), (airports.get('laguardia'))).miles, axis=1)
    df['to_laguardia'] = pd.concat((pickup, dropoff), axis=1).min(axis=1)

    pickup = df.apply(lambda x: distance.distance((x.pickup_latitude, x.pickup_longitude), (airports.get('newark'))).miles, axis=1)
    dropoff = df.apply(lambda x: distance.distance((x.dropoff_latitude, x.dropoff_longitude), (airports.get('newark'))).miles, axis=1)
    df['to_newark'] = pd.concat((pickup, dropoff), axis=1).min(axis=1)
    del pickup, dropoff
    return df

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
XGBoostLightGBMCatBoost是三种常用的梯度提升树算法,它们在机器学习竞赛和实际应用中都取得了很好的效果。下面是它们的调参介绍: 1. XGBoost调参: - 学习率(learning rate):控制每次迭代中模型权重的更新步长,一般取值范围为[0,1],较小的学习率可以使模型更加稳定,但需要更多的迭代次数。 - 树的数量(n_estimators):决定了模型中树的个数,一般需要通过交叉验证来确定合适的值。 - 树的深度(max_depth):控制每棵树的最大深度,较大的深度可以提高模型的拟合能力,但也容易导致过拟合。 - 正则化参数(lambda、alpha):用于控制模型的复杂度,防止过拟合。 - 特征子采样比例(colsample_bytree):控制每棵树在训练时随机选择的特征比例,可以降低模型的方差。 2. LightGBM调参: - 学习率(learning_rate):同样控制每次迭代中模型权重的更新步长。 - 树的数量(num_iterations):决定了模型中树的个数。 - 树的深度(max_depth):控制每棵树的最大深度。 - 叶子节点数(num_leaves):控制每棵树的叶子节点数,较大的值可以提高模型的拟合能力,但也容易导致过拟合。 - 特征子采样比例(feature_fraction):控制每棵树在训练时随机选择的特征比例。 - 数据子采样比例(bagging_fraction):控制每次迭代时随机选择的训练数据比例。 3. CatBoost调参: - 学习率(learning_rate):同样控制每次迭代中模型权重的更新步长。 - 树的数量(iterations):决定了模型中树的个数。 - 树的深度(depth):控制每棵树的最大深度。 - 正则化参数(l2_leaf_reg):用于控制模型的复杂度,防止过拟合。 - 特征子采样比例(colsample_bylevel):控制每棵树在训练时随机选择的特征比例。 - 数据子采样比例(subsample):控制每次迭代时随机选择的训练数据比例。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值