second-hand-2

零基础入门数据挖掘—二手车交易价格预测

Task3 特征工程学习笔记

写在前面:

学习内容网址:https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12281978.0.0.6802593aBnYiSh&postId=95501
 下面的内容是对自己的理解学习过程,内容来自前面提供的网址。看到一句kaggle上流行的话:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已。特征工程的重要性可显而知。

特征工程是什么呢?

 特征工程是将原始数据转换为更能代表预测模型的潜在问题特征的问题,可以通过挑选最相关的特征,提取特征以及创造特征来实现。其中创造特征又经常以降维算法的方式实现。接下来是大佬们列举的常见特征工程。

一、删除异常值

1、异常值处理代码(箱线图)

 下面是大佬们包装的异常值处理的代码,用于清洗异常值,可随便调用的!
  默认用 box_plot(scale=3)进行清洗。
  param data_ser:接收 pandas数据格式
  param col_name:pandas列名
  param scale:尺度

#利用箱线图去除异常值
def outliers_proc(data,col_name,scale=3):
    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="BuGn_r",ax=ax[1])
    return data_n

# 我们可以删掉一些异常数据,以 power 为例。  
# 这里删不删同学可以自行判断
# 但是要注意 test 的数据不能删 = = 不能掩耳盗铃是不是
Train_data = outliers_proc(Train_data, 'power', scale=3)
Delete number is: 963
Now column number is: 149037
Description of data less than the lower bound is:
count    0.0
mean     NaN
std      NaN
min      NaN
25%      NaN
50%      NaN
75%      NaN
max      NaN
Name: power, dtype: float64
Description of data larger than the upper bound is:
count      963.000000
mean       846.836968
std       1929.418081
min        376.000000
25%        400.000000
50%        436.000000
75%        514.000000
max      19312.000000
Name: power, dtype: float64

分位数Quantile

  (1)、概念:
  即统计学中,把所有数值由小到大排列并分成四等份,处于三个分割点位置的得分就是四分位数。
  第一四分位数 (Q1),又称“较小四分位数”,等于该样本中所有数值由小到大排列后第25%的数字。
  第二四分位数 (Q2),又称“中位数”,等于该样本中所有数值由小到大排列后第50%的数字。
  第三四分位数 (Q3),又称“较大四分位数”,等于该样本中所有数值由小到大排列后第75%的数字。
  第三四分位数与第一四分位数的差距又称四分位距(InterQuartile Range,IQR)。
  (2)、函数:
  DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpolation=’linear’)

reset_index函数

2、BOX-COX变换

 Box-Cox变换是Box和Cox在1964年提出的一种广义幂变换方法,是统计建模中常用的一种数据变换,用于连续的响应变量不满足正态分布的情况。Box-Cox变换之后,可以一定程度上减小不可观测的误差和预测变量的相关性。Box-Cox变换的主要特点是引入一个参数,通过数据本身估计该参数进而确定应采取的数据变换形式,Box-Cox变换可以明显地改善数据的正态性、对称性和方差相等性,对许多实际数据都是行之有效的。
参考学习网址:https://blog.csdn.net/lcmssd/article/details/80179102
         https://blog.csdn.net/sinat_26917383/article/details/77864582

四、特征构造

#训练集、测试集放在一起,方便构造特征
Train_data['train']=1
Test_data['train']=0
data=pd.concat([Train_data,Test_data],ignore_index=True)

C:\Users\ASUS\anaconda3\lib\site-packages\ipykernel_launcher.py:4: 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'.

  after removing the cwd from sys.path.

  特征说明:
  regDate:汽车注册时间
  creatDate:广告发布时间
  使用时间:data[‘creatDate’]-data[‘regData’],反应汽车使用时间,一般来说价格与使用时间成反比

