data_process_config_entity.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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] = None, fill_method: str = None,
  14. split_method: str = None, feature_search_strategy: str = 'iv', bin_search_interval: float = 0.05,
  15. iv_threshold: float = 0.03, iv_threshold_wide: float = 0.05, corr_threshold: float = 0.4,
  16. sample_rate: float = 0.1, x_candidate_num: int = 10, special_values: Union[dict, list] = None):
  17. # 定义y变量
  18. self._y_column = y_column
  19. # 候选x变量
  20. self._x_columns_candidate = x_columns_candidate
  21. # 缺失值填充方法
  22. self._fill_method = fill_method
  23. # 数据划分方法
  24. self._split_method = split_method
  25. # 最优特征搜索方法
  26. self._feature_search_strategy = feature_search_strategy
  27. # 使用iv筛变量时的阈值
  28. self._iv_threshold = iv_threshold
  29. # 使用iv粗筛变量时的阈值
  30. self._iv_threshold_wide = iv_threshold_wide
  31. # 贪婪搜索分箱时数据粒度大小,应该在0.01-0.1之间
  32. self._bin_search_interval = bin_search_interval
  33. # 最终保留多少x变量
  34. self._x_candidate_num = x_candidate_num
  35. self._special_values = special_values
  36. # 变量相关性阈值
  37. self._corr_threshold = corr_threshold
  38. # 贪婪搜索采样比例,只针对4箱5箱时有效
  39. self._sample_rate = sample_rate
  40. @property
  41. def sample_rate(self):
  42. return self._sample_rate
  43. @property
  44. def corr_threshold(self):
  45. return self._corr_threshold
  46. @property
  47. def iv_threshold_wide(self):
  48. return self._iv_threshold_wide
  49. @property
  50. def candidate_num(self):
  51. return self._x_candidate_num
  52. @property
  53. def y_column(self):
  54. return self._y_column
  55. @property
  56. def x_columns_candidate(self):
  57. return self._x_columns_candidate
  58. @property
  59. def fill_method(self):
  60. return self._fill_method
  61. @property
  62. def split_method(self):
  63. return self._split_method
  64. @property
  65. def feature_search_strategy(self):
  66. return self._feature_search_strategy
  67. @property
  68. def iv_threshold(self):
  69. return self._iv_threshold
  70. @property
  71. def bin_search_interval(self):
  72. return self._bin_search_interval
  73. @property
  74. def special_values(self):
  75. return self._special_values
  76. def get_special_values(self, column: str = None):
  77. if column is None or isinstance(self._special_values, list):
  78. return self._special_values
  79. if isinstance(self._special_values, dict) and column is not None:
  80. return self._special_values.get(column, [])
  81. return []
  82. @staticmethod
  83. def from_config(config_path: str):
  84. """
  85. 从配置文件生成实体类
  86. """
  87. if os.path.exists(config_path):
  88. with open(config_path, mode="r", encoding="utf-8") as f:
  89. j = json.loads(f.read())
  90. else:
  91. raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"指配置文件【{config_path}】不存在")
  92. return DataProcessConfigEntity(**j)
  93. if __name__ == "__main__":
  94. pass