ad_xgboost_predict.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import time
  2. import pandas as pd
  3. import xgboost as xgb
  4. from xgboost.sklearn import XGBClassifier
  5. from utils import RedisHelper
  6. from config import set_config
  7. redis_helper = RedisHelper()
  8. config_, _ = set_config()
  9. def predict(app_type):
  10. # 1. 模型加载
  11. model = XGBClassifier()
  12. booster = xgb.Booster()
  13. booster.load_model('./data/ad_xgb.model')
  14. model._Booster = booster
  15. # 2. 预测:ad_status = 0, 不出广告
  16. df_0 = pd.read_csv('./data/predict_data/predict_data_0.csv')
  17. columns_0 = df_0.columns.values.tolist()
  18. columns_0.remove('videoid')
  19. y_pred_proba_0 = model.predict_proba(df_0[columns_0[2:]])
  20. df_0['y_0'] = [x[1] for x in y_pred_proba_0]
  21. pre_df_0 = df_0[['apptype', 'mid', 'videoid', 'y_0']].copy()
  22. # 3. 预测:ad_status = 1, 不出广告
  23. df_1 = pd.read_csv('./data/predict_data/predict_data_1.csv')
  24. columns_1 = df_1.columns.values.tolist()
  25. columns_1.remove('videoid')
  26. y_pred_proba_1 = model.predict_proba(df_1[columns_1[2:]])
  27. df_1['y_1'] = [x[1] for x in y_pred_proba_1]
  28. pre_df_1 = df_1[['apptype', 'mid', 'videoid', 'y_1']].copy()
  29. # 4. merge 结果
  30. res_df = pd.merge(pre_df_0, pre_df_1, how='left', on=['apptype', 'mid', 'videoid'])
  31. res_df['res_predict'] = res_df['y_0'] - res_df['y_1']
  32. print(res_df.head())
  33. # 5. to csv
  34. # res_df.to_csv('./data/predict_data/predict_res.csv', index=False)
  35. # print("to csv finished!")
  36. f = open('./data/predict_data/predict_res.txt', "w")
  37. xgb_config = config_.AD_MODEL_ABTEST_CONFIG['xgb']
  38. # 6. to redis
  39. for ind, row in res_df.iterrows():
  40. app_type = row['apptype']
  41. mid = row['mid']
  42. video_id = row['videoid']
  43. pre_res = row['res_predict']
  44. key = f"{xgb_config['predict_key_prefix']}{app_type}:{mid}:{video_id}"
  45. # redis_helper.set_data_to_redis(key_name=key, value=pre_res, expire_time=48*3600)
  46. f.write(f"{key},{pre_res}\n")
  47. print("to redis finished!")
  48. # 7. 计算阈值
  49. # 获取对应实验id
  50. abtest_id_mapping = xgb_config['abtest_id_mapping']
  51. abtest_id = abtest_id_mapping[app_type]
  52. # 获取阈值参数记录
  53. threshold_record = redis_helper.get_data_from_redis(key_name=xgb_config['threshold_record'])
  54. threshold_record = eval(threshold_record)
  55. record = threshold_record[abtest_id]
  56. # 分实验组进行阈值计算
  57. predict_mean = res_df['res_predict'].mean()
  58. for ab_code, param in record.items():
  59. threshold = predict_mean * param
  60. # 写入redis
  61. threshold_key = f"{xgb_config['threshold']}{abtest_id}:{ab_code}"
  62. redis_helper.set_data_to_redis(key_name=threshold_key, value=threshold, expire_time=48 * 3600)
  63. print("update threshold finished!")
  64. if __name__ == '__main__':
  65. st_time = time.time()
  66. predict(config_.APP_TYPE['VLOG'])
  67. print(f"{time.time() - st_time}s")