-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_feature_whole.py
1691 lines (1256 loc) · 77.5 KB
/
extract_feature_whole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
@author: Gene Baratheon
@Email : [email protected]
@Main : 特征工程
"""
import pandas as pd
import numpy as np
import time
from collections import Counter
import gc
#计算特征的最大值/最小值/平均值
def feature_max_min_mean(data,main_id,second_id,common_id):
t11=data[[main_id,second_id]]
t11=t11.groupby(main_id).agg('max').reset_index()
t11.rename(columns={second_id:common_id+'_max'},inplace=True)
t12=data[[main_id,second_id]]
t12=t12.groupby(main_id).agg('min').reset_index()
t12.rename(columns={second_id:common_id+'_min'},inplace=True)
t13=data[[main_id,second_id]]
t13=t13.groupby(main_id).agg('mean').reset_index()
t13.rename(columns={second_id:common_id+'_avg'},inplace=True)
t1=pd.merge(t11,t12,on=main_id,how='left')
t1=pd.merge(t1,t13,on=main_id,how='left')
return t1
#计算特征的最大值
def feature_max(data,main_id,second_id,common_id):
t11=data[[main_id,second_id]]
t11=t11.groupby(main_id).agg('max').reset_index()
t11.rename(columns={second_id:common_id+'_max'},inplace=True)
return t11
#计算特征的最小值
def feature_min(data,main_id,second_id,common_id):
t12=data[[main_id,second_id]]
t12=t12.groupby(main_id).agg('min').reset_index()
t12.rename(columns={second_id:common_id+'_min'},inplace=True)
return t12
#计算特征的平均值
def feature_mean(data,main_id,second_id,common_id):
t13=data[[main_id,second_id]]
t13=t13.groupby(main_id).agg('mean').reset_index()
t13.rename(columns={second_id:common_id+'_avg'},inplace=True)
return t13
#计算特征的种类数
def feature_count_category(data,main_id,second_id,result_id):
t1=data[[main_id,second_id]]
t1[second_id]=t1[second_id].astype('str')
t1=t1.groupby(main_id)[second_id].agg(lambda x:':'.join(x)).reset_index()
t1[result_id]=t1[second_id].apply(lambda s:len(set(s.split(':'))))
del t1[second_id]
return t1
def item(data):
#共22维特征
print('item data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.广告商品被点击的次数 :item_click_nums
t0=data[['item_id']]
t0['item_click_nums']=1
t0=t0.groupby('item_id').agg('sum').reset_index()
data=pd.merge(data,t0,on='item_id',how='left')
''''-------------------------------------------part1----------------------------------------------------------------------'''
#2.一个品牌的点击次数:item_brand_click_nums
t2=data[['item_brand_id']]
t2['item_brand_click_nums']=1
t2=t2.groupby('item_brand_id').agg('sum').reset_index()
#3.一个品牌的商品价格的最大值/最小值/平均值:item_brand_item_price_max/min/avg
main_id='item_brand_id';second_id='item_price_level';common_id='item_brand_item_price'
t5=feature_mean(data,main_id,second_id,common_id)
#4.一个品牌的商品销量的最大值/最小值/平均值:item_brand_item_sales_max/min/avg
main_id='item_brand_id';second_id='item_sales_level';common_id='item_brand_item_sales'
t7=feature_max_min_mean(data,main_id,second_id,common_id)
#5.一个品牌的商品被收藏次数的最大值/最小值/平均值:item_brand_item_collected_max/min/avg
main_id='item_brand_id';second_id='item_collected_level';common_id='item_brand_item_collected'
t8=feature_mean(data,main_id,second_id,common_id)
#6.一个品牌的商品被展示次数的最大值/最小值/平均值:item_brand_item_pv_max/min/avg
main_id='item_brand_id';second_id='item_pv_level';common_id='item_brand_item_pv'
t9=feature_mean(data,main_id,second_id,common_id)
#融合
t_part1=pd.merge(t2,t5,on='item_brand_id',how='left')
t_part1=pd.merge(t_part1,t7,on='item_brand_id',how='left')
t_part1=pd.merge(t_part1,t8,on='item_brand_id',how='left')
t_part1=pd.merge(t_part1,t9,on='item_brand_id',how='left')
data=pd.merge(data,t_part1,on='item_brand_id',how='left')
''''-------------------------------------------part2----------------------------------------------------------------------'''
#1.一个城市的点击次数:item_city_click_nums
t10=data[['item_city_id']]
t10['item_city_click_nums']=1
t10=t10.groupby('item_city_id').agg('sum').reset_index()
#2.一个城市的商品价格的最大值/最小值/平均值:item_city_item_price_max/min/avg
main_id='item_city_id';second_id='item_price_level';common_id='item_city_item_price'
t13=feature_mean(data,main_id,second_id,common_id)
#3.一个城市的商品销量的最大值/最小值/平均值:item_city_item_sales_max/min/avg
main_id='item_city_id';second_id='item_sales_level';common_id='item_city_item_sales'
t15=feature_mean(data,main_id,second_id,common_id)
#4.一个城市的商品被收藏的最大值/最小值/平均值:item_city_item_collected_max/min/avg
main_id='item_city_id';second_id='item_collected_level';common_id='item_city_item_collected'
t16=feature_mean(data,main_id,second_id,common_id)
#5.一个城市的商品被展示次数的最大值/最小值/平均值:item_city_item_pv_max/min/avg
main_id='item_city_id';second_id='item_pv_level';common_id='item_city_item_pv'
t17=feature_min(data,main_id,second_id,common_id)
t_part2=pd.merge(t10,t13,on='item_city_id',how='left')
t_part2=pd.merge(t_part2,t15,on='item_city_id',how='left')
t_part2=pd.merge(t_part2,t16,on='item_city_id',how='left')
t_part2=pd.merge(t_part2,t17,on='item_city_id',how='left')
data=pd.merge(data,t_part2,on='item_city_id',how='left')
''''-------------------------------------------part3----------------------------------------------------------------------'''
print('一个city有多少item_price_level,item_sales_level,item_collected_level,item_pv_level')
itemcnt = data.groupby(['item_city_id'], as_index=False)['instance_id'].agg({'item_city_cnt': 'count'})
data = pd.merge(data, itemcnt, on=['item_city_id'], how='left')
for col in ['item_price_level', 'item_sales_level', 'item_collected_level', 'item_pv_level']:
itemcnt = data.groupby([col, 'item_city_id'], as_index=False)['instance_id'].agg({str(col) + '_city_cnt': 'count'})
data = pd.merge(data, itemcnt, on=[col, 'item_city_id'], how='left')
data[str(col) + '_city_prob'] = data[str(col) + '_city_cnt'] / data['item_city_cnt']
del data[str(col) + '_city_cnt']
del data['item_city_cnt']
print('一个price有多少item_sales_level,item_collected_level,item_pv_level')
itemcnt = data.groupby(['item_price_level'], as_index=False)['instance_id'].agg({'item_price_cnt': 'count'})
data = pd.merge(data, itemcnt, on=['item_price_level'], how='left')
for col in ['item_sales_level', 'item_collected_level', 'item_pv_level']:
itemcnt = data.groupby([col, 'item_city_id'], as_index=False)['instance_id'].agg({str(col) + '_price_cnt': 'count'})
data = pd.merge(data, itemcnt, on=[col, 'item_city_id'], how='left')
data[str(col) + '_price_prob'] = data[str(col) + '_price_cnt'] / data['item_price_cnt']
del data[str(col) + '_price_cnt']
del data['item_price_cnt']
print('一个item_sales_level有多少item_collected_level,item_pv_level')
itemcnt = data.groupby(['item_sales_level'], as_index=False)['instance_id'].agg({'item_salse_cnt': 'count'})
data = pd.merge(data, itemcnt, on=['item_sales_level'], how='left')
for col in ['item_collected_level', 'item_pv_level']:
itemcnt = data.groupby([col, 'item_sales_level'], as_index=False)['instance_id'].agg({str(col) + '_salse_cnt': 'count'})
data = pd.merge(data, itemcnt, on=[col, 'item_sales_level'], how='left')
data[str(col) + '_salse_prob'] = data[str(col) + '_salse_cnt'] / data['item_salse_cnt']
del data['item_salse_cnt']
print('item data shape: ',data.shape) ; gc.collect()
return data
def user(data):
#共14维特征
print('user data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.用户点击商品的次数:user_click_nums
t0=data[['user_id']]
t0['user_click_nums']=1
t0=t0.groupby('user_id').agg('sum').reset_index()
data=pd.merge(data,t0,on='user_id',how='left')
''''-------------------------------------------part1----------------------------------------------------------------------'''
#2.用户各个性别的点击的次数:user_gender_click_nums
t2=data[['user_gender_id']]
t2['user_gender_click_nums']=1
t2=t2.groupby('user_gender_id').agg('sum').reset_index()
data=pd.merge(data,t2,on='user_gender_id',how='left')
''''-------------------------------------------part4----------------------------------------------------------------------'''
print('性别的年龄段,职业有多少')
itemcnt = data.groupby(['user_gender_id'], as_index=False)['instance_id'].agg({'user_gender_cnt': 'count'})
data = pd.merge(data, itemcnt, on=['user_gender_id'], how='left')
for col in ['user_age_level', 'user_occupation_id', 'user_star_level']:
itemcnt = data.groupby([col, 'user_gender_id'], as_index=False)['instance_id'].agg({str(col) + '_user_gender_cnt': 'count'})
data = pd.merge(data, itemcnt, on=[col, 'user_gender_id'], how='left')
data[str(col) + '_user_gender_prob']=data[str(col) + '_user_gender_cnt']/data['user_gender_cnt']
del data['user_gender_cnt']
print('user_age_level对应的user_occupation_id,user_star_level')
itemcnt = data.groupby(['user_age_level'], as_index=False)['instance_id'].agg({'user_age_cnt': 'count'})
data = pd.merge(data, itemcnt, on=['user_age_level'], how='left')
for col in ['user_occupation_id', 'user_star_level']:
itemcnt = data.groupby([col, 'user_age_level'], as_index=False)['instance_id'].agg({str(col) + '_user_age_cnt': 'count'})
data = pd.merge(data, itemcnt, on=[col, 'user_age_level'], how='left')
data[str(col) + '_user_age_prob']=data[str(col) + '_user_age_cnt']/data['user_age_cnt']
del data['user_age_cnt']
print('user_occupation_id对应的user_star_level')
itemcnt = data.groupby(['user_occupation_id'], as_index=False)['instance_id'].agg({'user_occ_cnt': 'count'})
data = pd.merge(data, itemcnt, on=['user_occupation_id'], how='left')
for col in ['user_star_level']:
itemcnt = data.groupby([col, 'user_occupation_id'], as_index=False)['instance_id'].agg({str(col) + '_user_occ_cnt': 'count'})
data = pd.merge(data, itemcnt, on=[col, 'user_occupation_id'], how='left')
data[str(col) + '_user_occ_prob']=data[str(col) + '_user_occ_cnt']/data['user_occ_cnt']
del data['user_occ_cnt']
print('user data shape: ',data.shape) ; gc.collect()
return data
def context(data):
#共6维特征
print('context data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.每个小时段的点击商品的次数之和/最大值/最小值/平均值(h):context_timestamp_hour_click_nums/max/min/avg
t01=data[['hour','day']]
t01['context_timestamp_hour_click_nums']=1
t01=t01.groupby('hour').agg('sum').reset_index()
del t01['day']
ttemp=data[['hour','day']]
ttemp['xxx']=1
ttemp=ttemp.groupby(['hour','day']).agg('sum').reset_index()
t02=ttemp[['hour','xxx']]
t02=t02.groupby('hour').agg('max').reset_index()
t02.rename(columns={'xxx':'context_timestamp_hour_click_nums_max'},inplace=True)
t03=ttemp[['hour','xxx']]
t03=t03.groupby('hour').agg('min').reset_index()
t03.rename(columns={'xxx':'context_timestamp_hour_click_nums_min'},inplace=True)
t04=ttemp[['hour','xxx']]
t04=t04.groupby('hour').agg('mean').reset_index()
t04.rename(columns={'xxx':'context_timestamp_hour_click_nums_avg'},inplace=True)
t0=pd.merge(t01,t02,on='hour',how='left')
t0=pd.merge(t0,t03,on='hour',how='left')
t0=pd.merge(t0,t04,on='hour',how='left')
data=pd.merge(data,t0,on='hour',how='left')
''''-------------------------------------------part1----------------------------------------------------------------------'''
#2.每天早中晚时间段的点击商品的次数之和/最大值/最小值/平均值(h):context_timestamp_interval_click_nums/max/min/avg
t21=data[['time_interval','day']]
t21['context_timestamp_interval_click_nums']=1
t21=t21.groupby('time_interval').agg('sum').reset_index()
del t21['day']
data=pd.merge(data,t21,on='time_interval',how='left')
''''-------------------------------------------part2----------------------------------------------------------------------'''
#3.展示页面的点击商品的次数:context_page_click_nums
t4=data[['context_page_id']]
t4['context_page_click_nums']=1
t4=t4.groupby('context_page_id').agg('sum').reset_index()
data=pd.merge(data,t4,on='context_page_id',how='left')
print('context data shape: ',data.shape) ; gc.collect()
return data
def shop(data):
#共2维特征
print('shop data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.店铺被点击的次数:shop_click_nums
t0=data[['shop_id']]
t0['shop_click_nums']=1
t0=t0.groupby('shop_id').agg('sum').reset_index()
data=pd.merge(data,t0,on='shop_id',how='left')
''''-------------------------------------------part1----------------------------------------------------------------------'''
#2.店铺各个评价等级的服务态度评分的最大值/最小值/平均值:shop_review_score_service_max/min/avg
main_id='shop_review_num_level';second_id='shop_score_service';common_id='shop_review_score_service'
t6=feature_min(data,main_id,second_id,common_id)
data=pd.merge(data,t6,on='shop_review_num_level',how='left')
print('shop data shape: ',data.shape) ; gc.collect()
return data
def item_shop(data):
#共56维特征
print('item_shop data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.店铺总共多少种商品:item_shop_item_nums
t0=data[['shop_id','item_id']]
t0.item_id=t0.item_id.astype('str')
t0=t0.groupby('shop_id')['item_id'].agg(lambda x:':'.join(x)).reset_index()
t0['item_shop_item_nums']=t0.item_id.apply(lambda s:len(set(s.split(':'))))
del t0['item_id']
#2.店铺商品的价格的最大值/最小值/平均值:item_shop_item_price_max/min/avg
main_id='shop_id';second_id='item_price_level';common_id='item_shop_item_price'
t3=feature_max_min_mean(data,main_id,second_id,common_id)
#3.店铺商品的销量的最大值/最小值/平均值:item_shop_item_sales_max/min/avg
main_id='shop_id';second_id='item_sales_level';common_id='item_shop_item_sales'
t4=feature_max_min_mean(data,main_id,second_id,common_id)
#4.店铺商品的被收藏次数的最大值/最小值/平均值:item_shop_item_collected_max/min/avg
main_id='shop_id';second_id='item_collected_level';common_id='item_shop_item_collected'
t5=feature_max_min_mean(data,main_id,second_id,common_id)
#5.店铺商品的被展示次数的最大值/最小值/平均值:item_shop_item_pv_max/min/avg
main_id='shop_id';second_id='item_pv_level';common_id='item_shop_item_pv'
t6=feature_max_min_mean(data,main_id,second_id,common_id)
#融合
t_part0=pd.merge(t0,t3,on='shop_id',how='left')
t_part0=pd.merge(t_part0,t4, on='shop_id',how='left')
t_part0=pd.merge(t_part0,t5, on='shop_id',how='left')
t_part0=pd.merge(t_part0,t6, on='shop_id',how='left')
data=pd.merge(data,t_part0,on='shop_id',how='left')
''''-------------------------------------------add----------------------------------------------------------------------'''
#1.店铺被点击的次数:shop_click_nums
t0=data[['shop_id']]
t0['shop_click_nums']=1
t0=t0.groupby('shop_id').agg('sum').reset_index()
data=pd.merge(data,t0,on='shop_id',how='left')
#2.店铺各个评价等级的点击的次数:shop_review_num_click_nums
t2=data[['shop_review_num_level']]
t2['shop_review_num_click_nums']=1
t2=t2.groupby('shop_review_num_level').agg('sum').reset_index()
data=pd.merge(data,t2,on='shop_review_num_level',how='left')
#3.店铺各个星级的点击次数:shop_star_click_nums
t9=data[['shop_star_level']]
t9['shop_star_click_nums']=1
t9=t9.groupby('shop_star_level').agg('sum').reset_index()
data=pd.merge(data,t9,on='shop_star_level',how='left')
#4.店铺平均每件商品的点击次数:item_shop_click_nums_each_item
data['item_shop_click_nums_each_item']=data['shop_click_nums']/data['item_shop_item_nums']
''''-------------------------------------------part1----------------------------------------------------------------------'''
print('一个shop有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['shop_id',col]]
t1['item_shop_unique_'+str(col)+'_nums']=1
t1=t1.groupby(['shop_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['shop_id',col],how='left')
data[ 'item_shop_unique_'+str(col)+'_rate']=data[ 'item_shop_unique_'+str(col)+'_nums']/data['shop_click_nums']
''''-------------------------------------------part2----------------------------------------------------------------------'''
print('一个shop_review_num_level有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['shop_review_num_level',col]]
t1['item_shop_unique_review_'+str(col)+'_nums']=1
t1=t1.groupby(['shop_review_num_level',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['shop_review_num_level',col],how='left')
data[ 'item_shop_unique_review_'+str(col)+'_rate']=data[ 'item_shop_unique_review_'+str(col)+'_nums']/data['shop_review_num_click_nums']
''''-------------------------------------------part3----------------------------------------------------------------------'''
print('一个shop_star_level有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['shop_star_level',col]]
t1['item_shop_unique_star_'+str(col)+'_nums']=1
t1=t1.groupby(['shop_star_level',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['shop_star_level',col],how='left')
data[ 'item_shop_unique_star_'+str(col)+'_rate']=data[ 'item_shop_unique_star_'+str(col)+'_nums']/data['shop_star_click_nums']
''''-------------------------------------------drop----------------------------------------------------------------------'''
data=data.drop(['shop_click_nums','shop_review_num_click_nums','shop_star_click_nums'],axis=1)
print('item_shop data shape: ',data.shape) ; gc.collect()
return data
def item_user(data):
#共67维特征
print('item_user data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.商品被点击的用户数
main_id='item_id';second_id='user_id';result_id='item_user_click_user_nums'
t0=feature_count_category(data,main_id,second_id,result_id)
#2.商品被点击的用户的男人个数
t3=data[['item_id','user_gender_id']]
t3=t3[t3.user_gender_id==1]
t3['item_user_click_user_man_nums']=1
t3=t3.groupby('item_id').agg('sum').reset_index()
del t3['user_gender_id']
#3.商品被点击的用户的女人个数
t4=data[['item_id','user_gender_id']]
t4=t4[t4.user_gender_id==0]
t4['item_user_click_user_woman_nums']=1
t4=t4.groupby('item_id').agg('sum').reset_index()
del t4['user_gender_id']
#4.商品被点击的用户的家庭个数
t5=data[['item_id','user_gender_id']]
t5=t5[t5.user_gender_id==2]
t5['item_user_click_user_family_nums']=1
t5=t5.groupby('item_id').agg('sum').reset_index()
del t5['user_gender_id']
#5.商品被点击的用户的年龄的最大值/最小值/平均值(-1)
main_id='item_id';second_id='user_age_level';common_id='item_user_click_user_age'
t6=feature_mean(data,main_id,second_id,common_id)
#6.商品被点击的用户的星级的最大值/最小值/平均值(-1)
main_id='item_id';second_id='user_star_level';common_id='item_user_click_user_star'
t7=feature_mean(data,main_id,second_id,common_id)
t_part0=pd.merge(t0,t3,on='item_id',how='left')
t_part0=pd.merge(t_part0,t4, on='item_id',how='left')
t_part0=pd.merge(t_part0,t5, on='item_id',how='left')
t_part0=pd.merge(t_part0,t6, on='item_id',how='left')
t_part0=pd.merge(t_part0,t7, on='item_id',how='left')
data=pd.merge(data,t_part0,on='item_id',how='left')
#7.商品被点击的用户的男人个数/总人数
data['item_user_click_user_man_rate']=data['item_user_click_user_man_nums']/data['item_user_click_user_nums']
#8.商品被点击的用户的女人个数/总人数
data['item_user_click_user_woman_rate']=data['item_user_click_user_woman_nums']/data['item_user_click_user_nums']
#9.商品被点击的用户的家庭个数/总人数
data['item_user_click_user_family_rate']=data['item_user_click_user_family_nums']/data['item_user_click_user_nums']
data=data.drop(['item_user_click_user_woman_nums','item_user_click_user_family_nums'],axis=1)
''''-------------------------------------------add----------------------------------------------------------------------'''
#1.用户点击商品的次数:user_click_nums
t0=data[['user_id']]
t0['user_click_nums']=1
t0=t0.groupby('user_id').agg('sum').reset_index()
data=pd.merge(data,t0,on='user_id',how='left')
#2.用户各个性别的点击的次数:user_gender_click_nums
t2=data[['user_gender_id']]
t2['user_gender_click_nums']=1
t2=t2.groupby('user_gender_id').agg('sum').reset_index()
data=pd.merge(data,t2,on='user_gender_id',how='left')
#3.用户各个年龄段点击商品的次数:user_age_click_nums
t7=data[['user_age_level']]
t7['user_age_click_nums']=1
t7=t7.groupby('user_age_level').agg('sum').reset_index()
data=pd.merge(data,t7,on='user_age_level',how='left')
#4.用户各个职业的点击商品的次数:user_occupation_click_nums
t14=data[['user_occupation_id']]
t14['user_occupation_click_nums']=1
t14=t14.groupby('user_occupation_id').agg('sum').reset_index()
data=pd.merge(data,t14,on='user_occupation_id',how='left')
''''-------------------------------------------part1----------------------------------------------------------------------'''
print('用户对category_1的点击次数和点击率')
#1.用户对category_1的点击次数
t0=data[['user_id','category_1']]
t0['user_click_cate1_nums']=1
t0=t0.groupby(['user_id','category_1']).agg('sum').reset_index()
data=pd.merge(data,t0,on=['user_id','category_1'],how='left')
data['user_click_cate1_nums_rate']=data['user_click_cate1_nums']/data['user_click_nums']
#2.一个商品被同一个用户点击的次数
t9=data[['item_id','user_id']]
t9['item_user_unique_click_nums']=1
t9=t9.groupby(['item_id','user_id']).agg('sum').reset_index()
t10=data[['item_id']]
t10['xxx']=1
t10=t10.groupby('item_id').agg('sum').reset_index()
t_part1=pd.merge(t9,t10,on=['item_id'],how='left')
t_part1['item_user_unique_click_rate']=t_part1['item_user_unique_click_nums']/t_part1['xxx']
del t_part1['xxx']
data=pd.merge(data,t_part1,on=['item_id','user_id'],how='left')
''''-------------------------------------------part2----------------------------------------------------------------------'''
print('一个user_id有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['user_id',col]]
t1['item_user_unique_'+str(col)+'_nums']=1
t1=t1.groupby(['user_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_id',col],how='left')
data[ 'item_user_unique_'+str(col)+'_rate']=data[ 'item_user_unique_'+str(col)+'_nums']/data['user_click_nums']
''''-------------------------------------------part3----------------------------------------------------------------------'''
print('一个user_gender_id有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['user_gender_id',col]]
t1['item_user_unique_gender_'+str(col)+'_nums']=1
t1=t1.groupby(['user_gender_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_gender_id',col],how='left')
data[ 'item_user_unique_gender_'+str(col)+'_rate']=data[ 'item_user_unique_gender_'+str(col)+'_nums']/data['user_gender_click_nums']
''''-------------------------------------------part4----------------------------------------------------------------------'''
print('一个user_age_level有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['user_age_level',col]]
t1['item_user_unique_age_'+str(col)+'_nums']=1
t1=t1.groupby(['user_age_level',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_age_level',col],how='left')
data[ 'item_user_unique_age_'+str(col)+'_rate']=data[ 'item_user_unique_age_'+str(col)+'_nums']/data['user_age_click_nums']
''''-------------------------------------------part5----------------------------------------------------------------------'''
print('一个user_occupation_id有多少item_id,item_brand_id,item_city_id,item_price_level……')
column_list=['item_id','item_brand_id','item_city_id','item_price_level',
'item_sales_level','item_collected_level','item_pv_level']
for col in column_list:
t1=data[['user_occupation_id',col]]
t1['item_user_unique_occupation_'+str(col)+'_nums']=1
t1=t1.groupby(['user_occupation_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_occupation_id',col],how='left')
data[ 'item_user_unique_occupation_'+str(col)+'_rate']=data[ 'item_user_unique_occupation_'+str(col)+'_nums']/data['user_occupation_click_nums']
''''-------------------------------------------drop----------------------------------------------------------------------'''
print('drop some feature')
data=data.drop(['user_click_nums','user_gender_click_nums','user_age_click_nums','user_occupation_click_nums'],axis=1)
print('item_user data shape: ',data.shape) ; gc.collect()
return data
def user_shop(data):
#共49维特征
print('user_shop data feature ..........................................................')
''''-------------------------------------------part0----------------------------------------------------------------------'''
#1.用户点击店铺的评价数量等级的最大值/最小值/平均值
main_id='user_id';second_id='shop_review_num_level';common_id='user_shop_click_shop_review'
t1=feature_max_min_mean(data,main_id,second_id,common_id)
#2.用户点击店铺的好评率的最大值/最小值/平均值
main_id='user_id';second_id='shop_review_positive_rate';common_id='user_shop_click_shop_review_positive'
t2=feature_max_min_mean(data,main_id,second_id,common_id)
#3.用户点击店铺的星级编号的最大值/最小值/平均值
main_id='user_id';second_id='shop_star_level';common_id='user_shop_click_shop_star'
t3=feature_max_min_mean(data,main_id,second_id,common_id)
#4.用户点击店铺的服务态度评分的最大值/最小值/平均值
main_id='user_id';second_id='shop_score_service';common_id='user_shop_click_shop_service'
t4=feature_max_min_mean(data,main_id,second_id,common_id)
#5.用户点击店铺的物流服务评分的最大值/最小值/平均值
main_id='user_id';second_id='shop_score_delivery';common_id='user_shop_click_shop_delivery'
t5=feature_max_min_mean(data,main_id,second_id,common_id)
#6.用户点击店铺的描述相符评分的最大值/最小值/平均值
main_id='user_id';second_id='shop_score_description';common_id='user_shop_click_shop_description'
t6=feature_max_min_mean(data,main_id,second_id,common_id)
t_part0=pd.merge(t1,t2, on='user_id',how='left')
t_part0=pd.merge(t_part0,t3, on='user_id',how='left')
t_part0=pd.merge(t_part0,t4, on='user_id',how='left')
t_part0=pd.merge(t_part0,t5, on='user_id',how='left')
t_part0=pd.merge(t_part0,t6, on='user_id',how='left')
data=pd.merge(data,t_part0,on='user_id',how='left')
''''-------------------------------------------part1----------------------------------------------------------------------'''
#1.点击该店铺的用户的人数
main_id='shop_id';second_id='user_id';result_id='user_shop_click_user_nums'
t7=feature_count_category(data,main_id,second_id,result_id)
#2.点击店铺的用户的女性人数
t8=data[['shop_id','user_gender_id']]
t8=t8[t8.user_gender_id==0]
t8['uesr_shop_click_user_woman_nums']=1
t8=t8.groupby('shop_id').agg('sum').reset_index()
del t8['user_gender_id']
#3.点击店铺的用户的男性人数
t9=data[['shop_id','user_gender_id']]
t9=t9[t9.user_gender_id==1]
t9['uesr_shop_click_user_man_nums']=1
t9=t9.groupby('shop_id').agg('sum').reset_index()
del t9['user_gender_id']
#4.点击店铺的用户的家庭人数
t10=data[['shop_id','user_gender_id']]
t10=t10[t10.user_gender_id==2]
t10['uesr_shop_click_user_family_nums']=1
t10=t10.groupby('shop_id').agg('sum').reset_index()
del t10['user_gender_id']
#5.点击店铺的用户的年龄的最大值/最小值/平均值
main_id='shop_id';second_id='user_age_level';common_id='user_shop_click_user_age'
t11=feature_mean(data,main_id,second_id,common_id)
#6.点击店铺的用户的星级的最大值/最小值/平均值
main_id='shop_id';second_id='user_star_level';common_id='user_shop_click_user_star'
t12=feature_mean(data,main_id,second_id,common_id)
t_part1=pd.merge(t7,t8,on='shop_id',how='left')
t_part1=pd.merge(t_part1,t9, on='shop_id',how='left')
t_part1=pd.merge(t_part1,t10, on='shop_id',how='left')
t_part1=pd.merge(t_part1,t11, on='shop_id',how='left')
t_part1=pd.merge(t_part1,t12, on='shop_id',how='left')
data=pd.merge(data,t_part1,on='shop_id',how='left')
#7.点击店铺的用户的女性人数/总人数
data['user_shop_click_user_woman_rate']=data['uesr_shop_click_user_woman_nums']/data['user_shop_click_user_nums']
#8.点击店铺的用户的男性人数/总人数
data['user_shop_click_user_man_rate']=data['uesr_shop_click_user_man_nums']/data['user_shop_click_user_nums']
#9.点击店铺的用户的家庭人数/总人数
data['user_shop_click_user_family_rate']=data['uesr_shop_click_user_family_nums']/data['user_shop_click_user_nums']
#10.删除特征
data=data.drop(['uesr_shop_click_user_woman_nums','uesr_shop_click_user_man_nums'],axis=1)
''''-------------------------------------------add----------------------------------------------------------------------'''
#1.用户点击商品的次数:user_click_nums
t0=data[['user_id']]
t0['user_click_nums']=1
t0=t0.groupby('user_id').agg('sum').reset_index()
data=pd.merge(data,t0,on='user_id',how='left')
#2.用户各个性别的点击的次数:user_gender_click_nums
t2=data[['user_gender_id']]
t2['user_gender_click_nums']=1
t2=t2.groupby('user_gender_id').agg('sum').reset_index()
data=pd.merge(data,t2,on='user_gender_id',how='left')
#3.用户各个年龄段点击商品的次数:user_age_click_nums
t7=data[['user_age_level']]
t7['user_age_click_nums']=1
t7=t7.groupby('user_age_level').agg('sum').reset_index()
data=pd.merge(data,t7,on='user_age_level',how='left')
#4.用户各个职业的点击商品的次数:user_occupation_click_nums
t14=data[['user_occupation_id']]
t14['user_occupation_click_nums']=1
t14=t14.groupby('user_occupation_id').agg('sum').reset_index()
data=pd.merge(data,t14,on='user_occupation_id',how='left')
''''-------------------------------------------part2----------------------------------------------------------------------'''
#1.该用户点击店铺的次数
column_list=['shop_id','shop_review_num_level','shop_star_level']
for col in column_list:
t1=data[['user_id',col]]
t1['user_shop_unique_'+str(col)+'_nums']=1
t1=t1.groupby(['user_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_id',col],how='left')
data[ 'user_shop_unique_'+str(col)+'_rate']=data[ 'user_shop_unique_'+str(col)+'_nums']/data['user_click_nums']
''''-------------------------------------------part3----------------------------------------------------------------------'''
#2.同一类性别的用户点击同一店铺的次数
column_list=['shop_id','shop_review_num_level','shop_star_level']
for col in column_list:
t1=data[['user_gender_id',col]]
t1['user_shop_unique_gender_'+str(col)+'_nums']=1
t1=t1.groupby(['user_gender_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_gender_id',col],how='left')
data[ 'user_shop_unique_gender_'+str(col)+'_rate']=data[ 'user_shop_unique_gender_'+str(col)+'_nums']/data['user_gender_click_nums']
''''-------------------------------------------part4----------------------------------------------------------------------'''
#3.同一个年龄等级的用户点击同一店铺的次数
column_list=['shop_id','shop_review_num_level','shop_star_level']
for col in column_list:
t1=data[['user_age_level',col]]
t1['user_shop_unique_age_'+str(col)+'_nums']=1
t1=t1.groupby(['user_age_level',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_age_level',col],how='left')
data[ 'user_shop_unique_age_'+str(col)+'_rate']=data[ 'user_shop_unique_age_'+str(col)+'_nums']/data['user_age_click_nums']
''''-------------------------------------------part5----------------------------------------------------------------------'''
#4.同一个职业的用户点击同一店铺的次数
column_list=['shop_id','shop_review_num_level','shop_star_level']
for col in column_list:
t1=data[['user_occupation_id',col]]
t1['user_shop_unique_occupation_'+str(col)+'_nums']=1
t1=t1.groupby(['user_occupation_id',col]).agg('sum').reset_index()
data=pd.merge(data,t1,on=['user_occupation_id',col],how='left')
data[ 'user_shop_unique_occupation_'+str(col)+'_rate']=data[ 'user_shop_unique_occupation_'+str(col)+'_nums']/data['user_occupation_click_nums']
''''-------------------------------------------drop----------------------------------------------------------------------'''
#1.删除一些特征
data=data.drop(['user_click_nums','user_gender_click_nums','user_age_click_nums','user_occupation_click_nums'],axis=1)
print('user_shop data shape: ',data.shape) ; gc.collect()
return data
def get_day_gap_before(s):
this_time,all_time=s.split('/')
all_time=all_time.split('|')
if len(all_time)==1:
return -1
all_time=sorted(all_time)
this_index=all_time.index(this_time)
if this_index==0:
return -1
else:
before_time=all_time[this_index-1]
delta_time=(pd.to_datetime(this_time)-pd.to_datetime(before_time))
return (delta_time.total_seconds()/3600)
def get_day_gap_after(s):
this_time,all_time=s.split('/')
all_time=all_time.split('|')
if len(all_time)==1:
return -1
all_time=sorted(all_time)
this_index=all_time.index(this_time)
if this_index==(len(all_time)-1):
return -1
else:
after_time=all_time[this_index+1]
delta_time=(pd.to_datetime(after_time)-pd.to_datetime(this_time))
return (delta_time.total_seconds()/3600)
def leakage(data):
#共20维特征
print('basic feature ..........................................................')
#1.--------------------------------------------------------------------------------
#同一用户本次点击与前一次/后一次点击的时间间隔(hour)
t0=data[['user_id','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','context_timestamp','hour_gap_before_user','hour_gap_after_user']]
data=pd.merge(data,t1,on=['user_id','context_timestamp'],how='left')
data=data.drop_duplicates()
print('feature 1: ',data.shape)
#2.--------------------------------------------------------------------------------
#同一用户对同一商品本次点击与前一次/后一次点击的时间间隔
t0=data[['user_id','item_id','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','item_id'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','item_id','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','item_id'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_item'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_item'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','item_id','context_timestamp','hour_gap_before_user_item','hour_gap_after_user_item']]
data=pd.merge(data,t1,on=['user_id','item_id','context_timestamp'],how='left')
data=data.drop_duplicates()
print('feature 2: ',data.shape)
#3.--------------------------------------------------------------------------------
#同一用户对同一店铺本次点击与前一次/后一次点击的时间间隔
t0=data[['user_id','shop_id','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','shop_id'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','shop_id','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','shop_id'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_shop'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_shop'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','shop_id','context_timestamp','hour_gap_before_user_shop','hour_gap_after_user_shop']]
data=pd.merge(data,t1,on=['user_id','shop_id','context_timestamp'],how='left')
data=data.drop_duplicates()
print('feature 3:',data.shape)
print('item time feature ..........................................................')
#4.--------------------------------------------------------------------------------
#同一用户对同一商品类目(categpry1)点击与前一次/后一次点击的时间间隔
t0=data[['user_id','category_1','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','category_1'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','category_1','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','category_1'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_cate1'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_cate1'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','category_1','context_timestamp','hour_gap_before_user_cate1','hour_gap_after_user_cate1']]
data=pd.merge(data,t1,on=['user_id','category_1','context_timestamp'],how='left')
data=data.drop_duplicates()
print('feature 4: ',data.shape)
#5.--------------------------------------------------------------------------------
#同一用户对同一商品属性(property_0)点击与前一次/后一次点击的时间间隔
t0=data[['user_id','property_0','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','property_0'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','property_0','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','property_0'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_prop0'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_prop0'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','property_0','context_timestamp','hour_gap_before_user_prop0','hour_gap_after_user_prop0']]
data=pd.merge(data,t1,on=['user_id','property_0','context_timestamp'],how='left')
data=data.drop_duplicates()
#同一用户对同一商品属性(property_1)点击与前一次/后一次点击的时间间隔
t0=data[['user_id','property_1','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','property_1'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','property_1','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','property_1'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_prop1'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_prop1'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','property_1','context_timestamp','hour_gap_before_user_prop1','hour_gap_after_user_prop1']]
data=pd.merge(data,t1,on=['user_id','property_1','context_timestamp'],how='left')
data=data.drop_duplicates()
print('feature 5: ',data.shape)
#6.--------------------------------------------------------------------------------
#同一用户对同一商品品牌点击与前一次/后一次点击的时间间隔
t0=data[['user_id','item_brand_id','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','item_brand_id'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','item_brand_id','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','item_brand_id'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_item_brand'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_item_brand'] = t1.context_timestamp_date.apply(get_day_gap_after)
t1=t1[['user_id','item_brand_id','context_timestamp','hour_gap_before_user_item_brand','hour_gap_after_user_item_brand']]
data=pd.merge(data,t1,on=['user_id','item_brand_id','context_timestamp'],how='left')
data=data.drop_duplicates()
print('feature 6: ',data.shape)
#7.--------------------------------------------------------------------------------
#同一用户对同一商品城市点击与前一次/后一次点击的时间间隔
t0=data[['user_id','item_city_id','context_timestamp']]
t0.context_timestamp=t0.context_timestamp.astype('str')
t0 = t0.groupby(['user_id','item_city_id'])['context_timestamp'].agg(lambda x:'|'.join(x)).reset_index()
t0.rename(columns={'context_timestamp':'timestamp_all'},inplace=True)
t1=data[['user_id','item_city_id','context_timestamp']]
t1 = pd.merge(t1,t0,on=['user_id','item_city_id'],how='left')
t1['context_timestamp_date'] = t1.context_timestamp.astype('str') + '/' + t1.timestamp_all
t1['hour_gap_before_user_item_city'] = t1.context_timestamp_date.apply(get_day_gap_before)
t1['hour_gap_after_user_item_city'] = t1.context_timestamp_date.apply(get_day_gap_after)