homo_binning_cpn.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from federatedml.model_base import ModelBase
  16. from federatedml.param.feature_binning_param import HomoFeatureBinningParam
  17. from federatedml.feature.homo_feature_binning import virtual_summary_binning, recursive_query_binning
  18. from federatedml.util import consts
  19. from federatedml.feature.hetero_feature_binning.base_feature_binning import BaseFeatureBinning
  20. from federatedml.transfer_variable.transfer_class.homo_binning_transfer_variable import HomoBinningTransferVariable
  21. class HomoBinningArbiter(BaseFeatureBinning):
  22. def __init__(self):
  23. super().__init__()
  24. self.binning_obj = None
  25. self.transfer_variable = HomoBinningTransferVariable()
  26. self.model_param = HomoFeatureBinningParam()
  27. def _init_model(self, model_param):
  28. self.model_param = model_param
  29. if self.model_param.method == consts.VIRTUAL_SUMMARY:
  30. self.binning_obj = virtual_summary_binning.Server(self.model_param)
  31. elif self.model_param.method == consts.RECURSIVE_QUERY:
  32. self.binning_obj = recursive_query_binning.Server(self.model_param)
  33. else:
  34. raise ValueError(f"Method: {self.model_param.method} cannot be recognized")
  35. def fit(self, *args):
  36. self.binning_obj.set_transfer_variable(self.transfer_variable)
  37. self.binning_obj.fit_split_points()
  38. def transform(self, data_instances):
  39. pass
  40. class HomoBinningClient(BaseFeatureBinning):
  41. def __init__(self):
  42. super().__init__()
  43. self.binning_obj = None
  44. self.transfer_variable = HomoBinningTransferVariable()
  45. self.model_param = HomoFeatureBinningParam()
  46. def _init_model(self, model_param: HomoFeatureBinningParam):
  47. self.transform_type = self.model_param.transform_param.transform_type
  48. self.model_param = model_param
  49. if self.model_param.method == consts.VIRTUAL_SUMMARY:
  50. self.binning_obj = virtual_summary_binning.Client(self.model_param)
  51. elif self.model_param.method == consts.RECURSIVE_QUERY:
  52. self.binning_obj = recursive_query_binning.Client(role=self.component_properties.role,
  53. params=self.model_param
  54. )
  55. else:
  56. raise ValueError(f"Method: {self.model_param.method} cannot be recognized")
  57. def fit(self, data_instances):
  58. self._abnormal_detection(data_instances)
  59. self._setup_bin_inner_param(data_instances, self.model_param)
  60. transformed_instances = data_instances.mapValues(self.data_format_transform)
  61. transformed_instances.schema = self.schema
  62. self.binning_obj.set_bin_inner_param(self.bin_inner_param)
  63. self.binning_obj.set_transfer_variable(self.transfer_variable)
  64. split_points = self.binning_obj.fit_split_points(transformed_instances)
  65. data_out = self.transform(data_instances)
  66. summary = {}
  67. for k, v in split_points.items():
  68. summary[k] = list(v)
  69. self.set_summary({"split_points": summary})
  70. return data_out
  71. def transform(self, data_instances):
  72. return self.transform_data(data_instances)