data_process_config_entity.py 5.8 KB

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