feature.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. user_sparse_conf = [
  53. # 统计特征_用户
  54. 'u_1day_exp_cnt',
  55. 'u_1day_click_cnt',
  56. 'u_1day_share_cnt',
  57. 'u_1day_return_cnt',
  58. 'u_3day_exp_cnt',
  59. 'u_3day_click_cnt',
  60. 'u_3day_share_cnt',
  61. 'u_3day_return_cnt',
  62. 'u_7day_exp_cnt',
  63. 'u_7day_click_cnt',
  64. 'u_7day_share_cnt',
  65. 'u_7day_return_cnt',
  66. 'u_3month_exp_cnt',
  67. 'u_3month_click_cnt',
  68. 'u_3month_share_cnt',
  69. 'u_3month_return_cnt',
  70. ]
  71. user_dense_conf = [
  72. 'u_ctr_1day',
  73. 'u_str_1day',
  74. 'u_rov_1day',
  75. 'u_ros_1day',
  76. 'u_ctr_3day',
  77. 'u_str_3day',
  78. 'u_rov_3day',
  79. 'u_ros_3day',
  80. 'u_ctr_7day',
  81. 'u_str_7day',
  82. 'u_rov_7day',
  83. 'u_ros_7day',
  84. 'u_ctr_3month',
  85. 'u_str_3month',
  86. 'u_rov_3month',
  87. 'u_ros_3month',
  88. ]
  89. def format_x(x):
  90. if x is None:
  91. x = ''
  92. return str(x).replace(' ', '').replace(':', '_')
  93. def sparse_fea_2_feature(v, k):
  94. f_k = format_x(k)
  95. f_v = format_x(v)
  96. if len(f_v) < 1:
  97. return (None, None)
  98. return ('#'.join([f_k, f_v]), 1.0)
  99. def dense_fea_2_feature(v, k):
  100. f_k = format_x(k)
  101. f_v = format_x(v)
  102. if len(f_v) < 1:
  103. return (None, None)
  104. return (f_k, float(f_v))
  105. def get_features(sparse_conf, dense_conf, row):
  106. features = dict(map(lambda k:sparse_fea_2_feature(row[k], k), sparse_conf))
  107. dense_features = dict(map(lambda k:dense_fea_2_feature(row[k], k), dense_conf))
  108. features.update(dense_features)
  109. if None in features:
  110. del(features[None])
  111. return features
  112. def get_item_features(row):
  113. return get_features(item_sparse_conf, item_dense_conf, row)
  114. def get_user_features(row):
  115. return get_features(user_sparse_conf, user_dense_conf, row)
  116. label_col = 'ui_is_out'
  117. sparse_fea_cols = [
  118. # 'u_id',
  119. 'u_brand',
  120. 'u_device',
  121. 'u_system',
  122. 'u_system_ver',
  123. # 基础特征_场景
  124. #'ctx_day',
  125. 'ctx_week',
  126. 'ctx_hour',
  127. 'ctx_region',
  128. 'ctx_city',
  129. # 基础特征_交叉
  130. #'ui_is_out',
  131. #'playtime',
  132. #'ui_root_id',
  133. #'ui_share_id',
  134. ]
  135. dense_fea_cols = [
  136. ]