cal_hour_score.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # coding utf-8
  2. import sys
  3. import json
  4. import math
  5. import pandas as pd
  6. features = [
  7. 'apptype',
  8. 'code',
  9. 'videoid',
  10. 'lastonehour_preview', # 过去1小时预曝光人数
  11. 'lastonehour_view', # 过去1小时曝光人数
  12. 'lastonehour_play', # 过去1小时播放人数
  13. 'lastonehour_share', # 过去1小时分享人数
  14. 'lastonehour_return', # 过去1小时分享,过去1小时回流人数
  15. 'lastonehour_preview_total', # 过去1小时预曝光次数
  16. 'lastonehour_view_total', # 过去1小时曝光次数
  17. 'lastonehour_play_total', # 过去1小时播放次数
  18. 'lastonehour_share_total', # 过去1小时分享次数
  19. 'platform_return',
  20. 'lastonehour_show', # 不区分地域
  21. 'lastonehour_show_region', # 地域分组
  22. 'lasttwohour_share', # h-2小时分享人数
  23. 'lasttwohour_return_now', # h-2分享,过去1小时回流人数
  24. 'lasttwohour_return', # h-2分享,h-2回流人数
  25. 'lastthreehour_share', # h-3小时分享人数
  26. 'lastthreehour_return_now', # h-3分享,过去1小时回流人数
  27. 'lastthreehour_return', # h-3分享,h-3回流人数
  28. 'lastonehour_return_new', # 过去1小时分享,过去1小时回流人数(回流统计为对应地域分享带回的回流,分享限制地域,回流不限制地域)
  29. 'lasttwohour_return_now_new', # h-2分享,过去1小时回流人数(回流统计为对应地域分享带回的回流,分享限制地域,回流不限制地域)
  30. 'lasttwohour_return_new', # h-2分享,h-2回流人数(回流统计为对应地域分享带回的回流,分享限制地域,回流不限制地域)
  31. 'lastthreehour_return_now_new', # h-3分享,过去1小时回流人数(回流统计为对应地域分享带回的回流,分享限制地域,回流不限制地域)
  32. 'lastthreehour_return_new', # h-3分享,h-3回流人数(回流统计为对应地域分享带回的回流,分享限制地域,回流不限制地域)
  33. 'platform_return_new', # 平台分发回流(回流统计为对应地域分享带回的回流,分享限制地域,回流不限制地域)
  34. ]
  35. def data_group(data_path):
  36. """将数据按照videoid聚合(求和)"""
  37. f = open(data_path)
  38. index = 0
  39. data_dict = {}
  40. while True:
  41. line = f.readline()
  42. if not line:
  43. break
  44. if index == 0:
  45. index += 1
  46. continue
  47. index += 1
  48. items = line.strip().split(",")
  49. # print(items)
  50. if len(items) < len(features):
  51. continue
  52. video_id = items[2]
  53. if video_id not in data_dict:
  54. data_dict[video_id] = {'videoid': video_id}
  55. for i, feature in enumerate(features):
  56. if feature in ['apptype', 'code', 'videoid']:
  57. continue
  58. data_dict[video_id][feature] = int(float(items[i]))
  59. else:
  60. for i, feature in enumerate(features):
  61. if feature in ['apptype', 'code', 'videoid']:
  62. continue
  63. data_dict[video_id][feature] = data_dict[video_id][feature] + int(float(items[i]))
  64. f.close()
  65. data_list = [item for video_id, item in data_dict.items()]
  66. data_df = pd.DataFrame(data_list)
  67. return data_df
  68. def cal_score(data_df):
  69. """计算score"""
  70. df = data_df.copy()
  71. # share_rate_view = (share+1)/(view+1000)
  72. df['share_rate_view'] = (df['lastonehour_share'] + 1) / (df['lastonehour_view'] + 1000)
  73. # back_rate = (return+1)/(share+10)
  74. df['back_rate'] = (df['lastonehour_return'] + 1) / (df['lastonehour_share'] + 10)
  75. # back_rate_2h = (lasttwohour_return_now+1)/(share+10)
  76. df['back_rate_2h'] = (df['lasttwohour_return_now'] + 1) / (df['lasttwohour_share'] + 10)
  77. # back_rate_3h = (lastthreehour_return_now+1)/(share+10)
  78. df['back_rate_3h'] = (df['lastthreehour_return_now'] + 1) / (df['lastthreehour_share'] + 10)
  79. df['log_back'] = (df['lastonehour_return'] + 1).apply(math.log)
  80. # h-2回流留存
  81. df['return_retention_initial_2h'] = (df['lasttwohour_return_now'] + 1) / (df['lasttwohour_return'] + 5)
  82. df['return_retention_2h'] = df['return_retention_initial_2h'].apply(lambda x: 1 if x > 1 else x)
  83. # h-3回流留存
  84. df['return_retention_initial_3h'] = (df['lastthreehour_return_now'] + 1) / (df['lastthreehour_return'] + 10)
  85. df['return_retention_3h'] = df['return_retention_initial_3h'].apply(lambda x: 0.8 if x > 0.8 else x)
  86. # score1 = 回流/(view+5)
  87. df['hour_score1'] = df['lastonehour_return'] / (df['lastonehour_view'] + 5)
  88. # score2 = (回流 * (1 + h-2回流留存 + h-3回流留存))/(view+1000)
  89. df['hour_score2'] = (df['lastonehour_return'] * (1 + df['return_retention_2h'] + df['return_retention_3h'])) / \
  90. (df['lastonehour_view'] + 1000)
  91. # score3 = (lastthreehour_return_now + lasttwohour_return_now + lastonehour_return)/(lastonehour_view+1000)
  92. df['hour_score3'] = (df['lastthreehour_return_now'] + df['lasttwohour_return_now'] + df['lastonehour_return']) / \
  93. (df['lastonehour_view'] + 1000)
  94. # score4 = share/view * back_rate * logback
  95. df['hour_score4'] = df['share_rate_view'] * df['back_rate'] * df['log_back']
  96. # score5 = share/view * (back_rate + back_rate_2h + back_rate_3h) * logback
  97. df['hour_score5'] = df['share_rate_view'] * (df['back_rate'] + df['back_rate_2h'] + df['back_rate_3h']) * df['log_back']
  98. score_df = df[['videoid', 'hour_score1', 'hour_score2', 'hour_score3', 'hour_score4', 'hour_score5']]
  99. # print(score_df)
  100. return score_df
  101. if __name__ == "__main__":
  102. # 1.load data
  103. now_date = sys.argv[1]
  104. print(f"now_date: {now_date}")
  105. data_path = f"./data/hour_video_data_{now_date}.csv"
  106. data_df = data_group(data_path=data_path)
  107. print(f"data_df shape: {data_df.shape}")
  108. hour_score_path = f"./data/hour_score_{now_date}.csv"
  109. score_df = cal_score(data_df=data_df)
  110. score_df.to_csv(hour_score_path, index=False)
  111. print(f"score_df shape: {score_df.shape}")