feature.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #coding utf-8
  2. import sys
  3. import pandas as pd
  4. from tqdm import tqdm
  5. from collections import defaultdict
  6. types = defaultdict(str)
  7. item_sparse_conf = [
  8. # 基础特征_视频
  9. 'i_id',
  10. 'i_up_id',
  11. # 'i_tag',
  12. # 'i_title',
  13. 'i_title_len',
  14. 'i_play_len',
  15. 'i_days_since_upload',
  16. # 统计特征_视频
  17. 'i_1day_exp_cnt',
  18. 'i_1day_click_cnt',
  19. 'i_1day_share_cnt',
  20. 'i_1day_return_cnt',
  21. 'i_3day_exp_cnt',
  22. 'i_3day_click_cnt',
  23. 'i_3day_share_cnt',
  24. 'i_3day_return_cnt',
  25. 'i_7day_exp_cnt',
  26. 'i_7day_click_cnt',
  27. 'i_7day_share_cnt',
  28. 'i_7day_return_cnt',
  29. 'i_3month_exp_cnt',
  30. 'i_3month_click_cnt',
  31. 'i_3month_share_cnt',
  32. 'i_3month_return_cnt',
  33. ]
  34. item_dense_conf = [
  35. 'i_ctr_1day',
  36. 'i_str_1day',
  37. 'i_rov_1day',
  38. 'i_ros_1day',
  39. 'i_ctr_3day',
  40. 'i_str_3day',
  41. 'i_rov_3day',
  42. 'i_ros_3day',
  43. 'i_ctr_7day',
  44. 'i_str_7day',
  45. 'i_rov_7day',
  46. 'i_ros_7day',
  47. 'i_ctr_3month',
  48. 'i_str_3month',
  49. 'i_rov_3month',
  50. 'i_ros_3month',
  51. ]
  52. def format_x(x):
  53. return str(x).replace(' ', '').replace(':', '_')
  54. def sparse_fea_2_feature(v, k):
  55. f_k = format_x(k)
  56. f_v = format_x(v)
  57. if len(f_v) < 1:
  58. return (None, None)
  59. return ('#'.join([f_k, f_v]), 1.0)
  60. def dense_fea_2_feature(v, k):
  61. f_k = format_x(k)
  62. f_v = format_x(v)
  63. if len(f_v) < 1:
  64. return (None, None)
  65. return (f_k, float(f_v))
  66. def get_features(sparse_conf, dense_conf, row):
  67. features = dict(map(lambda k:sparse_fea_2_feature(row[k], k), sparse_conf))
  68. dense_features = dict(map(lambda k:dense_fea_2_feature(row[k], k), dense_conf))
  69. features.update(dense_features)
  70. if None in features:
  71. del(features[None])
  72. return features
  73. def get_item_features(row):
  74. return get_features(item_sparse_conf, item_dense_conf, row)
  75. label_col = 'ui_is_out'
  76. sparse_fea_cols = [
  77. # 'u_id',
  78. 'u_brand',
  79. 'u_device',
  80. 'u_system',
  81. 'u_system_ver',
  82. # 基础特征_场景
  83. #'ctx_day',
  84. 'ctx_week',
  85. 'ctx_hour',
  86. 'ctx_region',
  87. 'ctx_city',
  88. # 基础特征_交叉
  89. #'ui_is_out',
  90. #'playtime',
  91. #'ui_root_id',
  92. #'ui_share_id',
  93. # 统计特征_用户
  94. 'u_1day_exp_cnt',
  95. 'u_1day_click_cnt',
  96. 'u_1day_share_cnt',
  97. 'u_1day_return_cnt',
  98. 'u_3day_exp_cnt',
  99. 'u_3day_click_cnt',
  100. 'u_3day_share_cnt',
  101. 'u_3day_return_cnt',
  102. 'u_7day_exp_cnt',
  103. 'u_7day_click_cnt',
  104. 'u_7day_share_cnt',
  105. 'u_7day_return_cnt',
  106. 'u_3month_exp_cnt',
  107. 'u_3month_click_cnt',
  108. 'u_3month_share_cnt',
  109. 'u_3month_return_cnt',
  110. ]
  111. dense_fea_cols = [
  112. 'u_ctr_1day',
  113. 'u_str_1day',
  114. 'u_rov_1day',
  115. 'u_ros_1day',
  116. 'u_ctr_3day',
  117. 'u_str_3day',
  118. 'u_rov_3day',
  119. 'u_ros_3day',
  120. 'u_ctr_7day',
  121. 'u_str_7day',
  122. 'u_rov_7day',
  123. 'u_ros_7day',
  124. 'u_ctr_3month',
  125. 'u_str_3month',
  126. 'u_rov_3month',
  127. 'u_ros_3month',
  128. ]