pandas.to_datetime

  参考网址:https://blog.csdn.net/DataCastle/article/details/84323603
  主要参数:pandas.to_datetime(arg,errors =‘raise’,utc = None,format = None,unit = None )
  errors:三种取值,‘ignore’, ‘raise’, ‘coerce’,默认为raise。
     ‘raise’,则无效的解析将引发异常
     ‘coerce’,那么无效解析将被设置为NaT
     ‘ignore’,那么无效的解析将返回输入值
  布尔值,默认为none。返回utc即协调世界时。
  format:格式化显示时间的格式。
  unit:默认值为‘ns’,则将会精确到微妙,‘s’为秒。

#使用时间:data['creatDate']-data['regData'],反应汽车使用时间,一般来说价格与使用时间成反比
# 不过要注意,数据里有时间出错的格式,所以我们需要 errors='coerce'
data['used_time']=(pd.to_datetime(data['creatDate'],format='%Y%m%d',errors='coerce')-
                           pd.to_datetime(data['regDate'],format='%Y%m%d',errors='coerce'))

# 看一下空数据,有 15k 个样本的时间是有问题的,我们可以选择删除,也可以选择放着。
# 但是这里不建议删除,因为删除缺失数据占总样本量过大,7.5%
# 我们可以先放着,因为如果我们 XGBoost 之类的决策树,其本身就能处理缺失值,所以可以不用管;
data['used_time'].isnull().sum()

15072
#从邮编中提取城市信息,相当于加入了先验知识
data['city']=data['regionCode'].apply(lambda x : str(x)[:-3])
data=data
#计算某品牌的销售统计量
#以train的数据计算统计量
Train_gb=Train_data.groupby("brand")
all_info={ }
for kind,kind_data in Train_gb:
    info={ }
    kind_data=kind_data[kind_data['price']>0]
    info['brand_amount']=len(kind_data)
    info['brand_price_max']=kind_data.price.max()
    info['brand_price_median']=kind_data.price.median()
    info['brand_price_min']=kind_data.price.min()
    info['brand_price_sum']=kind_data.price.sum()
    info['brand_price_std']=kind_data.price.std()
    info['brand_price_average']=round(kind_data.price.sum()/(len(kind_data)+1),2)
    all_info[kind]=info
brand_fe=pd.DataFrame(all_info).T.reset_index().rename(columns={"index":"brand"})
data=data.merge(brand_fe,how='left',on='brand')

五、数据分桶

数据分桶 以 power 为例
这时候我们的缺失值也进桶了, LightGBM 在改进 XGBoost 时就增加了数据分桶,增强了模型的泛化性

bin=[i*10 for i in range(31)]
data['power_bin']=pd.cut(data['power'],bin,labels=False)
data[['power_bin','power']].head()
power_binpower
05.060
1NaN0
216.0163
319.0193
46.068
#删除不需要的数据
data=data.drop(['creatDate','regDate','regionCode'],axis=1)
print(data.shape)
data.columns
(199037, 39)





Index(['SaleID', 'bodyType', 'brand', 'fuelType', 'gearbox', 'kilometer',
       'model', 'name', 'notRepairedDamage', 'offerType', 'power', 'price',
       'seller', 'train', 'v_0', 'v_1', 'v_10', 'v_11', 'v_12', 'v_13', 'v_14',
       'v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'used_time',
       'city', 'brand_amount', 'brand_price_max', 'brand_price_median',
       'brand_price_min', 'brand_price_sum', 'brand_price_std',
       'brand_price_average', 'power_bin'],
      dtype='object')
data.to_csv('data_for_tree.csv',index=0)

六、归一化

data['power'].plot.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x254a71b76c8>

Train_data['power'].plot.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x254b3fdf988>

#我们对其取log,在做归一化
from sklearn import preprocessing
min_max_scaler=preprocessing.MinMaxScaler()
data['power']=np.log(data['power']+1)
data['power']=((data['power']-np.min(data['power']))/(np.max(data['power'])-np.min(data['power'])))
data['power'].plot.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x254a6cacf08>

#km的比较正常,应该是已经做过分桶了
data['kilometer'].plot.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x254a62cf388>

#所以我们可以直接做归一化
data['kilometer']=((data['kilometer']-np.min(data['kilometer']))/
                      (np.max(data['kilometer'])-np.min(data['kilometer'])))
