get_ad_out_sample_v2_item.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #coding utf-8
  2. from tqdm import tqdm
  3. import sys
  4. import json
  5. import traceback
  6. from threading import Timer
  7. from tqdm import tqdm
  8. from utils import RedisHelper, data_check, get_feature_data, send_msg_to_feishu
  9. from config import set_config
  10. from log import Log
  11. config_, _ = set_config()
  12. log_ = Log()
  13. redis_helper = RedisHelper()
  14. from feature import get_item_features
  15. from lr_model import LrModel
  16. from utils import exe_sql
  17. if __name__ == "__main__":
  18. project = 'loghubods'
  19. datetime = sys.argv[1]
  20. sql = """
  21. --odps sql
  22. --********************************************************************--
  23. --author:研发
  24. --create time:2023-12-01 15:48:17
  25. --********************************************************************--
  26. with candidate as (
  27. select
  28. -- 基础特征_用户
  29. mid AS u_id
  30. ,machineinfo_brand AS u_brand
  31. ,machineinfo_model AS u_device
  32. ,SPLIT(machineinfo_system,' ')[0] AS u_system
  33. ,machineinfo_system AS u_system_ver
  34. -- 基础特征_视频
  35. ,videoid AS i_id
  36. ,i_up_id AS i_up_id
  37. ,tags as i_tag
  38. ,title as i_title
  39. ,ceil(log2(i_title_len + 1)) as i_title_len
  40. ,ceil(log2(total_time + 1)) as i_play_len
  41. ,ceil(log2(i_days_since_upload + 1)) as i_days_since_upload -- 发布时间(距离现在天数)
  42. -- 基础特征_场景
  43. ,ctx_day AS ctx_day
  44. ,ctx_week AS ctx_week
  45. ,ctx_hour AS ctx_hour
  46. ,ctx_region as ctx_region
  47. ,ctx_city as ctx_city
  48. -- 基础特征_交叉
  49. ,ui_is_out as ui_is_out
  50. ,i_play_len as playtime
  51. -- ,IF(i_play_len > 1,'0','1') AS ui_is_out_new
  52. ,rootmid AS ui_root_id
  53. ,shareid AS ui_share_id
  54. -- 统计特征_用户
  55. ,ceil(log2(u_1day_exp_cnt + 1)) as u_1day_exp_cnt
  56. ,ceil(log2(u_1day_click_cnt + 1)) as u_1day_click_cnt
  57. ,ceil(log2(u_1day_share_cnt + 1)) as u_1day_share_cnt
  58. ,ceil(log2(u_1day_return_cnt + 1)) as u_1day_return_cnt
  59. ,ceil(log2(u_3day_exp_cnt + 1)) as u_3day_exp_cnt
  60. ,ceil(log2(u_3day_click_cnt + 1)) as u_3day_click_cnt
  61. ,ceil(log2(u_3day_share_cnt + 1)) as u_3day_share_cnt
  62. ,ceil(log2(u_3day_return_cnt + 1)) as u_3day_return_cnt
  63. ,ceil(log2(u_7day_exp_cnt + 1)) as u_7day_exp_cnt
  64. ,ceil(log2(u_7day_click_cnt + 1)) as u_7day_click_cnt
  65. ,ceil(log2(u_7day_share_cnt + 1)) as u_7day_share_cnt
  66. ,ceil(log2(u_7day_return_cnt + 1)) as u_7day_return_cnt
  67. ,ceil(log2(u_3month_exp_cnt + 1)) as u_3month_exp_cnt
  68. ,ceil(log2(u_3month_click_cnt + 1)) as u_3month_click_cnt
  69. ,ceil(log2(u_3month_share_cnt + 1)) as u_3month_share_cnt
  70. ,ceil(log2(u_3month_return_cnt + 1)) as u_3month_return_cnt
  71. ,round(if(u_ctr_1day > 10.0, 10.0, u_ctr_1day) / 10.0, 6) as u_ctr_1day
  72. ,round(if(u_str_1day > 10.0, 10.0, u_str_1day) / 10.0, 6) as u_str_1day
  73. ,round(if(u_rov_1day > 10.0, 10.0, u_rov_1day) / 10.0, 6) as u_rov_1day
  74. ,round(if(u_ros_1day > 10.0, 10.0, u_ros_1day) / 10.0, 6) as u_ros_1day
  75. ,round(if(u_ctr_3day > 10.0, 10.0, u_ctr_3day) / 10.0, 6) as u_ctr_3day
  76. ,round(if(u_str_3day > 10.0, 10.0, u_str_3day) / 10.0, 6) as u_str_3day
  77. ,round(if(u_rov_3day > 10.0, 10.0, u_rov_3day) / 10.0, 6) as u_rov_3day
  78. ,round(if(u_ros_3day > 10.0, 10.0, u_ros_3day) / 10.0, 6) as u_ros_3day
  79. ,round(if(u_ctr_7day > 10.0, 10.0, u_ctr_7day) / 10.0, 6) as u_ctr_7day
  80. ,round(if(u_str_7day > 10.0, 10.0, u_str_7day) / 10.0, 6) as u_str_7day
  81. ,round(if(u_rov_7day > 10.0, 10.0, u_rov_7day) / 10.0, 6) as u_rov_7day
  82. ,round(if(u_ros_7day > 10.0, 10.0, u_ros_7day) / 10.0, 6) as u_ros_7day
  83. ,round(if(u_ctr_3month > 10.0, 10.0, u_ctr_3month) / 10.0, 6) as u_ctr_3month
  84. ,round(if(u_str_3month > 10.0, 10.0, u_str_3month) / 10.0, 6) as u_str_3month
  85. ,round(if(u_rov_3month > 10.0, 10.0, u_rov_3month) / 10.0, 6) as u_rov_3month
  86. ,round(if(u_ros_3month > 10.0, 10.0, u_ros_3month) / 10.0, 6) as u_ros_3month
  87. -- 统计特征_视频
  88. ,ceil(log2(i_1day_exp_cnt + 1)) as i_1day_exp_cnt
  89. ,ceil(log2(i_1day_click_cnt + 1)) as i_1day_click_cnt
  90. ,ceil(log2(i_1day_share_cnt + 1)) as i_1day_share_cnt
  91. ,ceil(log2(i_1day_return_cnt + 1)) as i_1day_return_cnt
  92. ,ceil(log2(i_3day_exp_cnt + 1)) as i_3day_exp_cnt
  93. ,ceil(log2(i_3day_click_cnt + 1)) as i_3day_click_cnt
  94. ,ceil(log2(i_3day_share_cnt + 1)) as i_3day_share_cnt
  95. ,ceil(log2(i_3day_return_cnt + 1)) as i_3day_return_cnt
  96. ,ceil(log2(i_7day_exp_cnt + 1)) as i_7day_exp_cnt
  97. ,ceil(log2(i_7day_click_cnt + 1)) as i_7day_click_cnt
  98. ,ceil(log2(i_7day_share_cnt + 1)) as i_7day_share_cnt
  99. ,ceil(log2(i_7day_return_cnt + 1)) as i_7day_return_cnt
  100. ,ceil(log2(i_3month_exp_cnt + 1)) as i_3month_exp_cnt
  101. ,ceil(log2(i_3month_click_cnt + 1)) as i_3month_click_cnt
  102. ,ceil(log2(i_3month_share_cnt + 1)) as i_3month_share_cnt
  103. ,ceil(log2(i_3month_return_cnt + 1)) as i_3month_return_cnt
  104. ,round(if(i_ctr_1day > 10.0, 10.0, i_ctr_1day) / 10.0, 6) as i_ctr_1day
  105. ,round(if(i_str_1day > 10.0, 10.0, i_str_1day) / 10.0, 6) as i_str_1day
  106. ,round(if(i_rov_1day > 10.0, 10.0, i_rov_1day) / 10.0, 6) as i_rov_1day
  107. ,round(if(i_ros_1day > 10.0, 10.0, i_ros_1day) / 10.0, 6) as i_ros_1day
  108. ,round(if(i_ctr_3day > 10.0, 10.0, i_ctr_3day) / 10.0, 6) as i_ctr_3day
  109. ,round(if(i_str_3day > 10.0, 10.0, i_str_3day) / 10.0, 6) as i_str_3day
  110. ,round(if(i_rov_3day > 10.0, 10.0, i_rov_3day) / 10.0, 6) as i_rov_3day
  111. ,round(if(i_ros_3day > 10.0, 10.0, i_ros_3day) / 10.0, 6) as i_ros_3day
  112. ,round(if(i_ctr_7day > 10.0, 10.0, i_ctr_7day) / 10.0, 6) as i_ctr_7day
  113. ,round(if(i_str_7day > 10.0, 10.0, i_str_7day) / 10.0, 6) as i_str_7day
  114. ,round(if(i_rov_7day > 10.0, 10.0, i_rov_7day) / 10.0, 6) as i_rov_7day
  115. ,round(if(i_ros_7day > 10.0, 10.0, i_ros_7day) / 10.0, 6) as i_ros_7day
  116. ,round(if(i_ctr_3month > 10.0, 10.0, i_ctr_3month) / 10.0, 6) as i_ctr_3month
  117. ,round(if(i_str_3month > 10.0, 10.0, i_str_3month) / 10.0, 6) as i_str_3month
  118. ,round(if(i_rov_3month > 10.0, 10.0, i_rov_3month) / 10.0, 6) as i_rov_3month
  119. ,round(if(i_ros_3month > 10.0, 10.0, i_ros_3month) / 10.0, 6) as i_ros_3month
  120. from
  121. user_video_features_data_final
  122. where dt='{datetime}'
  123. and ad_ornot = '0'
  124. and apptype != '13'
  125. ), candidate_user as (
  126. SELECT
  127. u_id,
  128. max(u_1day_exp_cnt) as u_1day_exp_cnt,
  129. max(u_1day_click_cnt) as u_1day_click_cnt,
  130. max(u_1day_share_cnt) as u_1day_share_cnt,
  131. max(u_1day_return_cnt) as u_1day_return_cnt,
  132. max(u_3day_exp_cnt) as u_3day_exp_cnt,
  133. max(u_3day_click_cnt) as u_3day_click_cnt,
  134. max(u_3day_share_cnt) as u_3day_share_cnt,
  135. max(u_3day_return_cnt) as u_3day_return_cnt,
  136. max(u_7day_exp_cnt) as u_7day_exp_cnt,
  137. max(u_7day_click_cnt) as u_7day_click_cnt,
  138. max(u_7day_share_cnt) as u_7day_share_cnt,
  139. max(u_7day_return_cnt) as u_7day_return_cnt,
  140. max(u_3month_exp_cnt) as u_3month_exp_cnt,
  141. max(u_3month_click_cnt) as u_3month_click_cnt,
  142. max(u_3month_share_cnt) as u_3month_share_cnt,
  143. max(u_3month_return_cnt) as u_3month_return_cnt,
  144. max(u_ctr_1day) as u_ctr_1day,
  145. max(u_str_1day) as u_str_1day,
  146. max(u_rov_1day) as u_rov_1day,
  147. max(u_ros_1day) as u_ros_1day,
  148. max(u_ctr_3day) as u_ctr_3day,
  149. max(u_str_3day) as u_str_3day,
  150. max(u_rov_3day) as u_rov_3day,
  151. max(u_ros_3day) as u_ros_3day,
  152. max(u_ctr_7day) as u_ctr_7day,
  153. max(u_str_7day) as u_str_7day,
  154. max(u_rov_7day) as u_rov_7day,
  155. max(u_ros_7day) as u_ros_7day,
  156. max(u_ctr_3month) as u_ctr_3month,
  157. max(u_str_3month) as u_str_3month,
  158. max(u_rov_3month) as u_rov_3month,
  159. max(u_ros_3month) as u_ros_3month
  160. FROM
  161. candidate
  162. group by u_id
  163. ), candidate_item as (
  164. select
  165. i_id,
  166. max(i_up_id) as i_up_id,
  167. max(i_title_len) as i_title_len,
  168. max(i_play_len) as i_play_len,
  169. max(i_days_since_upload) as i_days_since_upload,
  170. max(i_1day_exp_cnt) as i_1day_exp_cnt,
  171. max(i_1day_click_cnt) as i_1day_click_cnt,
  172. max(i_1day_share_cnt) as i_1day_share_cnt,
  173. max(i_1day_return_cnt) as i_1day_return_cnt,
  174. max(i_3day_exp_cnt) as i_3day_exp_cnt,
  175. max(i_3day_click_cnt) as i_3day_click_cnt,
  176. max(i_3day_share_cnt) as i_3day_share_cnt,
  177. max(i_3day_return_cnt) as i_3day_return_cnt,
  178. max(i_7day_exp_cnt) as i_7day_exp_cnt,
  179. max(i_7day_click_cnt) as i_7day_click_cnt,
  180. max(i_7day_share_cnt) as i_7day_share_cnt,
  181. max(i_7day_return_cnt) as i_7day_return_cnt,
  182. max(i_3month_exp_cnt) as i_3month_exp_cnt,
  183. max(i_3month_click_cnt) as i_3month_click_cnt,
  184. max(i_3month_share_cnt) as i_3month_share_cnt,
  185. max(i_3month_return_cnt) as i_3month_return_cnt,
  186. max(i_ctr_1day) as i_ctr_1day,
  187. max(i_str_1day) as i_str_1day,
  188. max(i_rov_1day) as i_rov_1day,
  189. max(i_ros_1day) as i_ros_1day,
  190. max(i_ctr_3day) as i_ctr_3day,
  191. max(i_str_3day) as i_str_3day,
  192. max(i_rov_3day) as i_rov_3day,
  193. max(i_ros_3day) as i_ros_3day,
  194. max(i_ctr_7day) as i_ctr_7day,
  195. max(i_str_7day) as i_str_7day,
  196. max(i_rov_7day) as i_rov_7day,
  197. max(i_ros_7day) as i_ros_7day,
  198. max(i_ctr_3month) as i_ctr_3month,
  199. max(i_str_3month) as i_str_3month,
  200. max(i_rov_3month) as i_rov_3month,
  201. max(i_ros_3month) as i_ros_3month
  202. FROM
  203. candidate
  204. group by i_id
  205. )
  206. SELECT
  207. *
  208. from candidate_item
  209. """.format(datetime=datetime)
  210. # print(sql)
  211. data = exe_sql(project, sql)
  212. print('sql done')
  213. # data.to_csv('./data/ad_out_sample_v2_item.{datetime}'.format(datetime=datetime), sep='\t')
  214. # data = pd.read_csv('./data/ad_out_sample_v2_item.{datetime}'.format(datetime=datetime), sep='\t', dtype=str)
  215. model_key = 'ad_out_v2_model_v1.day'
  216. lr_model = LrModel('model/{}.json'.format(model_key))
  217. item_h_dict = {}
  218. k_col = 'i_id'
  219. dt = datetime
  220. key_name_prefix = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_ITEM}{model_key}"
  221. print(key_name_prefix)
  222. mean_item_h = 0.0
  223. count_item_h = 0
  224. with data.open_reader() as reader:
  225. for row in tqdm(reader):
  226. k = str(row['i_id'])
  227. item_features = get_item_features(row)
  228. item_h = lr_model.predict_h(item_features)
  229. redis_helper.set_data_to_redis(f"{key_name_prefix}:{k}", item_h, 28 * 3600)
  230. item_h_dict[k] = item_h
  231. mean_item_h += item_h
  232. count_item_h += 1
  233. # print(item_features)
  234. # print(item_h)
  235. mean_item_h = mean_item_h / count_item_h
  236. item_h_dict['mean'] = mean_item_h
  237. print(mean_item_h)
  238. print(count_item_h)
  239. k = 'mean'
  240. redis_helper.set_data_to_redis(f"{key_name_prefix}:{k}", mean_item_h, 28 * 3600)
  241. with open('{}.json'.format(key_name_prefix), 'w') as fout:
  242. json.dump(item_h_dict, fout, indent=2, ensure_ascii=False, sort_keys=True)