天猫重复购买预测-03特征工程
1 工具导入
1 |
|
2 数据读取
1 |
|
数据资源查看
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 260864 entries, 0 to 260863
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 user_id 260864 non-null int64
1 merchant_id 260864 non-null int64
2 label 260864 non-null int64
dtypes: int64(3)
memory usage: 6.0 MB
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 261477 entries, 0 to 261476
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 user_id 261477 non-null int64
1 merchant_id 261477 non-null int64
2 prob 0 non-null float64
dtypes: float64(1), int64(2)
memory usage: 6.0 MB
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 424170 entries, 0 to 424169
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 user_id 424170 non-null int64
1 age_range 421953 non-null float64
2 gender 417734 non-null float64
dtypes: float64(2), int64(1)
memory usage: 9.7 MB
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 54925330 entries, 0 to 54925329
Data columns (total 7 columns):
# Column Dtype
--- ------ -----
0 user_id int64
1 item_id int64
2 cat_id int64
3 seller_id int64
4 brand_id float64
5 time_stamp int64
6 action_type int64
dtypes: float64(1), int64(6)
memory usage: 2.9 GB
数据资源非常大,甚至达到2.9GB,需要进行数据压缩
3 对数据进行内存压缩
(1)定义内存压缩方法
1 |
|
代码解释
用于优化数据帧(dataframe)的内存使用,主要目的是将数据帧中的整数和浮点数类型转换为更小的数据类型,从而减少内存占用。
以下是代码的详细解释:
定义一个名为
reduce_mem_usage
的函数,接受一个数据帧df
和一个布尔值verbose
作为参数。计算原始数据帧的内存使用情况,并将结果除以1024^2以转换为MB。
定义一个包含整数和浮点数类型的列表
numerics
。遍历数据帧的列(
for col in df.columns:
)。获取当前列的数据类型(
col_type = df[col].dtypes
)。如果当前列的数据类型在
numerics
列表中(if col_type in numerics:
),则进行以下操作:a. 计算当前列的最小值和最大值(
c_min = df[col].min()
和c_max = df[col].max()
)。b. 如果当前列的数据类型是整数类型(
if str(col_type)[:3] == 'int'
),则检查当前列的最小值和最大值是否在整数类型的范围中。如果是,则将当前列转换为更小的整数类型(int8
、int16
、int32
或int64
)。c. 如果当前列的数据类型是浮点数类型,则检查当前列的最小值和最大值是否在浮点数类型的范围中。如果是,则将当前列转换为更小的浮点数类型(
float16
、float32
或64
)。计算优化后的数据帧的内存使用情况,并将结果除以1024^2以转换为MB。
打印优化后的数据帧的内存使用情况,以及与原始数据帧的内存使用情况的百分比差异。
返回优化后的数据帧。
(2)对数据进行内存压缩
1 |
|
1 |
|
Memory usage after optimization is: 1.74 MB
Decreased by 70.8%
Memory usage after optimization is: 3.49 MB
Decreased by 41.7%
Memory usage after optimization is: 3.24 MB
Decreased by 66.7%
Memory usage after optimization is: 32.43 MB
Decreased by 69.6%
(3)查看压缩后的数据信息
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 260864 entries, 0 to 260863
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 user_id 260864 non-null int32
1 merchant_id 260864 non-null int16
2 label 260864 non-null int8
dtypes: int16(1), int32(1), int8(1)
memory usage: 1.7 MB
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 261477 entries, 0 to 261476
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 user_id 261477 non-null int32
1 merchant_id 261477 non-null int16
2 prob 0 non-null float64
dtypes: float64(1), int16(1), int32(1)
memory usage: 3.5 MB
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 424170 entries, 0 to 424169
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 user_id 424170 non-null int32
1 age_range 421953 non-null float16
2 gender 417734 non-null float16
dtypes: float16(2), int32(1)
memory usage: 3.2 MB
1 |
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2000000 entries, 0 to 1999999
Data columns (total 7 columns):
# Column Dtype
--- ------ -----
0 user_id int32
1 item_id int32
2 cat_id int16
3 seller_id int16
4 brand_id float16
5 time_stamp int16
6 action_type int8
dtypes: float16(1), int16(3), int32(2), int8(1)
memory usage: 32.4 MB
4 数据处理
4.1 合并用户信息
1 |
|
159
1 |
|
user_id | merchant_id | label | age_range | gender | |
---|---|---|---|---|---|
0 | 34176 | 3906 | 0.0 | 6.0 | 0.0 |
1 | 34176 | 121 | 0.0 | 6.0 | 0.0 |
2 | 34176 | 4356 | 1.0 | 6.0 | 0.0 |
3 | 34176 | 2217 | 0.0 | 6.0 | 0.0 |
4 | 230784 | 4818 | 0.0 | 0.0 | 0.0 |
代码解释
这段代码的主要目的是合并训练集、测试集和用户信息表,并将它们合并为一个新的数据集all_data。
- 首先,代码删除了测试集中名为’prob’的列,因为它在训练集中不存在。
- 然后,将训练集和测试集合并为一个新的数据集all_data。这里使用了
append()
方法将测试集添加到训练集的末尾。。 - 接着,使用
merge()
方法将用户信息表和合并后的数据集all_data合并,使用’on’参数指定连接的列,使用’how’参数指定连接方式为左连接(即保留左表中的所有行,即使右表中没有匹配的行)。 - 最后,删除了训练集、测试集和用户信息表,使用
del
关键字。同时,调用gc.collect()
函数释放内存。
del
关键字用于删除变量或对象。当在Python中使用del
关键字时,它会从内存中删除变量或对象的引用,从而释放该对象所占用的内存空间。gc.collect()
函数是Python的垃圾回收器(Garbage Collector)的接口,用于手动触发垃圾回收操作。在某些情况下,Python可能会无法及时回收垃圾,这时可以使用gc.collect()
函数手动触发垃圾回收,以释放被占用的内存空间。
4.2 用户行为日志信息按时间进行排序
1 |
|
1 |
|
user_id | item_id | cat_id | seller_id | brand_id | time_stamp | action_type | |
---|---|---|---|---|---|---|---|
61975 | 16 | 980982 | 437 | 650 | 4276.0 | 914 | 0 |
61976 | 16 | 980982 | 437 | 650 | 4276.0 | 914 | 0 |
61977 | 16 | 980982 | 437 | 650 | 4276.0 | 914 | 0 |
61978 | 16 | 962763 | 19 | 650 | 4276.0 | 914 | 0 |
61979 | 16 | 391126 | 437 | 650 | 4276.0 | 914 | 0 |
代码解释
对user_id列和time_stamp列进行排序的具体步骤如下:
- 首先,使用
sort_values()
方法对user_id列进行升序排序。升序排序意味着从小到大排列,即按user_id的顺序排列。 - 然后,使用
sort_values()
方法对time_stamp列进行升序排序。升序排序意味着从小到大排列,即按时间戳的顺序排列。
因此,经过这两步排序后,user_log数据框中的数据将按照用户ID和时间戳的顺序进行排列。
4.3 对每个用户逐个合并所有的字段
合并字段为item_id, cat_id,seller_id,brand_id,time_stamp, action_type
1 |
|
1 |
|
代码解释
这段代码的主要目的是合并数据,将多个列组合成一个字符串,并将结果合并到现有的数据框中。实现原理如下:
使用lambda函数定义一个名为
list_join_func
的函数,该函数接受一个列表参数,并将列表中的元素以空格分隔拼接成一个字符串。定义一个名为
agg_dict
的字典,该字典包含要进行聚合操作的列及其对应的聚合函数。例如,'item_id' : list_join_func
表示要将item_id
列的值拼接成一个字符串。定义一个名为
rename_dict
的字典,该字典包含要重命名的列。例如,'item_id' : 'item_path'
表示要将item_id
列重命名为item_path
。定义一个名为
merge_list
的函数,该函数接受四个参数:df_ID
、join_columns
、df_data
和agg_dict
。函数首先对df_data
按照join_columns
进行分组,并使用agg_dict
进行聚合操作。然后,将结果重命名列,并返回重命名后的数据框。在主程序中,首先对
all_data
和user_log
按照user_id
进行左连接。然后,将合并后的数据帧传递给merge_list
函数,并将结果更新到all_data
。
1 |
|
user_id | item_path | cat_path | seller_path | brand_path | time_stamp_path | action_type_path | |
---|---|---|---|---|---|---|---|
0 | 16 | 980982 980982 980982 962763 391126 827174 6731... | 437 437 437 19 437 437 437 437 895 19 437 437 ... | 650 650 650 650 650 650 650 650 3948 650 650 6... | 4276.0 4276.0 4276.0 4276.0 4276.0 4276.0 4276... | 914 914 914 914 914 914 914 914 914 914 914 91... | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 ... |
1 | 19 | 388018 388018 88673 88673 88673 88673 846066 5... | 949 949 614 614 614 614 420 1401 948 948 513 1... | 2772 2772 4066 4066 4066 4066 4951 4951 2872 2... | 2112.0 2112.0 1552.0 1552.0 1552.0 1552.0 5200... | 710 710 711 711 711 711 908 908 1105 1105 1105... | 0 2 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
2 | 41 | 60215 1004605 60215 60215 60215 60215 628525 5... | 1308 1308 1308 1308 1308 1308 1271 656 656 656... | 2128 3207 2128 2128 2128 2128 3142 4618 4618 4... | 3848.0 3848.0 3848.0 3848.0 3848.0 3848.0 1014... | 521 521 521 521 521 522 529 828 828 828 828 82... | 0 0 0 2 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 ... |
3 | 56 | 889499 528459 765746 553259 889499 22435 40047... | 662 1075 662 1577 662 11 184 1604 11 11 177 11... | 4048 601 3104 3828 4048 4766 2419 2768 2565 26... | 5360.0 1040.0 8240.0 1446.0 5360.0 4360.0 3428... | 517 520 525 528 602 602 610 610 610 610 610 61... | 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 ... |
4 | 155 | 979639 890128 981780 211366 211366 797946 4567... | 267 1271 1505 267 267 1075 1075 407 407 1075 4... | 2429 4785 3784 800 800 1595 1418 2662 2662 315... | 2276.0 1422.0 5692.0 6328.0 6328.0 5800.0 7140... | 529 529 602 604 604 607 607 607 607 607 607 60... | 0 0 0 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 2 ... |
1 |
|
user_id | merchant_id | label | age_range | gender | item_path | cat_path | seller_path | brand_path | time_stamp_path | action_type_path | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 105600 | 1487 | 0.0 | 6.0 | 1.0 | 986160 681407 681407 910680 681407 592698 3693... | 35 1554 1554 119 1554 662 1095 662 35 833 833 ... | 4811 4811 4811 1897 4811 3315 2925 1340 1875 4... | 127.0 127.0 127.0 4704.0 127.0 1605.0 6000.0 1... | 518 518 518 520 520 524 524 524 525 525 525 52... | 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
1 | 110976 | 159 | 0.0 | 5.0 | 0.0 | 396970 961553 627712 926681 1012423 825576 149... | 1023 420 407 1505 962 602 184 1606 351 1505 11... | 1435 1648 223 3178 2418 1614 3004 2511 2285 78... | 5504.0 7780.0 1751.0 7540.0 6652.0 8116.0 5328... | 517 520 522 522 527 530 530 530 601 601 602 60... | 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 0 ... |
2 | 374400 | 302 | 0.0 | 5.0 | 1.0 | 256546 202393 927572 2587 10956 549283 270303 ... | 1188 646 1175 1188 1414 681 1175 681 681 115 1... | 805 390 4252 3979 1228 2029 2029 2029 4252 923... | 1842.0 5920.0 133.0 6304.0 7584.0 133.0 133.0 ... | 517 604 604 604 607 609 609 609 609 615 621 62... | 2 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
3 | 189312 | 1760 | 0.0 | 4.0 | 0.0 | 290583 166235 556025 217894 166235 556025 5589... | 601 601 601 601 601 601 601 601 601 601 601 60... | 3139 3139 3524 3139 3139 3524 3139 3139 3139 3... | 549.0 549.0 549.0 549.0 549.0 549.0 549.0 549.... | 924 924 924 924 924 924 924 924 924 924 924 92... | 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
4 | 189312 | 2511 | 0.0 | 4.0 | 0.0 | 290583 166235 556025 217894 166235 556025 5589... | 601 601 601 601 601 601 601 601 601 601 601 60... | 3139 3139 3524 3139 3139 3524 3139 3139 3139 3... | 549.0 549.0 549.0 549.0 549.0 549.0 549.0 549.... | 924 924 924 924 924 924 924 924 924 924 924 92... | 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
4.4 删除数据并回收内存
1 |
|
42
5 定义数据统计函数
5.1 定义统计函数
(1)定义统计数据总数的函数
1 |
|
(2)定义统计数据唯一值总数的函数
1 |
|
(3)定义统计数据最大值的函数
1 |
|
(4)定义统计数据最小值的函数
1 |
|
(5)定义统计数据标准差的函数
1 |
|
(6)定义统计数据中topN数据的函数
1 |
|
(7)定义统计数据中topN数据总数的函数
1 |
|
5.2 调用定义的统计函数
调用数据集的特征统计函数
1 |
|
6 提取统计特征
6.1 特征统计
(1)店铺特征统计
统计与店铺特点有关的特征,如店铺、商品、品牌等。
1 |
|
1 |
|
user_id | merchant_id | label | age_range | gender | item_path | cat_path | seller_path | brand_path | time_stamp_path | action_type_path | user_cnt | seller_nunique | cat_nunique | brand_nunique | item_nunique | time_stamp_nunique | action_type_nunique | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 105600 | 1487 | 0.0 | 6.0 | 1.0 | 986160 681407 681407 910680 681407 592698 3693... | 35 1554 1554 119 1554 662 1095 662 35 833 833 ... | 4811 4811 4811 1897 4811 3315 2925 1340 1875 4... | 127.0 127.0 127.0 4704.0 127.0 1605.0 6000.0 1... | 518 518 518 520 520 524 524 524 525 525 525 52... | 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... | 310 | 96 | 37 | 88 | 217 | 29 | 2 |
1 | 110976 | 159 | 0.0 | 5.0 | 0.0 | 396970 961553 627712 926681 1012423 825576 149... | 1023 420 407 1505 962 602 184 1606 351 1505 11... | 1435 1648 223 3178 2418 1614 3004 2511 2285 78... | 5504.0 7780.0 1751.0 7540.0 6652.0 8116.0 5328... | 517 520 522 522 527 530 530 530 601 601 602 60... | 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 0 ... | 274 | 181 | 70 | 159 | 233 | 52 | 3 |
2 | 374400 | 302 | 0.0 | 5.0 | 1.0 | 256546 202393 927572 2587 10956 549283 270303 ... | 1188 646 1175 1188 1414 681 1175 681 681 115 1... | 805 390 4252 3979 1228 2029 2029 2029 4252 923... | 1842.0 5920.0 133.0 6304.0 7584.0 133.0 133.0 ... | 517 604 604 604 607 609 609 609 609 615 621 62... | 2 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... | 278 | 57 | 59 | 62 | 148 | 35 | 3 |
3 | 189312 | 1760 | 0.0 | 4.0 | 0.0 | 290583 166235 556025 217894 166235 556025 5589... | 601 601 601 601 601 601 601 601 601 601 601 60... | 3139 3139 3524 3139 3139 3524 3139 3139 3139 3... | 549.0 549.0 549.0 549.0 549.0 549.0 549.0 549.... | 924 924 924 924 924 924 924 924 924 924 924 92... | 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... | 237 | 49 | 35 | 45 | 170 | 9 | 2 |
4 | 189312 | 2511 | 0.0 | 4.0 | 0.0 | 290583 166235 556025 217894 166235 556025 5589... | 601 601 601 601 601 601 601 601 601 601 601 60... | 3139 3139 3524 3139 3139 3524 3139 3139 3139 3... | 549.0 549.0 549.0 549.0 549.0 549.0 549.0 549.... | 924 924 924 924 924 924 924 924 924 924 924 92... | 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... | 237 | 49 | 35 | 45 | 170 | 9 | 2 |
1 |
|
1 |
|
1 |
|
(2)用户特征统计
对用户的点击、加购、购买、收藏等特征进行统计。
1 |
|
代码解释
这段代码用于对用户在购物网站上的点击、加购、购买和收藏行为进行统计。实现原理是使用DataFrame的apply方法对数据进行处理,根据不同的行为类型和业务逻辑对数据进行提取和统计。
功能:
col_cnt_
函数:统计指定列(columns_list)中每个路径的重复次数。col_nuique_
函数:统计指定列(columns_list)中每个路径去重后的次数。user_col_cnt
函数:将统计结果存储在DataFrame的指定列(name)中。user_col_nunique
函数:将统计结果存储在DataFrame的指定列(name)中。
(3)统计用户和店铺的关系
统计店铺被用户点击次数,加购次数,购买次数,收藏次数
1 |
|
1 |
|
user_id | merchant_id | label | age_range | gender | item_path | cat_path | seller_path | brand_path | time_stamp_path | ... | action_type_1 | seller_most_1_cnt | cat_most_1_cnt | brand_most_1_cnt | action_type_1_cnt | user_cnt_0 | user_cnt_1 | user_cnt_2 | user_cnt_3 | seller_nunique_0 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 105600 | 1487 | 0.0 | 6.0 | 1.0 | 986160 681407 681407 910680 681407 592698 3693... | 35 1554 1554 119 1554 662 1095 662 35 833 833 ... | 4811 4811 4811 1897 4811 3315 2925 1340 1875 4... | 127.0 127.0 127.0 4704.0 127.0 1605.0 6000.0 1... | 518 518 518 520 520 524 524 524 525 525 525 52... | ... | 0 | 35 | 43 | 35 | 299 | 310 | 310 | 310 | 310 | 97 |
1 | 110976 | 159 | 0.0 | 5.0 | 0.0 | 396970 961553 627712 926681 1012423 825576 149... | 1023 420 407 1505 962 602 184 1606 351 1505 11... | 1435 1648 223 3178 2418 1614 3004 2511 2285 78... | 5504.0 7780.0 1751.0 7540.0 6652.0 8116.0 5328... | 517 520 522 522 527 530 530 530 601 601 602 60... | ... | 0 | 9 | 56 | 11 | 259 | 274 | 274 | 274 | 274 | 181 |
2 | 374400 | 302 | 0.0 | 5.0 | 1.0 | 256546 202393 927572 2587 10956 549283 270303 ... | 1188 646 1175 1188 1414 681 1175 681 681 115 1... | 805 390 4252 3979 1228 2029 2029 2029 4252 923... | 1842.0 5920.0 133.0 6304.0 7584.0 133.0 133.0 ... | 517 604 604 604 607 609 609 609 609 615 621 62... | ... | 0 | 93 | 29 | 48 | 241 | 278 | 278 | 278 | 278 | 56 |
3 | 189312 | 1760 | 0.0 | 4.0 | 0.0 | 290583 166235 556025 217894 166235 556025 5589... | 601 601 601 601 601 601 601 601 601 601 601 60... | 3139 3139 3524 3139 3139 3524 3139 3139 3139 3... | 549.0 549.0 549.0 549.0 549.0 549.0 549.0 549.... | 924 924 924 924 924 924 924 924 924 924 924 92... | ... | 0 | 45 | 68 | 45 | 228 | 237 | 237 | 237 | 237 | 50 |
4 | 189312 | 2511 | 0.0 | 4.0 | 0.0 | 290583 166235 556025 217894 166235 556025 5589... | 601 601 601 601 601 601 601 601 601 601 601 60... | 3139 3139 3524 3139 3139 3524 3139 3139 3139 3... | 549.0 549.0 549.0 549.0 549.0 549.0 549.0 549.... | 924 924 924 924 924 924 924 924 924 924 924 92... | ... | 0 | 45 | 68 | 45 | 228 | 237 | 237 | 237 | 237 | 50 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
16859 | 120191 | 1899 | NaN | 4.0 | 0.0 | 793882 288225 288225 288225 195714 195714 1957... | 387 35 35 35 1213 1213 1213 1213 1075 447 1213... | 1146 696 696 696 1200 1200 1200 1200 2702 4279... | 8064.0 3600.0 3600.0 3600.0 2276.0 2276.0 2276... | 512 516 516 516 606 606 606 606 606 606 606 60... | ... | 0 | 11 | 14 | 11 | 69 | 96 | 96 | 96 | 96 | 29 |
16860 | 121727 | 4044 | NaN | 4.0 | 0.0 | 544029 562170 544029 562170 544029 544029 5621... | 1505 662 1505 662 1505 1505 662 1505 1505 1505... | 795 1910 795 1910 795 795 1910 795 795 795 411... | 3608.0 950.0 3608.0 950.0 3608.0 3608.0 950.0 ... | 628 628 628 628 628 628 628 628 628 628 710 71... | ... | 0 | 12 | 12 | 12 | 43 | 49 | 49 | 49 | 49 | 15 |
16861 | 385919 | 3912 | NaN | 0.0 | 0.0 | 187936 187936 657875 969054 462255 985073 1602... | 602 602 1389 1505 1228 1604 1228 662 1228 662 ... | 661 661 643 643 4738 643 3740 643 4738 643 643... | 1484.0 1484.0 968.0 968.0 6220.0 968.0 4072.0 ... | 512 512 513 513 524 524 524 524 524 524 526 60... | ... | 0 | 33 | 19 | 33 | 44 | 54 | 54 | 54 | 54 | 12 |
16862 | 215423 | 4356 | NaN | 5.0 | 0.0 | 885364 938282 966141 174392 885364 821661 3473... | 1389 662 1095 1095 1389 662 1577 821 662 1389 ... | 2602 2602 2602 2602 2602 2602 3128 2602 2602 2... | 1900.0 1900.0 1900.0 1900.0 1900.0 1900.0 8392... | 521 521 521 521 521 521 521 521 521 521 521 52... | ... | 0 | 63 | 58 | 63 | 118 | 152 | 152 | 152 | 152 | 25 |
16863 | 215423 | 1840 | NaN | 5.0 | 0.0 | 885364 938282 966141 174392 885364 821661 3473... | 1389 662 1095 1095 1389 662 1577 821 662 1389 ... | 2602 2602 2602 2602 2602 2602 3128 2602 2602 2... | 1900.0 1900.0 1900.0 1900.0 1900.0 1900.0 8392... | 521 521 521 521 521 521 521 521 521 521 521 52... | ... | 0 | 63 | 58 | 63 | 118 | 152 | 152 | 152 | 152 | 25 |
16864 rows × 35 columns
6.2 特征组合
(1)特征组合进行业务特征提取
1 |
|
(2)查看提取的特征
1 |
|
Index(['user_id', 'merchant_id', 'label', 'age_range', 'gender', 'item_path',
'cat_path', 'seller_path', 'brand_path', 'time_stamp_path',
'action_type_path', 'user_cnt', 'seller_nunique', 'cat_nunique',
'brand_nunique', 'item_nunique', 'time_stamp_nunique',
'action_type_nunique', 'time_stamp_max', 'time_stamp_min',
'time_stamp_std', 'time_stamp_range', 'seller_most_1', 'cat_most_1',
'brand_most_1', 'action_type_1', 'seller_most_1_cnt', 'cat_most_1_cnt',
'brand_most_1_cnt', 'action_type_1_cnt', 'user_cnt_0', 'user_cnt_1',
'user_cnt_2', 'user_cnt_3', 'seller_nunique_0'],
dtype='object')
1 |
|
['user_id',
'merchant_id',
'label',
'age_range',
'gender',
'item_path',
'cat_path',
'seller_path',
'brand_path',
'time_stamp_path',
'action_type_path',
'user_cnt',
'seller_nunique',
'cat_nunique',
'brand_nunique',
'item_nunique',
'time_stamp_nunique',
'action_type_nunique',
'time_stamp_max',
'time_stamp_min',
'time_stamp_std',
'time_stamp_range',
'seller_most_1',
'cat_most_1',
'brand_most_1',
'action_type_1',
'seller_most_1_cnt',
'cat_most_1_cnt',
'brand_most_1_cnt',
'action_type_1_cnt',
'user_cnt_0',
'user_cnt_1',
'user_cnt_2',
'user_cnt_3',
'seller_nunique_0']
7 利用Countvector,TF-IDF提取特征
(1)利用Countvector和TF-IDF提取特征,代码如下:
1 |
|
代码解释
这段代码是针对文本数据的向量化处理。代码主要使用了TfidfVectorizer
进行文本特征的提取。以下是对代码逐行的解释:
首先,导入必要的库和方法。
CountVectorizer
和TfidfVectorizer
用于将文本数据转换为向量形式。ENGLISH_STOP_WORDS
提供英文停用词列表,这些词通常在文本处理中被过滤掉,因为它们通常不含有相关信息。sparse
来自scipy
库,用于处理稀疏矩阵。注释掉了一个创建
CountVectorizer
的实例的行,这意味着作者可能原本计划使用它,但最后选择了TfidfVectorizer
。停用词被设为英语停用词,ngram_range
为(1, 1),表示只考虑单个词语(一元模型),max_features=100
表示仅保留词频最高的100个词。TfidfVectorizer
同样创建了一个实例,配置和CountVectorizer
几乎相同,但是TfidfVectorizer
计算的是词的TF-IDF值,这是词频(TF)和逆文档频率(IDF)的乘积,能够更好地表示词的重要性。columns_list
定义了需要进行特征提取的列名称列表,在这段代码中,作者只选择了'seller_path'
一列进行特征提取。通过遍历
columns_list
,对每个列采取如下操作:- 首先将列的数据类型转换为字符串类型。
- 使用
tfidfVec
对象的fit
方法来“学习”这一列的词汇和IDF值。 - 使用
transform
方法将这列的文本转换为TF-IDF的稀疏矩阵形式。
如果这是第一个列(由
i == 0
检查),则将转换后得到的稀疏矩阵赋值给data_cat
。如果不是第一个列,则使用scipy
库中的sparse.hstack
方法将新的稀疏矩阵与前面的矩阵横向拼接。结果是
data_cat
变量现在包含了'seller_path'
列经过TF-IDF转换后的特征矩阵。若columns_list
中有更多列,则data_cat
会依据上述逻辑横向拼接它们的TF-IDF矩阵。
这段代码的目的是生成用于后续机器学习模型训练的特征,这种特征提取方法在文本分类、情感分析等NLP任务中非常常见。
(2)特征重命名 特征合并
1 |
|
8 嵌入(embeeding)特征
1 |
|
embeeding特征和原始特征合并
1 |
|
9 stacking特征
1 |
|
(1) stacking 回归特征
1 |
|
(2) stacking 分类特征
1 |
|
(3) 获取训练和验证数据
1 |
|
处理函数值inf以及nan,为特征工程做准备
1 |
|
1 |
|
(4) 使用lgb和xgb分类模型构造stacking特征
1)使用5折交叉验证
1 |
|
2)选择1gb和xgb分类模型作为基模型。
1 |
|
3)训练模型,获取stacking特征
1 |
|
xgb now score is: [2.627641465068789, 2.5421163955938, 2.4962673833575, 2.393391320956645, 2.488317792597411]
xgb_score_list: [2.627641465068789, 2.5421163955938, 2.4962673833575, 2.393391320956645, 2.488317792597411]
xgb_score_mean: 2.5095468715148295
(5)原始特征和stacking特征合并
1 |
|
1 |
|
1 |
|
10 保存特征数据
1 |
|