sample_weight_param.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. from federatedml.param.base_param import BaseParam
  19. from federatedml.util import consts, LOGGER
  20. class SampleWeightParam(BaseParam):
  21. """
  22. Define sample weight parameters
  23. Parameters
  24. ----------
  25. class_weight : str or dict, or None, default None
  26. class weight dictionary or class weight computation mode, string value only accepts 'balanced';
  27. If dict provided, key should be class(label), and weight will not be normalize, e.g.: {'0': 1, '1': 2}
  28. If both class_weight and sample_weight_name are None, return original input data.
  29. sample_weight_name : str
  30. name of column which specifies sample weight.
  31. feature name of sample weight; if both class_weight and sample_weight_name are None, return original input data
  32. normalize : bool, default False
  33. whether to normalize sample weight extracted from `sample_weight_name` column
  34. need_run : bool, default True
  35. whether to run this module or not
  36. """
  37. def __init__(self, class_weight=None, sample_weight_name=None, normalize=False, need_run=True):
  38. self.class_weight = class_weight
  39. self.sample_weight_name = sample_weight_name
  40. self.normalize = normalize
  41. self.need_run = need_run
  42. def check(self):
  43. descr = "sample weight param's"
  44. if self.class_weight:
  45. if not isinstance(self.class_weight, str) and not isinstance(self.class_weight, dict):
  46. raise ValueError(f"{descr} class_weight must be str, dict, or None.")
  47. if isinstance(self.class_weight, str):
  48. self.class_weight = self.check_and_change_lower(self.class_weight,
  49. [consts.BALANCED],
  50. f"{descr} class_weight")
  51. if isinstance(self.class_weight, dict):
  52. for k, v in self.class_weight.items():
  53. if v < 0:
  54. LOGGER.warning(f"Negative value {v} provided for class {k} as class_weight.")
  55. if self.sample_weight_name:
  56. self.check_string(self.sample_weight_name, f"{descr} sample_weight_name")
  57. self.check_boolean(self.need_run, f"{descr} need_run")
  58. self.check_boolean(self.normalize, f"{descr} normalize")
  59. return True