data_process_config_entity.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, breaks_list: dict = None, pos_neg_cnt=1,
  19. jupyter=False, strees=False, strees_sample_times=100, strees_bad_rate_list=[], *args, **kwargs):
  20. # 是否开启下输出内容
  21. self._strees = strees
  22. # jupyter下输出内容
  23. self._strees_sample_times = strees_sample_times
  24. # jupyter下输出内容
  25. self._strees_bad_rate_list = strees_bad_rate_list
  26. # jupyter下输出内容
  27. self._jupyter = jupyter
  28. # 单调性允许变化次数
  29. self._pos_neg_cnt = pos_neg_cnt
  30. # 是否启用粗分箱
  31. self._format_bin = format_bin
  32. # 项目名称,和缓存路径有关
  33. self._project_name = project_name
  34. # 定义y变量
  35. self._y_column = y_column
  36. # 候选x变量
  37. self._x_columns_candidate = x_columns_candidate
  38. # 缺失值填充方法
  39. self._fill_method = fill_method
  40. # 缺失值填充值
  41. self._fill_value = fill_value
  42. # 数据划分方法
  43. self._split_method = split_method
  44. # 最优特征搜索方法
  45. self._feature_search_strategy = feature_search_strategy
  46. # 使用iv筛变量时的阈值
  47. self._iv_threshold = iv_threshold
  48. # 使用iv粗筛变量时的阈值
  49. self._iv_threshold_wide = iv_threshold_wide
  50. # 贪婪搜索分箱时数据粒度大小,应该在0.01-0.1之间
  51. self._bin_search_interval = bin_search_interval
  52. # 最终保留多少x变量
  53. self._x_candidate_num = x_candidate_num
  54. self._special_values = special_values
  55. self._breaks_list = breaks_list
  56. # 变量相关性阈值
  57. self._corr_threshold = corr_threshold
  58. # 贪婪搜索采样比例,只针对4箱5箱时有效
  59. self._sample_rate = sample_rate
  60. if self._project_name is None or len(self._project_name) == 0:
  61. self._base_dir = os.path.join(BaseConfig.train_path, f"{f_get_datetime()}")
  62. else:
  63. self._base_dir = os.path.join(BaseConfig.train_path, self._project_name)
  64. os.makedirs(self._base_dir, exist_ok=True)
  65. @property
  66. def strees(self):
  67. return self._strees
  68. @property
  69. def strees_sample_times(self):
  70. return self._strees_sample_times
  71. @property
  72. def strees_bad_rate_list(self):
  73. return self._strees_bad_rate_list
  74. @property
  75. def jupyter(self):
  76. return self._jupyter
  77. @property
  78. def base_dir(self):
  79. return self._base_dir
  80. @property
  81. def pos_neg_cnt(self):
  82. return self._pos_neg_cnt
  83. @property
  84. def format_bin(self):
  85. return self._format_bin
  86. @property
  87. def project_name(self):
  88. return self._project_name
  89. @property
  90. def sample_rate(self):
  91. return self._sample_rate
  92. @property
  93. def corr_threshold(self):
  94. return self._corr_threshold
  95. @property
  96. def iv_threshold_wide(self):
  97. return self._iv_threshold_wide
  98. @property
  99. def candidate_num(self):
  100. return self._x_candidate_num
  101. @property
  102. def y_column(self):
  103. return self._y_column
  104. @property
  105. def x_columns_candidate(self):
  106. return self._x_columns_candidate
  107. @property
  108. def fill_value(self):
  109. return self._fill_value
  110. @property
  111. def fill_method(self):
  112. return self._fill_method
  113. @property
  114. def split_method(self):
  115. return self._split_method
  116. @property
  117. def feature_search_strategy(self):
  118. return self._feature_search_strategy
  119. @property
  120. def iv_threshold(self):
  121. return self._iv_threshold
  122. @property
  123. def bin_search_interval(self):
  124. return self._bin_search_interval
  125. @property
  126. def special_values(self):
  127. if self._special_values is None or len(self._special_values) == 0:
  128. return None
  129. if isinstance(self._special_values, str):
  130. return [self._special_values]
  131. if isinstance(self._special_values, (dict, list)):
  132. return self._special_values
  133. return None
  134. def get_special_values(self, column: str = None):
  135. if self._special_values is None or len(self._special_values) == 0:
  136. return []
  137. if isinstance(self._special_values, str):
  138. return [self._special_values]
  139. if isinstance(self._special_values, list):
  140. return self._special_values
  141. if isinstance(self._special_values, dict) and column is not None:
  142. return self._special_values.get(column, [])
  143. return []
  144. @property
  145. def breaks_list(self):
  146. if self._breaks_list is None:
  147. return {}
  148. if isinstance(self._breaks_list, dict):
  149. return self._breaks_list
  150. return {}
  151. def get_breaks_list(self, column: str = None):
  152. if self._breaks_list is None or len(self._breaks_list) == 0:
  153. return []
  154. if isinstance(self._breaks_list, dict) and column is not None:
  155. return self._breaks_list.get(column, [])
  156. return []
  157. def f_get_save_path(self, file_name: str) -> str:
  158. path = os.path.join(self._base_dir, file_name)
  159. return path
  160. @staticmethod
  161. def from_config(config_path: str):
  162. """
  163. 从配置文件生成实体类
  164. """
  165. if os.path.exists(config_path):
  166. with open(config_path, mode="r", encoding="utf-8") as f:
  167. j = json.loads(f.read())
  168. else:
  169. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  170. return DataProcessConfigEntity(**j)
  171. if __name__ == "__main__":
  172. pass