第二次打卡。
先来讲一下在跑代码过程中遇到的两个问题。
首先是缩进的问题。比如如下代码:
def outliers_proc(data, col_name, scale=3):
def box_plot_outliers(data_ser, box_scale):
iqr = box_scale * (data_ser.quantile(0.75) - data_ser.quantile(0.25))
val_low = data_ser.quantile(0.25) - iqr
一开始,iqr这一行的代码缩进不正确,跑的时候立刻出现了错误提示。
Indentation Error:unindent does not match any outer indentation level
此时,仔细看一下代码处理好缩进问题,
def outliers_proc(data, col_name, scale=3):
def box_plot_outliers(data_ser, box_scale):
iqr = box_scale * (data_ser.quantile(0.75) - data_ser.quantile(0.25))
val_low = data_ser.quantile(0.25) - iqr
就没问题了。
其次的一个问题是出在数据分桶的时候,使用
pd.cancat 的时候,因为实际合并的数据未必列名完全相同,所以会出现如下提示:
E:\anaconda\lib\site-packages\ipykernel_launcher.py:3: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass ‘sort=False’.
To retain the current behavior and silence the warning, pass ‘sort=True’.
This is separate from the ipykernel package so we can avoid doing imports until
此时,按照要求在pd.concat里添加sort=False就可以了。
讲完问题后,特意去学习了一下关于缺失值处理的问题,针对XGBoost树,通常不进行缺失值处理。
XGBoost树是Boosting算法的一种,实际上应当也是一个GBDT,而GBDT使用的决策树是CART回归树,所以我就从CART回归树开始讲起。
CART 回归树是通过不断将特征分裂,用最优的划分特征来构造节点。回归树的预测目标是连续值,并且用MSE作为结点分裂的依据。
再来考虑梯度提升树,利用损失函数的负梯度来提升算法中对残差的评估。
将这两部分组合在一起,就是GBDT了。
XGBoost和GBDT的主要差别在于目标函数不太一样。XGBoost函数还用正则项去更好地定义树的复杂度,相比GBDT在每一次迭代时选用全部数据,XGBoost支持对数据的采样。
下面简要讲一下这次学习。
在导入数据后,我们首先对异常值进行删除处理,得到结果如下。
然后,在特征构造中,我们要使用数据分桶,使内积运算速度更快,并且增强模型的泛化性。
此后,我们需要对数据进行构造,有些数据的构造过于单一,我们可以使用长尾截断进行处理,也可以使用对数据的归一化,使用log变换等。
最后,在进行特征筛选时,我们可以采用corr进行相关性的有效分析。