data_process_config_entity.py 5.6 KB

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