在python中实现几种boosting算法
KTBoost的Python项目详细描述
ktboost-用于boosting的python包
这个python包使用基本学习者、优化算法和丢失函数的不同组合实现了几个boosting算法。
说明
关于基础学习者,ktboost包括:
- 树
- 再生核hilbert空间(rkhs)岭回归函数(即高斯过程的后验均值)
- 两者的结合(ktboost算法)
关于查找boosting更新的优化步骤,包支持:
- 梯度下降
- 牛顿法(如适用)
- 作为基础学习者的两种树的混合版本
该软件包实现以下"丢失"功能:
- 连续数据("回归"):二次损失(l2损失)、绝对误差(l1损失)、huber损失、分位数回归损失、gamma回归损失、负gaussian对数似然,均值和标准差均为特征函数
- 计数数据("回归"):泊松回归损失
- (无序)分类数据("分类"):logistic回归损失(对数损失)、指数损失、与softmax的交叉熵损失
- 混合连续分类数据("删失回归"):负tobit似然(grabit模型)
安装
使用
pip install -U KTBoost
然后使用
importKTBoost.KTBoostasKTBoost
作者
法比奥·西格里斯特
参考文献
- 弗里德曼,J.H.(2001)。贪心函数逼近:梯度提升机。《统计年鉴》,1189-1232年。
- Sigrist,F.,&Hirnshall,C.(2017年)。Grabit:Gradient Tree增加了用于默认预测的Tobit模型。arxiv预印本arxiv:1711.08695。
- Sigrist,F.(2018年)。用于分类和回归的梯度和牛顿boosting。arxiv预印本arxiv:1808.03064。
- sigrist,f.(2019年)。ktboost:内核和树的组合boosting。arxiv预印本arxiv:1902.03999。
用法和示例
该软件包是作为boosting算法的scikit-learn实现的扩展而构建的,其工作流程与scikit-learn非常相似。
这两个主要类是ktboost.boostingClassifier
和ktboost.boostingRegressor
。下面的代码示例演示如何使用包。有关主要参数的更多信息,请参见下面的内容。
定义模型、训练模型、进行预测
importKTBoost.KTBoostasKTBoost################################################## Define model (see below for more examples) #################################################### Standard tree-boosting for regression with quadratic loss and hybrid gradient-Newton updates as in Friedman (2001)model=KTBoost.BoostingRegressor(loss='ls')#################### Train models ####################model.fit(Xtrain,ytrain)######################## Make predictions ########################model.predict(Xpred)
更多型号示例
############################### More examples of models ################################# Boosted Tobit model, i.e. Grabit model (Sigrist and Hirnschall, 2017), ## with lower and upper limits at 0 and 100model=KTBoost.BoostingRegressor(loss='tobit',yl=0,yu=100)## KTBoost algorithm (combined kernel and tree boosting) for classification with Newton updatesmodel=KTBoost.BoostingClassifier(loss='deviance',base_learner='combined',update_step='newton',theta=1)## Gradient boosting for classification with trees as base learnersmodel=KTBoost.BoostingClassifier(loss='deviance',update_step='gradient')## Newton boosting for classification model with trees as base learnersmodel=KTBoost.BoostingClassifier(loss='deviance',update_step='newton')## Hybrid gradient-Newton boosting (Friedman, 2001) for classification with ## trees as base learners (this is the version that scikit-learn implements)model=KTBoost.BoostingClassifier(loss='deviance',update_step='hybrid')## Kernel boosting for regression with quadratic lossmodel=KTBoost.BoostingRegressor(loss='ls',base_learner='kernel',theta=1)## Kernel boosting with the Nystroem method and the range parameter theta chosen ## as the average distance to the 100-nearest neighbors (of the Nystroem samples)model=KTBoost.BoostingRegressor(loss='ls',base_learner='kernel',nystroem=True,n_components=1000,theta=None,n_neighbors=100)## Regression model where both the mean and the standard deviation depend ## on the covariates / featuresmodel=KTBoost.BoostingRegressor(loss='msr')
特征重要性和部分相关图
########################### Feature importances ## (only defined for trees as base learners)#########################Xtrain=np.random.rand(1000,10)ytrain=2*Xtrain[:,0]+2*Xtrain[:,1]+np.random.rand(1000)model=KTBoost.BoostingRegressor()model.fit(Xtrain,ytrain)## Extract feature importances calculated as described in Friedman (2001)feat_imp=model.feature_importances_## Alternatively, plot feature importances directlyKTBoost.plot_feature_importances(model=model,feature_names=feature_names,maxFeat=10)################################ Partial dependence plots ## (currently only implemented for trees as base learners)##############################fromKTBoost.partial_dependenceimportplot_partial_dependenceimportmatplotlib.pyplotaspltfeatures=[0,1,2,3,4,5]fig,axs=plot_partial_dependence(model,Xtrain,features,percentiles=(0,1),figsize=(8,6))plt.subplots_adjust(top=0.9)fig.suptitle('Partial dependence plots')## Alternatively, get partial dependencies in numerical formfromKTBoost.partial_dependenceimportpartial_dependencekwargs=dict(X=Xtrain,percentiles=(0,1))partial_dependence(model,[0],**kwargs)
主要参数汇总
在下面,我们将描述这两个类的构造函数的最重要参数ktboost.boostingClassifier
和ktboost.boostingRegressor
损失:要优化的损失函数。
ktboost.boostingClassifier
{'deviance','exponential'},可选(默认为'deviance')"偏差"是指二进制分类的logistic回归损失,以及多类分类的带softmax函数的交叉熵损失。
ktboost.boostingressor
{'ls'、'lad'、'huber'、'quantile'、'poisson'、'gamma'、'tobit'、'msr'},可选(默认值为'ls')"ls"指的是方波损失。lad(最小绝对偏差)是稳健的 版本。休伯是前两者的结合体。分位数 允许分位数回归(使用"alpha"指定分位数)。 "tobit"对应于带有tobit lo的grabit模型党卫军。msr'是一个线性回归模型,其中标准差的均值和对数都是变化的。
更新步骤:string,default="hybrid"
定义如何计算增强更新。使用"渐变"进行渐变增强 或"牛顿"表示牛顿增压(如适用)。"混合"使用梯度步长来查找树的结构,使用牛顿步长来查找叶的值。对于内核提升,"混合"使用 梯度下降。有关详细信息,请参阅参考文件。
基础学习者:string,default="tree"
用于促进更新的基础学习者。在"树"中选择树,"内核"中选择树 再生核hilbert空间(rkhs)回归函数,并"组合"为二者的组合。有关详细信息,请参阅参考文件。
学习率:浮动,可选(默认值=0.1)
学习率通过"学习率"缩小了每个基础学习者的贡献。 学习率和n_估计量之间存在权衡。
n_估计量:int(默认值=100)
要执行的提升迭代次数。
最大深度:整数,可选(默认值=5)
回归树的最大深度。最大值 深度限制树中的节点数。这个值决定了交互作用 预测变量的。
最小采样叶:int,浮点,可选(默认值=1)
叶节点所需的最小样本数:
- 如果是int,则将
min_samples_leaf
作为最小值。 - 如果浮动,则
min_samples_leaf
是一个百分比,并且ceil(最小采样数叶*n采样数)
是最小值 每个节点的样本数。
- 如果是int,则将
最小叶重量:浮动,可选(默认值为1.)
叶节点所需的最小加权样本数。 如果使用牛顿增压,则相当于 归一化)确定权重的加权样本数 基于二阶导数/Hessians。
条件:字符串,可选(默认值为mse)
测量分割质量的函数。支持的条件 "friedman_mse"表示改进后的均方误差 弗里德曼评分,均方误差为"mse",均方误差为"mae" 平均绝对误差。
随机状态:int,随机状态实例或无,可选(默认为无)
如果是int,则random_state是随机数生成器使用的种子; 如果是随机状态实例,则随机状态是随机数生成器; 如果没有,则随机数生成器是使用的随机状态实例 通过
np.random
内核:字符串,default="rbf"
用于内核提升的内核函数。目前,支持"laplace"、"rbf"和"gw" (推广了"平滑参数"mu=1的wendland)。
θ:浮点,默认值:1。
决定核函数速度的核函数的范围参数 随距离衰减。
n"邻居":int,默认值:none
如果没有给出范围参数"theta",则可以使用 参数。参数"theta"被选为"n个邻居"的平均距离 近邻距离。参数"range_adjust"可用于修改此项。 如果range_adjust=3或range_adjust=4.6,则选择"theta",使内核函数具有 在 'n_neighbors'最近的邻居(用于rbf和拉普拉斯核)。
AlphaReg:浮点,默认值:1。
核岭回归boosting更新的正则化参数。
nystroem:布尔值,默认值=无
指示是否将nystrom采样用于内核提升。
n_组件:int,detault=100
Nystroem采样中用于内核提升的数据点数。