stepwise_param.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. from federatedml.param.base_param import BaseParam
  18. from federatedml.util import consts
  19. class StepwiseParam(BaseParam):
  20. """
  21. Define stepwise params
  22. Parameters
  23. ----------
  24. score_name: {"AIC", "BIC"}, default: 'AIC'
  25. Specify which model selection criterion to be used
  26. mode: {"Hetero", "Homo"}, default: 'Hetero'
  27. Indicate what mode is current task
  28. role: {"Guest", "Host", "Arbiter"}, default: 'Guest'
  29. Indicate what role is current party
  30. direction: {"both", "forward", "backward"}, default: 'both'
  31. Indicate which direction to go for stepwise.
  32. 'forward' means forward selection; 'backward' means elimination; 'both' means possible models of both directions are examined at each step.
  33. max_step: int, default: '10'
  34. Specify total number of steps to run before forced stop.
  35. nvmin: int, default: '2'
  36. Specify the min subset size of final model, cannot be lower than 2. When nvmin > 2, the final model size may be smaller than nvmin due to max_step limit.
  37. nvmax: int, default: None
  38. Specify the max subset size of final model, 2 <= nvmin <= nvmax. The final model size may be larger than nvmax due to max_step limit.
  39. need_stepwise: bool, default False
  40. Indicate if this module needed to be run
  41. """
  42. def __init__(self, score_name="AIC", mode=consts.HETERO, role=consts.GUEST, direction="both",
  43. max_step=10, nvmin=2, nvmax=None, need_stepwise=False):
  44. super(StepwiseParam, self).__init__()
  45. self.score_name = score_name
  46. self.mode = mode
  47. self.role = role
  48. self.direction = direction
  49. self.max_step = max_step
  50. self.nvmin = nvmin
  51. self.nvmax = nvmax
  52. self.need_stepwise = need_stepwise
  53. def check(self):
  54. model_param_descr = "stepwise param's"
  55. self.score_name = self.check_and_change_lower(self.score_name, ["aic", "bic"], model_param_descr)
  56. self.check_valid_value(self.mode, model_param_descr, valid_values=[consts.HOMO, consts.HETERO])
  57. self.check_valid_value(self.role, model_param_descr, valid_values=[consts.HOST, consts.GUEST, consts.ARBITER])
  58. self.direction = self.check_and_change_lower(self.direction, ["forward", "backward", "both"], model_param_descr)
  59. self.check_positive_integer(self.max_step, model_param_descr)
  60. self.check_positive_integer(self.nvmin, model_param_descr)
  61. if self.nvmin < 2:
  62. raise ValueError(model_param_descr + " nvmin must be no less than 2.")
  63. if self.nvmax is not None:
  64. self.check_positive_integer(self.nvmax, model_param_descr)
  65. if self.nvmin > self.nvmax:
  66. raise ValueError(model_param_descr + " nvmax must be greater than nvmin.")
  67. self.check_boolean(self.need_stepwise, model_param_descr)