process_data.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. process the data to satisfy the lightgbm
  3. """
  4. import sys
  5. import os
  6. import json
  7. from tqdm import tqdm
  8. import jieba.analyse
  9. sys.path.append(os.getcwd())
  10. from functions import generate_label_date, MysqlClient
  11. class DataProcessor(object):
  12. """
  13. Process the data to satisfy the lightGBM
  14. """
  15. def __init__(self, flag):
  16. self.client = MysqlClient()
  17. self.flag = flag
  18. def generate_train_label(self, item, y_ori_data, cate):
  19. """
  20. 生成训练数据,用 np.array矩阵的方式返回,
  21. :return: x_train, 训练数据, y_train, 训练 label
  22. """
  23. video_id = item["video_id"]
  24. dt = item["dt"]
  25. userful_features = [
  26. "uid",
  27. "type",
  28. "channel",
  29. "fans",
  30. "view_count_user_30days",
  31. "share_count_user_30days",
  32. "return_count_user_30days",
  33. "rov_user",
  34. "str_user",
  35. "out_user_id",
  36. "mode",
  37. "out_play_cnt",
  38. "out_like_cnt",
  39. "out_share_cnt",
  40. "out_collection_cnt",
  41. ]
  42. item_features = [item[i] for i in userful_features]
  43. keywords_textrank = self.title_processor(video_id)
  44. if keywords_textrank:
  45. for i in range(3):
  46. try:
  47. item_features.append(keywords_textrank[i])
  48. except:
  49. item_features.append(None)
  50. else:
  51. item_features.append(None)
  52. item_features.append(None)
  53. item_features.append(None)
  54. label_dt = generate_label_date(dt)
  55. label_obj = y_ori_data.get(label_dt, {}).get(video_id)
  56. if label_obj:
  57. label = int(label_obj[cate]) if label_obj[cate] else 0
  58. else:
  59. label = 0
  60. return label, item_features
  61. def title_processor(self, video_id):
  62. """
  63. 通过 video_id 去获取title, 然后通过 title 再分词,把关键词作为 feature
  64. :param video_id: the video id
  65. :return: tag_list [tag, tag, tag, tag......]
  66. """
  67. sql = f"""SELECT title from wx_video where id = {video_id};"""
  68. try:
  69. title = self.client.select(sql)[0][0]
  70. keywords_textrank = jieba.analyse.textrank(title, topK=3)
  71. return list(keywords_textrank)
  72. except Exception as e:
  73. print(video_id, "\t", e)
  74. return []
  75. def producer(self):
  76. """
  77. 生成数据
  78. :return:none
  79. """
  80. if self.flag == "train":
  81. x_path = "data/hour_train.json"
  82. y_path = "data/daily-label-20240101-20240320.json"
  83. elif self.flag == "predict":
  84. x_path = "prid_data/train_0320.json"
  85. y_path = "data/daily-label-20240321-20240325.json"
  86. else:
  87. return
  88. with open(x_path) as f:
  89. x_data = json.loads(f.read())
  90. with open(y_path) as f:
  91. y_data = json.loads(f.read())
  92. cate_list = ["total_return"]
  93. for c in cate_list:
  94. x_list = []
  95. y_list = []
  96. for video_obj in tqdm(x_data):
  97. our_label, features = self.generate_train_label(video_obj, y_data, c)
  98. x_list.append(features)
  99. y_list.append(our_label)
  100. with open("produce_data/x_data_{}_{}.json".format(c, self.flag), "w") as f1:
  101. f1.write(json.dumps(x_list, ensure_ascii=False))
  102. with open("produce_data/y_data_{}_{}.json".format(c, self.flag), "w") as f2:
  103. f2.write(json.dumps(y_list, ensure_ascii=False))
  104. if __name__ == "__main__":
  105. D = DataProcessor(flag="predict")
  106. D.producer()