参考:https://www.freesion.com/article/76441004344/#LightGBM__sklearn__329
https://blog.csdn.net/qq_39777550/article/details/109277937
LightGBM的优点
lightgbm是xgboost的加强升级版.
LightGBM=XGBoost+Histogram+GOSS+EFB
其中,Histogram算法是直方图算法,作用:减少后选分类点的算法
GOSS是基于梯度的单边采样算法,作用减少样本数量
EFB算法是互斥特征捆绑算法,作用是减少特征数量
基于以上三个算法,LightGBM生产一片叶子需要的复杂度大大降低了,从而极大节约了计算时间。同时Histogram算法还将特征浮点数转换成0~255位的证书进行存储,从而集打节约了内存存储空间。
(1)基于leaf-wise的决策树的生长策略
对于大部分决策树算法使用的试level-wise生长策略,即同一层的叶子节点每次都一起分裂,但是实际上一些叶子节点的分裂增益较低,这样分裂会增加不小的开销。lightGBM使用的leaf-wise策略,每次在当前的叶子节点中,找出分裂增益最大的叶子节点进行分裂,而不是所有节点都进行分裂,这样可以提高精度。
(2)直方图算法
简答来说,就是先对特征值进行装箱处理,把连续的浮点特征值离散化成k个整数,形成一个一个的箱体,同时构造一个宽度为k的直方图,在遍历数据的时候,根据离散化后的值作为索引在直方图中累积统计值(因此这里试频数直方图),当遍历一次数据后,直方图累积了需要的统计量,然后根据直方图的离散值,遍历寻找最优的分割点。
对于连续的特征来说,装箱处理时特征工程中的离散化:如[0,10)区间的值都可以幅值为0,[10,20)区间的值都可以赋值为1等,这样就可以把众多的数据值划分为到有限的分箱中,在LightGBM中默认分箱数(bins)为256。对于分类的特征来说,则是每一种取值放入一个分箱中,且当取值的个数大于最大分箱数是,会忽略哪些很少出现的分类值。
(3)GOSS算法
单边梯度采样GOSS算法是通过对样本采样的方法来减少计算目标函数增益时候的复杂度。
在GOOS算法中,梯度更大的样本点在计算信息增益的时候占有更重要的作用,当我们对样本进行下采样的时候,保留这些体大较大的样本点,并随机取掉梯度小的样本。
首先把样本按照梯度排序,选出梯度最大的a%个样本,然后在剩下的小梯度样本数据中随机选取b%个样本,在计算信息增益的时候,将选出来的b%小梯度样本的信息增益扩大1-a/b的倍数。
(如果一个样本的梯度很小,证明该样本拟合较好,所以不需要再计算新的增益了。但是如果全部舍去梯度小的样本,那么精度就会下降,所以随机去掉梯度小的样本。)
(4)EFB算法:
互斥特征绑定算法,即将互斥特征绑定在一起以减少特征维度;EFB算法可以有效减少用于构建直方图的特征数量,从而降低计算复杂度,尤其是特征中包含大量稀疏特征的时候。LightGBM可以直接将每个类别取值和一个bin关联,从而自动处理它们,而无需预处理成onehot编码。
(5)并行学习
LightGBM支持特征并行和数据并行两种。传统的特征并行主要思想是在并行化决策树中寻找最佳切分点,在数据量大时难以加速,同时需要切分结果进行通信整合。LightGBM则是使用分散规则,它将直方图合并的任务分给不同的机器,降低通信和计算的开销,并利用直方图加速训练,进一步减少开销。
(6)支持类别特征
传统的机器学习一般不能支持直接输入类别特征,需要先转化成多维的0-1特征,这样无论在空间上还是时间上效率都不高。LightGBM通过更改决策树算法的决策规则,直接原生支持类别特征,不需要转化,提高了近8倍的速度。
(7)支持并行学习
LightGBM原生支持并行学习,目前支持特征并行(Featrue Parallelization)和数据并行(Data Parallelization)两种,还有一种是基于投票的数据并行(Voting Parallelization)
特征并行的主要思想是在不同机器、在不同的特征集合上分别寻找最优的分割点,然后在机器间同步最优的分割点。
数据并行则是让不同的机器先在本地构造直方图,然后进行全局的合并,最后在合并的直方图上面寻找最优分割点。
LightGBM针对这两种并行方法都做了优化。
(8)支持增量学习
每次读取文件的一部分,用于训练模型,并保存模型的训练结果;然后读取文件的另一部分,再对模型进行更新训练;迭代读取全部数据完毕,最终完成整个文件数据的训练过程。
LIGHTGBM 的 SKLEARN 风格接口
lightgbm.LGBMModel
class LGBMModel(_LGBMModelBase):
“”“Implementation of the scikit-learn API for LightGBM.”"" _def init(self, boosting_type=‘gbdt’, num_leaves=31, max_depth=-1,
learning_rate=0.1, n_estimators=100,
subsample_for_bin=200000, objective=None, class_weight=None,
min_split_gain=0., min_child_weight=1e-3, min_child_samples=20,
subsample=1., subsample_freq=0, colsample_bytree=1.,
reg_alpha=0., reg_lambda=0., random_state=None,
n_jobs=-1, silent=True, importance_type=‘split’, **kwargs):
Parameters
- boosting_type (string, optional (default=‘gbdt’))
‘gbdt’, traditional Gradient Boosting Decision Tree.
‘dart’, Dropouts meet Multiple Additive Regression Trees.
‘goss’, Gradient-based One-Side Sampling.
‘rf’, Random Forest.
-
num_leaves (int, optional (default=31),<=2^max_depth) – 每个基学习器的最大叶子节点.
-
max_depth (int, optional (default=-1)) – 每个基学习器的最大深度, -1 means no limit, 当模型过拟合,首先降低max_depth
-
learning_rate (float, optional (default=0.1)) – Boosting learning rate. You can use
callbacks
parameter offit
method to shrink/adapt learning rate in training usingreset_parameter
callback. Note, that this will ignore thelearning_rate
argument in training. -
n_estimators (int, optional (default=100)) – 基学习器的训练数量.
-
max_bin (int, optional (default=255)) feature将存入的bin的最大数量,应该是直方图的k值
-
subsample_for_bin (int, optional (default=200000)) – Number of samples for constructing bins.
-
objective (string, callable or None, optional (default=None)) – Specify the learning task and the corresponding learning objective or a custom objective function to be used (see note below). Default: ‘regression’ for LGBMRegressor, ‘binary’ or ‘multiclass’ for LGBMClassifier, ‘lambdarank’ for LGBMRanker.
-
class_weight (dict, ‘balanced’ or None, optional (default=None)) – Weights associated with classes in the form
{class_label: weight}
. Use this parameter only for multi-class classification task; for binary classification task you may useis_unbalance
orscale_pos_weight
parameters. Note, that the usage of all these parameters will result in poor estimates of the individual class probabilities. You may want to consider performing probability calibration (https://scikit-learn.org/stable/modules/calibration.html) of your model. The ‘balanced’ mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data asn_samples / (n_classes * np.bincount(y))
. If None, all classes are supposed to have weight one. Note, that these weights will be multiplied withsample_weight
(passed through thefit
method) ifsample_weight
is specified. -
min_split_gain (float, optional (default=0.)) – 树的叶子节点上进行进一步划分所需的最小损失减少.
-
min_child_weight (float, optional (default=1e-3)) – Minimum sum of instance weight (hessian) needed in a child (leaf).
-
min_child_samples (int, optional (default=20)) – 叶子节点具有的最小记录数.
-
subsample (float, optional (default=1.)) – 训练时采样一定比例的数据.
-
subsample_freq (int, optional (default=0)) – Frequency of subsample, <=0 means no enable.
-
colsample_bytree (float, optional (default=1.)) – Subsample ratio of columns when constructing each tree.
-
reg_alpha (float, optional (default=0.)) – L1 regularization term on weights.
-
reg_lambda (float, optional (default=0.)) – L2 regularization term on weights.
-
random_state (int, RandomState object or None, optional (default=None)) – Random number seed. If int, this number is used to seed the C++ code. If RandomState object (numpy), a random integer is picked based on its state to seed the C++ code. If None, default seeds in C++ code are used.
-
n_jobs (int, optional (default=-1)) – Number of parallel threads.
-
silent (bool, optional (default=True)) – Whether to print messages while running boosting.
-
importance_type (string, optional (default=‘split’)) – The type of feature importance to be filled into
feature_importances_
. If ‘split’, result contains numbers of times the feature is used in a model. If ‘gain’, result contains total gains of splits which use the feature. -
**kwargs –
LGBMRegressor引入以及重要参数
from lightgbm import LGBMRegressor
from lightgbm import plot_importance
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets