【Kaggle纽约出租车车程用时预测实战(4)】One-hot encode处理属性标签数据

特征数据分类

分类变量(定量特征)与连续变量(定性特征)。我们训练模型的变量,一般分为两种形式。比如之前进行的房价预测,单以最后的标签(因变量)来看,房价可以在一定范围内取得任意数值,则此时变量为连续变量。如果把房价进行分段处理,之前是按照大于中位数就是高房价,低于中位数就是低房价,那么此时变量为分类变量

特征转换。对于分类变量,要进行转换,通常直接转换为数字。比如将高房价和低房价表示为True(1),False(0)。原因主要有两点:

1) 转换后可以提高模型运算效率
2) 对于一些模型,比如逻辑回归或计算距离时,无法对分类值直接进行计算

因此,需要更好的编码方式对特征进行转换

one hot编码

one hot编码(独热编码)是将类别变量转换为机器学习算法易于利用的一种形式的过程。one-hot编码的定义是用N位状态寄存器来对N个状态进行编码。比如上面的例子高房价和低房价,有2个分类值,因此N为2,对应的one-hot编码可以表示为10和01

1. one hot编码实现
ls = ['上海','北京','深圳']
pd.get_dummies(ls)

–> 输出的结果为:
在这里插入图片描述

2. 以出租车公司举例

这里有两家出租车公司,分别为1和2,为了避免数字之间的大小对模拟预测的影响,可以将数据转化为one hot编码

train[['vendor_id','store_and_fwd_flag']].head()

–> 输出的结果为:(第一个字段表示出租车公司的类别,第二个字段代表的是有没有打计价器)
在这里插入图片描述
将数据转化为独热编码,为了保证一致性,要确保测试集和训练集上都进行转变

vendor_train = pd.get_dummies(train['vendor_id'], prefix='vi', prefix_sep='_')
vendor_test = pd.get_dummies(test['vendor_id'], prefix='vi', prefix_sep='_')

vendor_train.head()

–> 输出的结果为:(比如这里查看一下测试集上面该字段转化后的内容,可以看出结果是和上面的数据对应上的)
在这里插入图片描述

3. 将所有的属性标签进行独热编码转化
store_and_fwd_flag_train = pd.get_dummies(train['store_and_fwd_flag'], prefix='sf', prefix_sep='_')
store_and_fwd_flag_test = pd.get_dummies(test['store_and_fwd_flag'], prefix='sf', prefix_sep='_')
#是否打表计时也属于分类数据
cluster_pickup_train = pd.get_dummies(train['pickup_cluster'], prefix='p', prefix_sep='_')
cluster_pickup_test = pd.get_dummies(test['pickup_cluster'], prefix='p', prefix_sep='_')
cluster_dropoff_train = pd.get_dummies(train['dropoff_cluster'], prefix='d', prefix_sep='_')
cluster_dropoff_test = pd.get_dummies(test['dropoff_cluster'], prefix='d', prefix_sep='_')

train['dayofweek'] = train['pickup_datetime'].dt.dayofweek
#这里将上车的时间转化为星期几的形式,然后再转化为独热编码
#默认出租车是在一天内行驶的,所以只处理上车的数据即可
test['dayofweek'] = test['pickup_datetime'].dt.dayofweek

month_train = pd.get_dummies(train['month'], prefix='m', prefix_sep='_')
month_test = pd.get_dummies(test['month'], prefix='m', prefix_sep='_')
dom_train = pd.get_dummies(train['day'], prefix='dom', prefix_sep='_')
dom_test = pd.get_dummies(test['day'], prefix='dom', prefix_sep='_')
hour_train = pd.get_dummies(train['hr'], prefix='h', prefix_sep='_')
hour_test = pd.get_dummies(test['hr'], prefix='h', prefix_sep='_')
hour_cate_train = pd.get_dummies(train['hr_categori'], prefix='hc', prefix_sep='_')
#这个也是分类数据
hour_cate_test = pd.get_dummies(test['hr_categori'], prefix='hc', prefix_sep='_')
dow_train = pd.get_dummies(train['dayofweek'], prefix='dow', prefix_sep='_')
#将确认为星期几也是属于分类的数据,和月天小时是同理的
dow_test = pd.get_dummies(test['dayofweek'], prefix='dow', prefix_sep='_')

month_train.head()

–> 输出的结果为:
在这里插入图片描述

4. 删除干扰特征

因为已经将属性标签(分类数据)进行独热编码处理了,那么原来的字段数据就可以进行删除了(这些数据有的会有大小之间的关系,造成不希望的结果),避免对模型预测产生影响,最后的一个因为是使用了比赛要求的形式评估结果,所以这个字段的数据也应该删除

train = train.drop(['id','vendor_id','store_and_fwd_flag','dayofweek','hr_categori','minute',
                    'month','day','hr','pickup_cluster','dropoff_cluster','dropoff_datetime', 'trip_duration'],axis = 1)
                    
#Test_id = test['id']
test = test.drop(['id','vendor_id','store_and_fwd_flag','dayofweek','hr_categori','minute',
                  'month','day','hr','pickup_cluster','dropoff_cluster'], axis = 1)
原创文章 159 获赞 93 访问量 4万+

猜你喜欢

转载自blog.csdn.net/lys_828/article/details/105028223