Datawhale 零基础入门数据挖掘-Task3 特征工程

Datawhale 零基础入门数据挖掘-Task3 特征工程

这段比较难


# 这里我包装了一个异常值处理的代码,可以随便调用。 def outliers_proc(data, col_name, scale=3):
 """
 用于清洗异常值,默认用 box_plot(scale=3)进行清洗
 :param data: 接收 pandas 数据格式
 :param col_name: pandas 列名
 :param scale: 尺度
 :return:
 """
 def box_plot_outliers(data_ser, box_scale):
 """
 利用箱线图去除异常值
 :param data_ser: 接收 pandas.Series 数据格式
 :param box_scale: 箱线图尺度,
 :return:
 """
 iqr = box_scale * (data_ser.quantile(0.75) - data_ser.quantile(0.25))
 val_low = data_ser.quantile(0.25) - iqr
 val_up = data_ser.quantile(0.75) + iqr
 rule_low = (data_ser < val_low)
 rule_up = (data_ser > val_up)
 return (rule_low, rule_up), (val_low, val_up)
 data_n = data.copy()
 data_series = data_n[col_name]
 rule, value = box_plot_outliers(data_series, box_scale=scale)
 index = np.arange(data_series.shape[0])[rule[0] | rule[1]]
 print("Delete number is: {}".format(len(index)))
 data_n = data_n.drop(index)
 data_n.reset_index(drop=True, inplace=True)
 print("Now column number is: {}".format(data_n.shape[0]))
 index_low = np.arange(data_series.shape[0])[rule[0]]
 outliers = data_series.iloc[index_low]
 print("Description of data less than the lower bound is:")
 print(pd.Series(outliers).describe())
 index_up = np.arange(data_series.shape[0])[rule[1]]
 outliers = data_series.iloc[index_up]
 print("Description of data larger than the upper bound is:")
 print(pd.Series(outliers).describe())
 
 fig, ax = plt.subplots(1, 2, figsize=(10, 7))
 sns.boxplot(y=data[col_name], data=data, palette="Set1", ax=ax[0])
 sns.boxplot(y=data_n[col_name], data=data_n, palette="Set1", ax=ax[1])
 return data_n

缺失值处理:
不处理(针对类似 XGBoost 等树模型);
删除(缺失数据太多);
插值补全,包括均值/中位数/众数/建模预测/多重插补/压缩感知补全/矩阵补全等;
分箱,缺失值一个箱;
5. 特征构造:
构造统计量特征,报告计数、求和、比例、标准差等;
时间特征,包括相对时间和绝对时间,节假日,双休日等;
地理信息,包括分箱,分布编码等方法;
非线性变换,包括 log/ 平方/ 根号等;
特征组合,特征交叉;
仁者见仁,智者见智。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值