data_process_config_entity.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: yq
  4. @time: 2024/11/1
  5. @desc: 数据处理配置类
  6. """
  7. import json
  8. import os
  9. from typing import List, Union
  10. from commom import GeneralException
  11. from enums import ResultCodesEnum
  12. class DataProcessConfigEntity():
  13. def __init__(self, y_column: str, x_columns_candidate: List[str], fill_method: str = None, split_method: str = None,
  14. feature_search_strategy: str = 'iv', bin_search_interval: float = 0.05, iv_threshold: float = 0.03,
  15. x_candidate_num: int = 10, special_values: Union[dict, list] = None):
  16. # 定义y变量
  17. self._y_column = y_column
  18. # 候选x变量
  19. self._x_columns_candidate = x_columns_candidate
  20. # 缺失值填充方法
  21. self._fill_method = fill_method
  22. # 数据划分方法
  23. self._split_method = split_method
  24. # 最优特征搜索方法
  25. self._feature_search_strategy = feature_search_strategy
  26. # 使用iv筛变量时的阈值
  27. self._iv_threshold = iv_threshold
  28. # 贪婪搜索分箱时数据粒度大小,应该在0.01-0.1之间
  29. self._bin_search_interval = bin_search_interval
  30. # 最终保留多少x变量
  31. self._x_candidate_num = x_candidate_num
  32. self._special_values = special_values
  33. @property
  34. def candidate_num(self):
  35. return self._x_candidate_num
  36. @property
  37. def y_column(self):
  38. return self._y_column
  39. @property
  40. def x_columns_candidate(self):
  41. return self._x_columns_candidate
  42. @property
  43. def fill_method(self):
  44. return self._fill_method
  45. @property
  46. def split_method(self):
  47. return self._split_method
  48. @property
  49. def feature_search_strategy(self):
  50. return self._feature_search_strategy
  51. @property
  52. def iv_threshold(self):
  53. return self._iv_threshold
  54. @property
  55. def bin_search_interval(self):
  56. return self._bin_search_interval
  57. @property
  58. def special_values(self):
  59. return self._special_values
  60. def get_special_values(self, column: str = None):
  61. if column is None or isinstance(self._special_values, list):
  62. return self._special_values
  63. if isinstance(self._special_values, dict) and column is not None:
  64. return self._special_values.get(column, [])
  65. return []
  66. @staticmethod
  67. def from_config(config_path: str):
  68. """
  69. 从配置文件生成实体类
  70. """
  71. if os.path.exists(config_path):
  72. with open(config_path, mode="r", encoding="utf-8") as f:
  73. j = json.loads(f.read())
  74. else:
  75. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  76. return DataProcessConfigEntity(**j)
  77. if __name__ == "__main__":
  78. pass