hot_recall.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @filename : hot_recall.py
  5. @description : 热度召回算法
  6. @time : 2025/01/21/00
  7. @author : Sherlock1011 & Min1027
  8. @Version : 1.0
  9. '''
  10. import pandas as pd
  11. import redis
  12. import random
  13. import joblib
  14. random.seed(12345)
  15. class HotRecallModel:
  16. """TODO 1. 将加载数据修改为数据库加载
  17. 2. 将结果保存到redis数据库中"""
  18. def __init__(self):
  19. pass
  20. def load_dataset(self, data_path):
  21. self._order_data = pd.read_excel(data_path)
  22. def _calculate_hot_score(self, hot_name):
  23. """
  24. 根据热度指标计算热度得分
  25. :param hot_name: 热度指标
  26. :type param: string
  27. :return: 所有热度指标的得分
  28. :rtype: list
  29. """
  30. results = self._order_data.groupby("BB_RETAIL_CUSTOMER_CODE")[hot_name].mean().reset_index()
  31. sorted_results = results.sort_values(by=hot_name, ascending=False).reset_index(drop=True)
  32. item_hot_score = []
  33. # mock热度召回最大分数
  34. max_score = random.randint(85,100) * 0.01
  35. total_score = sorted_results.loc[0, hot_name] / max_score
  36. for row in sorted_results.itertuples(index=True, name="Row"):
  37. item = {row[1]:(row[2]/total_score)*100}
  38. item_hot_score.append(item)
  39. return {"key":f"hot:{hot_name}", "value":item_hot_score}
  40. def calculate_all_hot_score(self):
  41. """
  42. 计算所有的热度指标得分
  43. """
  44. hot_datas = []
  45. for col in list(self._order_data[2:]):
  46. hot_datas.appends(self._calculate_hot_score(col))
  47. return hot_datas
  48. if __name__ == "__main__":
  49. # 序列化
  50. model = HotRecallModel()
  51. joblib.dump(model, "hot_recall.model")