sample_weight_param.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 pipeline.param.base_param import BaseParam
  19. from pipeline.param import consts
  20. class SampleWeightParam(BaseParam):
  21. """
  22. Define sample weight parameters.
  23. Parameters
  24. ----------
  25. class_weight : str or dict, 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, name of column which specifies sample weight.
  30. feature name of sample weight; if both class_weight and sample_weight_name are None, return original input data
  31. normalize : bool, default False
  32. whether to normalize sample weight extracted from `sample_weight_name` column
  33. need_run : bool, default True
  34. whether to run this module or not
  35. """
  36. def __init__(self, class_weight=None, sample_weight_name=None, normalize=False, need_run=True):
  37. self.class_weight = class_weight
  38. self.sample_weight_name = sample_weight_name
  39. self.normalize = normalize
  40. self.need_run = need_run
  41. def check(self):
  42. descr = "sample weight param's"
  43. if self.class_weight:
  44. if not isinstance(self.class_weight, str) and not isinstance(self.class_weight, dict):
  45. raise ValueError(f"{descr} class_weight must be str, dict, or None.")
  46. if isinstance(self.class_weight, str):
  47. self.class_weight = self.check_and_change_lower(self.class_weight,
  48. [consts.BALANCED],
  49. f"{descr} class_weight")
  50. if self.sample_weight_name:
  51. self.check_string(self.sample_weight_name, f"{descr} sample_weight_name")
  52. self.check_boolean(self.need_run, f"{descr} need_run")
  53. self.check_boolean(self.normalize, f"{descr} normalize")
  54. return True