lr_model.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #coding utf-8
  2. import json
  3. import math
  4. import time
  5. def load_json(filename):
  6. with open(filename, 'r') as fin:
  7. json_data = json.load(fin)
  8. return json_data
  9. def sigmoid(x):
  10. return 1.0 / (1.0 + math.exp(-x))
  11. online_w = load_json('./ad_out_v1_online_w.json')
  12. def get_online_score(online_features):
  13. score = online_w.get('bias', 0.0)
  14. score += sum([online_w.get('#'.join([k, v]), 0.0) for k, v in online_features.items()])
  15. return score
  16. def get_final_score(online_features, offline_score):
  17. online_score = get_online_score(online_features)
  18. final_score = online_score + offline_score
  19. final_score = sigmoid(final_score)
  20. return final_score, online_score
  21. if __name__ == '__main__':
  22. app_type = 0
  23. online_features = {
  24. 'ctx_apptype': str(app_type),
  25. 'ctx_week': time.strftime('%w', time.localtime()),
  26. 'ctx_hour': time.strftime('%H', time.localtime()),
  27. }
  28. print(get_final_score(online_features, -1.0))
  29. print(get_final_score(online_features, 0.0))
  30. print(get_final_score({}, 0.0))