yangxiaohui 1 gadu atpakaļ
vecāks
revīzija
11c0f49bb2
3 mainītis faili ar 77 papildinājumiem un 3 dzēšanām
  1. 16 3
      ad_recommend.py
  2. 37 0
      data/ad_out_v1_online_w.json
  3. 24 0
      lr_model.py

+ 16 - 3
ad_recommend.py

@@ -1,9 +1,11 @@
 import json
+import time
 import traceback
 import datetime
 from utils import RedisHelper
 from config import set_config
 from log import Log
+from lr_model import get_final_score
 log_ = Log()
 config_ = set_config()
 redis_helper = RedisHelper()
@@ -294,7 +296,8 @@ def predict_mid_video_res_with_add(now_date, mid, video_id, abtest_param, abtest
             'ad_predict': ad_predict}
     return result
 
-def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abtest_id, abtest_config_tag, ab_test_code, care_model_status):
+def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abtest_id, abtest_config_tag, ab_test_code, care_model_status, app_type):
+
     model_key = abtest_param.get('model_key', 'ad_out_v1')
     mean_key = 'mean'
     user_key_name = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_USER}{model_key}:{mid}"
@@ -321,7 +324,15 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
         else:
             item_score = 0.0
 
-    final_score = user_score + item_score
+    offline_score = user_score + item_score
+
+    online_features = {
+        'ctx_apptype': str(app_type),
+        'ctx_week': time.strftime('%w', time.localtime()),
+        'ctx_hour':  time.strftime('%H', time.localtime()),
+    }
+
+    final_score, online_score = get_final_score(online_features, offline_score)
 
     # 获取对应的阈值
     threshold = redis_helper.get_data_from_redis(key_name=threshold_key)
@@ -339,6 +350,7 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
         'user_score': user_score,
         'item_score': item_score,
         'final_score': final_score,
+        'online_score': online_score,
         'threshold': threshold,
         'ad_predict': ad_predict
     }
@@ -507,7 +519,8 @@ def ad_recommend_predict(app_type, mid, video_id, ab_exp_info, ab_test_code, car
                 abtest_id=abtest_id,
                 abtest_config_tag=abtest_config_tag,
                 ab_test_code=ab_test_code,
-                care_model_status=care_model_status
+                care_model_status=care_model_status,
+                app_type=app_type
             )
         else:
             result = predict_mid_video_res(

+ 37 - 0
data/ad_out_v1_online_w.json

@@ -0,0 +1,37 @@
+{
+  "bias": -0.119063,
+  "ctx_apptype#0": 0.179341,
+  "ctx_apptype#17": -0.212768,
+  "ctx_apptype#18": -0.0122158,
+  "ctx_apptype#19": -0.128269,
+  "ctx_apptype#22": -0.263358,
+  "ctx_apptype#3": -0.178475,
+  "ctx_apptype#4": -0.0046397,
+  "ctx_apptype#5": -0.104733,
+  "ctx_apptype#6": -0.425318,
+  "ctx_hour#00": -0.809942,
+  "ctx_hour#01": -0.816382,
+  "ctx_hour#02": -0.65927,
+  "ctx_hour#03": -0.637063,
+  "ctx_hour#04": -0.820683,
+  "ctx_hour#05": -1.19348,
+  "ctx_hour#06": -1.42801,
+  "ctx_hour#07": -1.61269,
+  "ctx_hour#08": -0.0168045,
+  "ctx_hour#09": 0.0064677,
+  "ctx_hour#10": 0.0443871,
+  "ctx_hour#11": 0.0355702,
+  "ctx_hour#12": 0.0473058,
+  "ctx_hour#13": 0.0515942,
+  "ctx_hour#14": 0.0514012,
+  "ctx_hour#15": 0.0361565,
+  "ctx_hour#16": 0.0377414,
+  "ctx_hour#17": 0.0504248,
+  "ctx_hour#18": 0.0185322,
+  "ctx_hour#19": -0.0684192,
+  "ctx_hour#20": -0.0806333,
+  "ctx_hour#21": -0.0186427,
+  "ctx_hour#22": -0.0327707,
+  "ctx_hour#23": 0.0765017,
+  "ctx_week#1": -0.119094
+}

+ 24 - 0
lr_model.py

@@ -0,0 +1,24 @@
+#coding utf-8
+import json
+import math
+def load_json(filename):
+    with open(filename, 'r') as fin:
+        json_data = json.load(fin)
+    return json_data
+
+def sigmoid(x):
+    return 1.0 / (1.0 + math.exp(-x))
+
+online_w = load_json('data/ad_out_v1_online_w.json')
+
+def get_online_score(online_features):
+    score = online_w.get('bias', 0.0)
+    score += sum([online_w.get('#'.join([k, v]), 0.0) for k, v in online_features.items()])
+    return score
+
+def get_final_score(online_features, offline_score):
+    online_score = get_online_score(online_features)
+    final_score = online_score + offline_score
+    final_score = sigmoid(final_score)
+    return final_score, online_score
+