data['kilometer'].plot.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x254b8d65e48>

#变换
def max_min(x):
    return (x-np.min(x))/(np.max(x)-np.min(x))
data['brand_amount'] = ((data['brand_amount'] - np.min(data['brand_amount'])) / 
                        (np.max(data['brand_amount']) - np.min(data['brand_amount'])))
data['brand_price_average'] = ((data['brand_price_average'] - np.min(data['brand_price_average'])) / 
                               (np.max(data['brand_price_average']) - np.min(data['brand_price_average'])))
data['brand_price_max'] = ((data['brand_price_max'] - np.min(data['brand_price_max'])) / 
                           (np.max(data['brand_price_max']) - np.min(data['brand_price_max'])))
data['brand_price_median'] = ((data['brand_price_median'] - np.min(data['brand_price_median'])) /
                              (np.max(data['brand_price_median']) - np.min(data['brand_price_median'])))
data['brand_price_min'] = ((data['brand_price_min'] - np.min(data['brand_price_min'])) / 
                           (np.max(data['brand_price_min']) - np.min(data['brand_price_min'])))
data['brand_price_std'] = ((data['brand_price_std'] - np.min(data['brand_price_std'])) / 
                           (np.max(data['brand_price_std']) - np.min(data['brand_price_std'])))
data['brand_price_sum'] = ((data['brand_price_sum'] - np.min(data['brand_price_sum'])) / 
                           (np.max(data['brand_price_sum']) - np.min(data['brand_price_sum'])))

OneEncoder

  1. 离散后稀疏向量内积乘法运算速度更快,计算结果也方便存储,容易扩展;
  2. 离散后的特征对异常值更具鲁棒性,如 age>30 为 1 否则为 0,对于年龄为 200 的也不会对模型造成很大的干扰;
  3. LR 属于广义线性模型,表达能力有限,经过离散化后,每个变量有单独的权重,这相当于引入了非线性,能够提升模型的表达能力,加大拟合;
  4. 离散后特征可以进行特征交叉,提升表达能力,由 M+N 个变量编程 M*N 个变量,进一步引入非线形,提升了表达能力;
  5. 特征离散后模型更稳定,如用户年龄区间,不会因为用户年龄长了一岁就变化
#对类别特征进行OneEncoder
data=pd.get_dummies(data,columns=['model','brand','bodyType','fuelType',
                                   'gearbox','notRepairedDamage','power_bin'])
print(data.shape)
data.columns
(199037, 370)





Index(['SaleID', 'kilometer', 'name', 'offerType', 'power', 'price', 'seller',
       'train', 'v_0', 'v_1',
       ...
       'power_bin_20.0', 'power_bin_21.0', 'power_bin_22.0', 'power_bin_23.0',
       'power_bin_24.0', 'power_bin_25.0', 'power_bin_26.0', 'power_bin_27.0',
       'power_bin_28.0', 'power_bin_29.0'],
      dtype='object', length=370)
#这份数据可以给LR用
data.to_csv('data_for_lr.csv',index=0)
#相关性分析
print(data['power'].corr(data['price'],method='spearman'))
print(data['kilometer'].corr(data['price'],method='spearman'))
print(data['brand_amount'].corr(data['price'], method='spearman'))
print(data['brand_price_average'].corr(data['price'], method='spearman'))
print(data['brand_price_max'].corr(data['price'], method='spearman'))
print(data['brand_price_median'].corr(data['price'], method='spearman'))
0.5728285196051496
-0.4082569701616764
0.058156610025581514
0.3834909576057687
0.259066833880992
0.38691042393409447
#直接看图
data_numeric=data[['power','kilometer','brand_amount','brand_price_average',
                  'brand_price_max','brand_price_median']]
correlation=data_numeric.corr()
f,ax=plt.subplots(figsize=(7,7))
plt.title('Correlation of Numeric Features with Price',y=1,size=16)
sns.heatmap(correlation,square = True,  vmax=0.8)
<matplotlib.axes._subplots.AxesSubplot at 0x254a5b27ac8>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值