preprocess.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from database.dao.mysql_dao import MySqlDao
  2. from models.rank.data.config import ProductConfig
  3. from models.rank.data.utils import sample_data_clear
  4. class Item2VecDataProcess:
  5. def __init__(self, city_uuid):
  6. self._mysql_dao = MySqlDao()
  7. print("item2vec: 正在加载product_info...")
  8. self._product_data = self._mysql_dao.load_product_data(city_uuid)
  9. self._data_process()
  10. def _data_process(self):
  11. """数据预处理"""
  12. # 获取指定的特征
  13. self._product_data = self._product_data[ProductConfig.FEATURE_COLUMNS]
  14. # 数据清洗
  15. self._product_data = sample_data_clear(self._product_data, ProductConfig)
  16. def item_to_token(self, row):
  17. """根据每款烟的特征生成sentence"""
  18. token = []
  19. for col in ProductConfig.FEATURE_COLUMNS:
  20. if col == 'product_code':
  21. continue
  22. else:
  23. token.append(f"{row[col].strip()}")
  24. token_map = {"product_code": row['product_code'], "token": token}
  25. return token_map
  26. def generate_tokens(self):
  27. tokens = self._product_data.apply(self.item_to_token, axis=1).tolist()
  28. return tokens
  29. if __name__ == "__main__":
  30. city_uuid = "00000000000000000000000011445301"
  31. processor = Item2VecDataProcess(city_uuid)
  32. processor.generate_tokens()