scorecard_param.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 ScorecardParam(BaseParam):
  21. """
  22. Define method used for transforming prediction score to credit score
  23. Parameters
  24. ----------
  25. method : {"credit"}, default: 'credit'
  26. score method, currently only supports "credit"
  27. offset : int or float, default: 500
  28. score baseline
  29. factor : int or float, default: 20
  30. scoring step, when odds double, result score increases by this factor
  31. factor_base : int or float, default: 2
  32. factor base, value ln(factor_base) is used for calculating result score
  33. upper_limit_ratio : int or float, default: 3
  34. upper bound for odds, credit score upper bound is upper_limit_ratio * offset
  35. lower_limit_value : int or float, default: 0
  36. lower bound for result score
  37. need_run : bool, default: True
  38. Indicate if this module needs to be run.
  39. """
  40. def __init__(
  41. self,
  42. method="credit",
  43. offset=500,
  44. factor=20,
  45. factor_base=2,
  46. upper_limit_ratio=3,
  47. lower_limit_value=0,
  48. need_run=True):
  49. super(ScorecardParam, self).__init__()
  50. self.method = method
  51. self.offset = offset
  52. self.factor = factor
  53. self.factor_base = factor_base
  54. self.upper_limit_ratio = upper_limit_ratio
  55. self.lower_limit_value = lower_limit_value
  56. self.need_run = need_run
  57. def check(self):
  58. descr = "scorecard param"
  59. if not isinstance(self.method, str):
  60. raise ValueError(f"{descr}method {self.method} not supported, should be str type")
  61. else:
  62. user_input = self.method.lower()
  63. if user_input == "credit":
  64. self.method = consts.CREDIT
  65. else:
  66. raise ValueError(f"{descr} method {user_input} not supported")
  67. if type(self.offset).__name__ not in ["int", "long", "float"]:
  68. raise ValueError(f"{descr} offset must be numeric,"
  69. f"received {type(self.offset)} instead.")
  70. if type(self.factor).__name__ not in ["int", "long", "float"]:
  71. raise ValueError(f"{descr} factor must be numeric,"
  72. f"received {type(self.factor)} instead.")
  73. if type(self.factor_base).__name__ not in ["int", "long", "float"]:
  74. raise ValueError(f"{descr} factor_base must be numeric,"
  75. f"received {type(self.factor_base)} instead.")
  76. if type(self.upper_limit_ratio).__name__ not in ["int", "long", "float"]:
  77. raise ValueError(f"{descr} upper_limit_ratio must be numeric,"
  78. f"received {type(self.upper_limit_ratio)} instead.")
  79. if type(self.lower_limit_value).__name__ not in ["int", "long", "float"]:
  80. raise ValueError(f"{descr} lower_limit_value must be numeric,"
  81. f"received {type(self.lower_limit_value)} instead.")
  82. BaseParam.check_boolean(self.need_run, descr=descr + "need_run ")
  83. LOGGER.debug("Finish Scorecard parameter check!")
  84. return True