-
Notifications
You must be signed in to change notification settings - Fork 8
/
kengkeng_jdata_s1.py
1744 lines (1572 loc) · 159 KB
/
kengkeng_jdata_s1.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
#!/usr/bin/env python
# -*-coding:utf-8-*-
'''
'''
import math
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
from scipy.stats import mode
import csv
# import matplotlib.dates
# from datetime import *
import datetime
from sklearn.preprocessing import *
from sklearn import ensemble
import xgboost as xgb
from sklearn import metrics
from xgboost.sklearn import XGBClassifier
from sklearn.model_selection import GridSearchCV,cross_val_score
from sklearn.preprocessing import *
import xgboost as xgb
from sklearn import metrics
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.model_selection import train_test_split, KFold, GridSearchCV, StratifiedKFold
from sklearn.externals import joblib
# import matplotlib.pyplot as plt
# import matplotlib.pylab as pylab
# import seaborn as sns
# from datetime import datetime
# from pylab import *
# mpl.rcParams['font.sans-serif'] = ['SimHei']
#定义时间处理函数
def dealDateColumns(df, columnName):
df[columnName] = pd.to_datetime(df[columnName])
df['year'] = df[columnName].map(lambda x: int(x.year))
df['month'] = df[columnName].map(lambda x: int(x.month))
df['day'] = df[columnName].map(lambda x: int(x.day))
return df
#首先尝试对每个月构造训练集,并为每条样本打上标签
def getTrainDfMonthly(jdata_user_order_data_target, jdata_user_basic_info_data, month, year, start_time, end_time):
user_set = set(jdata_user_order_data_target['user_id'][((jdata_user_order_data_target.o_date < end_time) & (jdata_user_order_data_target.o_date >= start_time))])
jdata_train_df_1 = pd.DataFrame(columns=['user_id', 'age', 'sex', 'user_lv_cd'])
for user_id, age, sex, user_lv_cd in jdata_user_basic_info_data[['user_id', 'age', 'sex', 'user_lv_cd']].values:
if user_id in user_set:
insertRow = pd.DataFrame([[user_id, age, sex, user_lv_cd]], columns=['user_id', 'age', 'sex', 'user_lv_cd'])
jdata_train_df_1 = jdata_train_df_1.append(insertRow)
jdata_train_df_1['month'] = int(month)
jdata_train_df_1['year'] = int(year)
jdata_train_df_1 = pd.merge(jdata_train_df_1, jdata_user_order_data_target[['user_id', 'year', 'month', 'day', 'o_date']], on=['user_id', 'year', 'month'], how='left')
jdata_train_df_1 = jdata_train_df_1.sort_index(by=['user_id', 'o_date'], ascending=True)
jdata_train_df_1 = jdata_train_df_1.drop_duplicates(['user_id'])
jdata_train_df_1['o_date'] = jdata_train_df_1['o_date'].fillna(pd.to_datetime('2000-01-01'))
jdata_train_df_1['is_order'] = jdata_train_df_1['day'].map(lambda x: 0 if math.isnan(x) else 1)
jdata_train_df_1['order_day'] = jdata_train_df_1['day'].map(lambda x: -1 if math.isnan(x) else x)
del jdata_train_df_1['day']
del jdata_train_df_1['o_date']
print(year, month, len(jdata_train_df_1[jdata_train_df_1.is_order == 1]))
# jdata_train_df = pd.concat([jdata_train_df, jdata_train_df_1])
return jdata_train_df_1
# 统计某用户在滑窗区间内下单某个类目商品的次数,逐个月进行处理
def getUserOrderNumberMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_order_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_order_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_order_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_order_future.drop_duplicates(['user_id', 'o_id'], inplace=True)
jdata_user_order_data_order_future_pivot_table = pd.pivot_table(jdata_user_order_data_order_future, index=['user_id'], values=['o_id'], aggfunc=len)
jdata_user_order_data_order_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_order_future_pivot_table.rename(columns={'o_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_order_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserOrderNumber(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserOrderNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_orderNumber')
jdata_df = getUserOrderNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_orderNumber')
jdata_df = getUserOrderNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_orderNumber')
jdata_df = getUserOrderNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_orderNumber')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品的次数,逐个月进行处理
def getUserBuyNumberMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['sku_id'], aggfunc=len)
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'sku_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserBuyNumber(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyNumber')
jdata_df = getUserBuyNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyNumber')
jdata_df = getUserBuyNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyNumber')
jdata_df = getUserBuyNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyNumber')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品价格的最小值,逐个月进行处理
def getUserBuyPriceMinMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc="min")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPriceMin(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPriceMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPriceMin')
jdata_df = getUserBuyPriceMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPriceMin')
jdata_df = getUserBuyPriceMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPriceMin')
jdata_df = getUserBuyPriceMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPriceMin')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品价格的最大值,逐个月进行处理
def getUserBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc="max")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPriceMax(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPriceMax')
jdata_df = getUserBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPriceMax')
jdata_df = getUserBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPriceMax')
jdata_df = getUserBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPriceMax')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品价格的均值,逐个月进行处理
def getUserBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc='mean')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPriceMean(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPriceMean')
jdata_df = getUserBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPriceMean')
jdata_df = getUserBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPriceMean')
jdata_df = getUserBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPriceMean')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品价格的总值,逐个月进行处理
def getUserBuyPriceSumMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc='sum')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPriceSum(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPriceSumMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPriceSum')
jdata_df = getUserBuyPriceSumMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPriceSum')
jdata_df = getUserBuyPriceSumMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPriceSum')
jdata_df = getUserBuyPriceSumMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPriceSum')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品参数一的最小值,逐个月进行处理
def getUserBuyPara1MinMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['para_1'], aggfunc='min')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'para_1':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPara1Min(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPara1MinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPara1Min')
jdata_df = getUserBuyPara1MinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPara1Min')
jdata_df = getUserBuyPara1MinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPara1Min')
jdata_df = getUserBuyPara1MinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPara1Min')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品参数一的最大值,逐个月进行处理
def getUserBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['para_1'], aggfunc='max')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'para_1':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPara1Max(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPara1Max')
jdata_df = getUserBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPara1Max')
jdata_df = getUserBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPara1Max')
jdata_df = getUserBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPara1Max')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品参数一的均值,逐个月进行处理
def getUserBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['para_1'], aggfunc='mean')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'para_1':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyPara1Mean(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyPara1Mean')
jdata_df = getUserBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyPara1Mean')
jdata_df = getUserBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyPara1Mean')
jdata_df = getUserBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyPara1Mean')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品的天数,逐个月进行处理
def getUserBuyDayNumberMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future.drop_duplicates(['user_id', 'o_date'], inplace=True)
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['o_id'], aggfunc=len)
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'o_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserBuyDayNumber(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyDayNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyDayNumber')
jdata_df = getUserBuyDayNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyDayNumber')
jdata_df = getUserBuyDayNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyDayNumber')
jdata_df = getUserBuyDayNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyDayNumber')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品的件数,逐个月进行处理
def getUserBuyCountMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['o_sku_num'], aggfunc='sum')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'o_sku_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserBuyCount(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyCountMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyCount')
jdata_df = getUserBuyCountMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyCount')
jdata_df = getUserBuyCountMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyCount')
jdata_df = getUserBuyCountMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyCount')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品在当月第几天的一些统计特征(min,max,mean),逐个月进行处理
def getUserBuyDayMinMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['day'], aggfunc='min')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'day':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyDayMin(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyDayMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyDayMin')
jdata_df = getUserBuyDayMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyDayMin')
jdata_df = getUserBuyDayMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyDayMin')
jdata_df = getUserBuyDayMinMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyDayMin')
return jdata_df
def getUserBuyDayMaxMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['day'], aggfunc='max')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'day':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyDayMax(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyDayMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyDayMax')
jdata_df = getUserBuyDayMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyDayMax')
jdata_df = getUserBuyDayMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyDayMax')
jdata_df = getUserBuyDayMaxMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyDayMax')
return jdata_df
def getUserBuyDayMeanMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['day'], aggfunc='mean')
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'day':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserBuyDayMean(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyDayMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyDayMean')
jdata_df = getUserBuyDayMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyDayMean')
jdata_df = getUserBuyDayMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyDayMean')
jdata_df = getUserBuyDayMeanMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyDayMean')
return jdata_df
# 统计某用户在滑窗区间内购买某个类目商品的月份数,逐个月进行处理
def getUserBuyMonthNumberMonthly(jdata_df, jdata_user_order_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & ((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30))]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.o_date >= start_time) & (jdata_user_order_data.o_date < end_time) & (jdata_user_order_data.cate == cate)]
jdata_user_order_data_buy_future.drop_duplicates(['user_id', 'month'], inplace=True)
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['month'], aggfunc=len)
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'month':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserBuyMonthNumber(jdata_df, jdata_user_order_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBuyMonthNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_buyMonthNumber')
jdata_df = getUserBuyMonthNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_buyMonthNumber')
jdata_df = getUserBuyMonthNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_buyMonthNumber')
jdata_df = getUserBuyMonthNumberMonthly(jdata_df, jdata_user_order_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_buyMonthNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的操作个数,逐个月进行处理
def getUserSkuActionNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate)]
jdata_user_action_data_future.drop_duplicates(['user_id', 'sku_id'], inplace=True)
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['sku_id'], aggfunc=len)
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'sku_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserSkuActionNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserSkuActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_skuActionNumber')
jdata_df = getUserSkuActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_skuActionNumber')
jdata_df = getUserSkuActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_skuActionNumber')
jdata_df = getUserSkuActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_skuActionNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的操作天数,逐个月进行处理
def getUserSkuActionDayNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate)]
jdata_user_action_data_future.drop_duplicates(['user_id', 'a_date'], inplace=True)
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_date'], aggfunc=len)
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_date':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserSkuActionDayNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserSkuActionDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_skuActionDayNumber')
jdata_df = getUserSkuActionDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_skuActionDayNumber')
jdata_df = getUserSkuActionDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_skuActionDayNumber')
jdata_df = getUserSkuActionDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_skuActionDayNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的操作总数,逐个月进行处理
def getUserActionNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate)]
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_num'], aggfunc='sum')
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserActionNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_actionNumber')
jdata_df = getUserActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_actionNumber')
jdata_df = getUserActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_actionNumber')
jdata_df = getUserActionNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_actionNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的浏览个数,逐个月进行处理
def getUserSkuBrowseNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 1)]
jdata_user_action_data_future.drop_duplicates(['user_id', 'sku_id'], inplace=True)
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['sku_id'], aggfunc=len)
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'sku_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserSkuBrowseNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserSkuBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_skuBrowseNumber')
jdata_df = getUserSkuBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_skuBrowseNumber')
jdata_df = getUserSkuBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_skuBrowseNumber')
jdata_df = getUserSkuBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_skuBrowseNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的浏览天数,逐个月进行处理
def getUserSkuBrowseDayNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 1)]
jdata_user_action_data_future.drop_duplicates(['user_id', 'a_date'], inplace=True)
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_date'], aggfunc=len)
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_date':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserSkuBrowseDayNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserSkuBrowseDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_skuBrowseDayNumber')
jdata_df = getUserSkuBrowseDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_skuBrowseDayNumber')
jdata_df = getUserSkuBrowseDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_skuBrowseDayNumber')
jdata_df = getUserSkuBrowseDayNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_skuBrowseDayNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的浏览总数,逐个月进行处理
def getUserBrowseNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 1)]
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_num'], aggfunc="sum")
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserBrowseNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_browseNumber')
jdata_df = getUserBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_browseNumber')
jdata_df = getUserBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_browseNumber')
jdata_df = getUserBrowseNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_browseNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的关注个数,逐个月进行处理
def getUserSkuFocusNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 2)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 2)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 2)]
jdata_user_action_data_future.drop_duplicates(['user_id', 'sku_id'], inplace=True)
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['sku_id'], aggfunc=len)
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'sku_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserSkuFocusNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserSkuFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_skuFocusNumber')
jdata_df = getUserSkuFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_skuFocusNumber')
jdata_df = getUserSkuFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_skuFocusNumber')
jdata_df = getUserSkuFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_skuFocusNumber')
return jdata_df
# 统计某用户在滑窗区间内对某个类目商品的关注总数,逐个月进行处理
def getUserFocusNumberMonthly(jdata_df, jdata_user_action_data, start_time, end_time, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 2)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 2)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date >= start_time) & (jdata_user_action_data.a_date < end_time) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 2)]
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_num'], aggfunc="sum")
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
# print(len(jdata_train_df_dealMonth))
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserFocusNumber(jdata_df, jdata_user_action_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 101, 'user_cate101_last' + str(month) + 'Month_focusNumber')
jdata_df = getUserFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, 30, 'user_cate30_last' + str(month) + 'Month_focusNumber')
jdata_df = getUserFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -1, 'user_targetCate_last' + str(month) + 'Month_focusNumber')
jdata_df = getUserFocusNumberMonthly(jdata_df, jdata_user_action_data, featureMonthBegin, featureMonthEnd, -2, 'user_relatedCate_last' + str(month) + 'Month_focusNumber')
return jdata_df
# 统计某用户在滑窗区间内的评价次数,逐个月进行处理
def getUserCommentNumberMonthly(jdata_df, jdata_user_comment_score_data, start_time, end_time, score_level, newColName):
jdata_train_df_dealMonth = jdata_df
if score_level == 0:
jdata_user_comment_score_data_future = jdata_user_comment_score_data[(jdata_user_comment_score_data.comment_create_tm >= start_time) & (jdata_user_comment_score_data.comment_create_tm < end_time)]
else:
jdata_user_comment_score_data_future = jdata_user_comment_score_data[(jdata_user_comment_score_data.comment_create_tm >= start_time) & (jdata_user_comment_score_data.comment_create_tm < end_time) & (jdata_user_comment_score_data.score_level == score_level)]
jdata_user_comment_score_data_future_pivot_table = pd.pivot_table(jdata_user_comment_score_data_future, index=['user_id'], values=['score_level'], aggfunc=len)
jdata_user_comment_score_data_future_pivot_table.reset_index(inplace=True)
jdata_user_comment_score_data_future_pivot_table.rename(columns={'score_level':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_comment_score_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
return jdata_train_df_dealMonth
def getUserCommentNumber(jdata_df, jdata_user_comment_score_data, FeatureMonthList):
for featureMonthBegin, featureMonthEnd, month in FeatureMonthList:
jdata_df = getUserCommentNumberMonthly(jdata_df, jdata_user_comment_score_data, featureMonthBegin, featureMonthEnd, 1, 'user_score1_last' + str(month) + 'Month_commentNumber')
jdata_df = getUserCommentNumberMonthly(jdata_df, jdata_user_comment_score_data, featureMonthBegin, featureMonthEnd, 2, 'user_score2_last' + str(month) + 'Month_commentNumber')
jdata_df = getUserCommentNumberMonthly(jdata_df, jdata_user_comment_score_data, featureMonthBegin, featureMonthEnd, 3, 'user_score3_last' + str(month) + 'Month_commentNumber')
jdata_df = getUserCommentNumberMonthly(jdata_df, jdata_user_comment_score_data, featureMonthBegin, featureMonthEnd, 0, 'user_score0_last' + str(month) + 'Month_commentNumber')
return jdata_df
# 统计某用户平均每个月购买某个类目商品的次数,逐个月进行处理
def getUserMonthlyBuyNumberMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary, length):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['sku_id'], aggfunc=len)
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'sku_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyBuyNumber(jdata_df, jdata_user_order_data, time_boundary, length):
jdata_df = getUserMonthlyBuyNumberMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyNumber', time_boundary, length)
jdata_df = getUserMonthlyBuyNumberMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyNumber', time_boundary, length)
jdata_df = getUserMonthlyBuyNumberMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyNumber', time_boundary, length)
jdata_df = getUserMonthlyBuyNumberMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyNumber', time_boundary, length)
return jdata_df
# 统计某用户平均每个月购买某个类目商品的下单次数,逐个月进行处理
def getUserMonthlyOrderNumberMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary, length):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future.drop_duplicates(['user_id', 'o_id'], inplace=True)
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['o_id'], aggfunc=len)
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'o_id':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyOrderNumber(jdata_df, jdata_user_order_data, time_boundary, length):
jdata_df = getUserMonthlyOrderNumberMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyOrderNumber', time_boundary, length)
jdata_df = getUserMonthlyOrderNumberMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyOrderNumber', time_boundary, length)
jdata_df = getUserMonthlyOrderNumberMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyOrderNumber', time_boundary, length)
jdata_df = getUserMonthlyOrderNumberMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyOrderNumber', time_boundary, length)
return jdata_df
# 统计某用户平均每个月购买某个类目商品的件数,逐个月进行处理
def getUserMonthlyBuyCountMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary, length):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['o_sku_num'], aggfunc="sum")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'o_sku_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyBuyCount(jdata_df, jdata_user_order_data, time_boundary, length):
jdata_df = getUserMonthlyBuyCountMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyCount', time_boundary, length)
jdata_df = getUserMonthlyBuyCountMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyCount', time_boundary, length)
jdata_df = getUserMonthlyBuyCountMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyCount', time_boundary, length)
jdata_df = getUserMonthlyBuyCountMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyCount', time_boundary, length)
return jdata_df
# 统计某用户平均每个月购买某个类目商品的天数,逐个月进行处理
def getUserMonthlyBuyDayMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary, length):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future.drop_duplicates(['user_id', 'o_date'], inplace=True)
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['o_date'], aggfunc=len)
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'o_date':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyBuyDay(jdata_df, jdata_user_order_data, time_boundary, length):
jdata_df = getUserMonthlyBuyDayMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyDay', time_boundary, length)
jdata_df = getUserMonthlyBuyDayMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyDay', time_boundary, length)
jdata_df = getUserMonthlyBuyDayMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyDay', time_boundary, length)
jdata_df = getUserMonthlyBuyDayMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyDay', time_boundary, length)
return jdata_df
# 统计某用户历史购买某个类目商品价格的最小值,逐个月进行处理
def getUserMonthlyBuyPriceMinMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc="min")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserMonthlyBuyPriceMin(jdata_df, jdata_user_order_data, time_boundary):
jdata_df = getUserMonthlyBuyPriceMinMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyPriceMin', time_boundary)
jdata_df = getUserMonthlyBuyPriceMinMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyPriceMin', time_boundary)
jdata_df = getUserMonthlyBuyPriceMinMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyPriceMin', time_boundary)
jdata_df = getUserMonthlyBuyPriceMinMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyPriceMin', time_boundary)
return jdata_df
# 统计某用户历史购买某个类目商品价格的最大值,逐个月进行处理
def getUserMonthlyBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc="max")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserMonthlyBuyPriceMax(jdata_df, jdata_user_order_data, time_boundary):
jdata_df = getUserMonthlyBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyPriceMax', time_boundary)
jdata_df = getUserMonthlyBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyPriceMax', time_boundary)
jdata_df = getUserMonthlyBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyPriceMax', time_boundary)
jdata_df = getUserMonthlyBuyPriceMaxMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyPriceMax', time_boundary)
return jdata_df
# 统计某用户历史购买某个类目商品价格的均值,逐个月进行处理
def getUserMonthlyBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['price'], aggfunc="mean")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'price':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserMonthlyBuyPriceMean(jdata_df, jdata_user_order_data, time_boundary):
jdata_df = getUserMonthlyBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyPriceMean', time_boundary)
jdata_df = getUserMonthlyBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyPriceMean', time_boundary)
jdata_df = getUserMonthlyBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyPriceMean', time_boundary)
jdata_df = getUserMonthlyBuyPriceMeanMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyPriceMean', time_boundary)
return jdata_df
# 统计某用户历史购买某个类目商品参数一的最小值,逐个月进行处理
def getUserMonthlyBuyPara1MinMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['para_1'], aggfunc="min")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'para_1':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserMonthlyBuyPara1Min(jdata_df, jdata_user_order_data, time_boundary):
jdata_df = getUserMonthlyBuyPara1MinMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyPara1Min', time_boundary)
jdata_df = getUserMonthlyBuyPara1MinMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyPara1Min', time_boundary)
jdata_df = getUserMonthlyBuyPara1MinMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyPara1Min', time_boundary)
jdata_df = getUserMonthlyBuyPara1MinMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyPara1Min', time_boundary)
return jdata_df
# 统计某用户历史购买某个类目商品参数一的最大值,逐个月进行处理
def getUserMonthlyBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['para_1'], aggfunc="max")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'para_1':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserMonthlyBuyPara1Max(jdata_df, jdata_user_order_data, time_boundary):
jdata_df = getUserMonthlyBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyPara1Max', time_boundary)
jdata_df = getUserMonthlyBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyPara1Max', time_boundary)
jdata_df = getUserMonthlyBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyPara1Max', time_boundary)
jdata_df = getUserMonthlyBuyPara1MaxMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyPara1Max', time_boundary)
return jdata_df
# 统计某用户历史购买某个类目商品参数一的均值,逐个月进行处理
def getUserMonthlyBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, cate, newColName, time_boundary):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate == 101) | (jdata_user_order_data.cate == 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[((jdata_user_order_data.cate != 101) & (jdata_user_order_data.cate != 30)) & (jdata_user_order_data.o_date < time_boundary)]
else:
jdata_user_order_data_buy_future = jdata_user_order_data[(jdata_user_order_data.cate == cate) & (jdata_user_order_data.o_date < time_boundary)]
jdata_user_order_data_buy_future_pivot_table = pd.pivot_table(jdata_user_order_data_buy_future, index=['user_id'], values=['para_1'], aggfunc="mean")
jdata_user_order_data_buy_future_pivot_table.reset_index(inplace=True)
jdata_user_order_data_buy_future_pivot_table.rename(columns={'para_1':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_order_data_buy_future_pivot_table, on=['user_id'], how='left')
return jdata_train_df_dealMonth
def getUserMonthlyBuyPara1Mean(jdata_df, jdata_user_order_data, time_boundary):
jdata_df = getUserMonthlyBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, 101, 'user_101Cate_monthlyBuyPara1Mean', time_boundary)
jdata_df = getUserMonthlyBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, 30, 'user_30Cate_monthlyBuyPara1Mean', time_boundary)
jdata_df = getUserMonthlyBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, -1, 'user_targetCate_monthlyBuyPara1Mean', time_boundary)
jdata_df = getUserMonthlyBuyPara1MeanMonthly(jdata_df, jdata_user_order_data, -2, 'user_relatedCate_monthlyBuyPara1Mean', time_boundary)
return jdata_df
# 统计某用户平均每个月对某个类目商品的关注总数,逐个月进行处理
def getUserMonthlyFocusNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 2)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 2)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 2)]
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_num'], aggfunc="sum")
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyFocusNumber(jdata_df, jdata_user_action_data, time_boundary, length):
jdata_df = getUserMonthlyFocusNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 101, 'user_cate101_monthlyFocusNumber')
jdata_df = getUserMonthlyFocusNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 30, 'user_cate30_monthlyFocusNumber')
jdata_df = getUserMonthlyFocusNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -1, 'user_targetCate_monthlyFocusNumber')
jdata_df = getUserMonthlyFocusNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -2, 'user_relatedCate_monthlyFocusNumber')
return jdata_df
# 统计某用户平均每个月对某个类目商品的浏览总数,逐个月进行处理
def getUserMonthlyBrowseNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30)) & (jdata_user_action_data.a_type == 1)]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & (jdata_user_action_data.cate == cate) & (jdata_user_action_data.a_type == 1)]
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_num'], aggfunc="sum")
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyBrowseNumber(jdata_df, jdata_user_action_data, time_boundary, length):
jdata_df = getUserMonthlyBrowseNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 101, 'user_cate101_monthlyBrowseNumber')
jdata_df = getUserMonthlyBrowseNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 30, 'user_cate30_monthlyBrowseNumber')
jdata_df = getUserMonthlyBrowseNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -1, 'user_targetCate_monthlyBrowseNumber')
jdata_df = getUserMonthlyBrowseNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -2, 'user_relatedCate_monthlyBrowseNumber')
return jdata_df
# 统计某用户平均每个月对某个类目商品的操作总数,逐个月进行处理
def getUserMonthlyActionNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & (jdata_user_action_data.cate == cate)]
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_num'], aggfunc="sum")
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_num':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyActionNumber(jdata_df, jdata_user_action_data, time_boundary, length):
jdata_df = getUserMonthlyActionNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 101, 'user_cate101_monthlyActionNumber')
jdata_df = getUserMonthlyActionNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 30, 'user_cate30_monthlyActionNumber')
jdata_df = getUserMonthlyActionNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -1, 'user_targetCate_monthlyActionNumber')
jdata_df = getUserMonthlyActionNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -2, 'user_relatedCate_monthlyActionNumber')
return jdata_df
# 统计某用户平均每个月对某个类目商品的操作天数,逐个月进行处理
def getUserMonthlyActionDayNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, cate, newColName):
jdata_train_df_dealMonth = jdata_df
if ((cate == -1) | (cate == -2)):
if cate == -1:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate == 101) | (jdata_user_action_data.cate == 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & ((jdata_user_action_data.cate != 101) & (jdata_user_action_data.cate != 30))]
else:
jdata_user_action_data_future = jdata_user_action_data[(jdata_user_action_data.a_date < time_boundary) & (jdata_user_action_data.cate == cate)]
jdata_user_action_data_future.drop_duplicates(['user_id', 'a_date'], inplace=True)
jdata_user_action_data_future_pivot_table = pd.pivot_table(jdata_user_action_data_future, index=['user_id'], values=['a_date'], aggfunc=len)
jdata_user_action_data_future_pivot_table.reset_index(inplace=True)
jdata_user_action_data_future_pivot_table.rename(columns={'a_date':newColName}, inplace=True)
jdata_train_df_dealMonth = pd.merge(jdata_train_df_dealMonth, jdata_user_action_data_future_pivot_table, on=['user_id'], how='left')
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName].fillna(0)
jdata_train_df_dealMonth[newColName] = jdata_train_df_dealMonth[newColName] / length
return jdata_train_df_dealMonth
def getUserMonthlyActionDayNumber(jdata_df, jdata_user_action_data, time_boundary, length):
jdata_df = getUserMonthlyActionDayNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 101, 'user_cate101_monthlyActionDayNumber')
jdata_df = getUserMonthlyActionDayNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, 30, 'user_cate30_monthlyActionDayNumber')
jdata_df = getUserMonthlyActionDayNumberMonthly(jdata_df, jdata_user_action_data, time_boundary, length, -1, 'user_targetCate_monthlyActionDayNumber')