preprocess.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 tokenize_features(self, row):
  17. """根据每款烟的特征生成sentence"""
  18. tokens = []
  19. for col in ProductConfig.FEATURE_COLUMNS:
  20. if col == 'product_code':
  21. continue
  22. if col in ["direct_retail_price", "tbc_total_length"]:
  23. tokens.append(f"{col}_{row[col].replace('-', '_')}")
  24. else:
  25. tokens.append(f"{col}_{row[col]}")
  26. return tokens
  27. def generate_sentence(self):
  28. sentcens = self._product_data.apply(self.tokenize_features, axis=1).tolist()
  29. return sentcens
  30. if __name__ == "__main__":
  31. city_uuid = "00000000000000000000000011445301"
  32. processor = Item2VecDataProcess(city_uuid)
  33. processor.generate_sentence()