data_process_config_entity.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, f_get_datetime
  11. from config import BaseConfig
  12. from enums import ResultCodesEnum
  13. class DataProcessConfigEntity():
  14. def __init__(self, y_column: str, x_columns_candidate: List[str] = None, fill_method: str = None, fill_value=None,
  15. split_method: str = None, feature_search_strategy: str = 'iv', bin_search_interval: float = 0.05,
  16. iv_threshold: float = 0.03, iv_threshold_wide: float = 0.05, corr_threshold: float = 0.4,
  17. sample_rate: float = 0.1, x_candidate_num: int = 10, special_values: Union[dict, list, str] = None,
  18. project_name: str = None, format_bin: str = False, *args, **kwargs):
  19. # 是否启用粗分箱
  20. self._format_bin = format_bin
  21. # 项目名称,和缓存路径有关
  22. self._project_name = project_name
  23. # 定义y变量
  24. self._y_column = y_column
  25. # 候选x变量
  26. self._x_columns_candidate = x_columns_candidate
  27. # 缺失值填充方法
  28. self._fill_method = fill_method
  29. # 缺失值填充值
  30. self._fill_value = fill_value
  31. # 数据划分方法
  32. self._split_method = split_method
  33. # 最优特征搜索方法
  34. self._feature_search_strategy = feature_search_strategy
  35. # 使用iv筛变量时的阈值
  36. self._iv_threshold = iv_threshold
  37. # 使用iv粗筛变量时的阈值
  38. self._iv_threshold_wide = iv_threshold_wide
  39. # 贪婪搜索分箱时数据粒度大小,应该在0.01-0.1之间
  40. self._bin_search_interval = bin_search_interval
  41. # 最终保留多少x变量
  42. self._x_candidate_num = x_candidate_num
  43. self._special_values = special_values
  44. # 变量相关性阈值
  45. self._corr_threshold = corr_threshold
  46. # 贪婪搜索采样比例,只针对4箱5箱时有效
  47. self._sample_rate = sample_rate
  48. if self._project_name is None or len(self._project_name) == 0:
  49. self._base_dir = os.path.join(BaseConfig.train_path, f"{f_get_datetime()}")
  50. else:
  51. self._base_dir = os.path.join(BaseConfig.train_path, self._project_name)
  52. os.makedirs(self._base_dir, exist_ok=True)
  53. @property
  54. def base_dir(self):
  55. return self._base_dir
  56. @property
  57. def format_bin(self):
  58. return self._format_bin
  59. @property
  60. def project_name(self):
  61. return self._project_name
  62. @property
  63. def sample_rate(self):
  64. return self._sample_rate
  65. @property
  66. def corr_threshold(self):
  67. return self._corr_threshold
  68. @property
  69. def iv_threshold_wide(self):
  70. return self._iv_threshold_wide
  71. @property
  72. def candidate_num(self):
  73. return self._x_candidate_num
  74. @property
  75. def y_column(self):
  76. return self._y_column
  77. @property
  78. def x_columns_candidate(self):
  79. return self._x_columns_candidate
  80. @property
  81. def fill_value(self):
  82. return self._fill_value
  83. @property
  84. def fill_method(self):
  85. return self._fill_method
  86. @property
  87. def split_method(self):
  88. return self._split_method
  89. @property
  90. def feature_search_strategy(self):
  91. return self._feature_search_strategy
  92. @property
  93. def iv_threshold(self):
  94. return self._iv_threshold
  95. @property
  96. def bin_search_interval(self):
  97. return self._bin_search_interval
  98. @property
  99. def special_values(self):
  100. if self._special_values is None or len(self._special_values) == 0:
  101. return None
  102. if isinstance(self._special_values, str):
  103. return [self._special_values]
  104. if isinstance(self._special_values, (dict, list)):
  105. return self._special_values
  106. return None
  107. def get_special_values(self, column: str = None):
  108. if self._special_values is None or len(self._special_values) == 0:
  109. return []
  110. if isinstance(self._special_values, str):
  111. return [self._special_values]
  112. if isinstance(self._special_values, list):
  113. return self._special_values
  114. if isinstance(self._special_values, dict) and column is not None:
  115. return self._special_values.get(column, [])
  116. return []
  117. def f_get_save_path(self, file_name: str) -> str:
  118. path = os.path.join(self._base_dir, file_name)
  119. return path
  120. @staticmethod
  121. def from_config(config_path: str):
  122. """
  123. 从配置文件生成实体类
  124. """
  125. if os.path.exists(config_path):
  126. with open(config_path, mode="r", encoding="utf-8") as f:
  127. j = json.loads(f.read())
  128. else:
  129. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  130. return DataProcessConfigEntity(**j)
  131. if __name__ == "__main__":
  132. pass