quantile_tool.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import functools
  16. from federatedml.feature.binning.quantile_binning import QuantileBinning
  17. from federatedml.param.feature_binning_param import FeatureBinningParam
  18. from federatedml.statistic import data_overview
  19. from federatedml.util import consts, LOGGER
  20. class QuantileBinningTool(QuantileBinning):
  21. """
  22. Use for quantile binning data directly.
  23. """
  24. def __init__(self, bin_nums=consts.G_BIN_NUM, param_obj: FeatureBinningParam = None,
  25. abnormal_list=None, allow_duplicate=False):
  26. if param_obj is None:
  27. param_obj = FeatureBinningParam(bin_num=bin_nums)
  28. super().__init__(params=param_obj, abnormal_list=abnormal_list, allow_duplicate=allow_duplicate)
  29. self.has_fit = False
  30. def fit_split_points(self, data_instances):
  31. res = super(QuantileBinningTool, self).fit_split_points(data_instances)
  32. self.has_fit = True
  33. return res
  34. def fit_summary(self, data_instances, is_sparse=None):
  35. if is_sparse is None:
  36. is_sparse = data_overview.is_sparse_data(data_instances)
  37. LOGGER.debug(f"is_sparse: {is_sparse}")
  38. f = functools.partial(self.feature_summary,
  39. params=self.params,
  40. abnormal_list=self.abnormal_list,
  41. cols_dict=self.bin_inner_param.bin_cols_map,
  42. header=self.header,
  43. is_sparse=is_sparse)
  44. summary_dict_table = data_instances.mapReducePartitions(f, self.copy_merge)
  45. # summary_dict = dict(summary_dict.collect())
  46. if is_sparse:
  47. total_count = data_instances.count()
  48. summary_dict_table = summary_dict_table.mapValues(lambda x: x.set_total_count(total_count))
  49. return summary_dict_table
  50. def get_quantile_point(self, quantile):
  51. """
  52. Return the specific quantile point value
  53. Parameters
  54. ----------
  55. quantile : float, 0 <= quantile <= 1
  56. Specify which column(s) need to apply statistic.
  57. Returns
  58. -------
  59. return a dict of result quantile points.
  60. eg.
  61. quantile_point = {"x1": 3, "x2": 5... }
  62. """
  63. if not self.has_fit:
  64. raise RuntimeError("Quantile Binning Tool's split points should be fit before calling"
  65. " get quantile points")
  66. f = functools.partial(self._get_split_points,
  67. allow_duplicate=self.allow_duplicate,
  68. percentile_rate=[quantile])
  69. quantile_points = dict(self.summary_dict.mapValues(f).collect())
  70. quantile_points = {k: v[0] for k, v in quantile_points.items()}
  71. return quantile_points
  72. def get_median(self):
  73. return self.get_quantile_point(0.5)