hetero_kmeans_param.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. class KmeansParam(BaseParam):
  20. """
  21. Parameters
  22. ----------
  23. k : int, default 5
  24. The number of the centroids to generate.
  25. should be larger than 1 and less than 100 in this version
  26. max_iter : int, default 300.
  27. Maximum number of iterations of the hetero-k-means algorithm to run.
  28. tol : float, default 0.001.
  29. tol
  30. random_stat : None or int
  31. random seed
  32. """
  33. def __init__(self, k=5, max_iter=300, tol=0.001, random_stat=None):
  34. super(KmeansParam, self).__init__()
  35. self.k = k
  36. self.max_iter = max_iter
  37. self.tol = tol
  38. self.random_stat = random_stat
  39. def check(self):
  40. descr = "Kmeans_param's"
  41. if not isinstance(self.k, int):
  42. raise ValueError(
  43. descr + "k {} not supported, should be int type".format(self.k))
  44. elif self.k <= 1:
  45. raise ValueError(
  46. descr + "k {} not supported, should be larger than 1")
  47. elif self.k > 100:
  48. raise ValueError(
  49. descr + "k {} not supported, should be less than 100 in this version")
  50. if not isinstance(self.max_iter, int):
  51. raise ValueError(
  52. descr + "max_iter not supported, should be int type".format(self.max_iter))
  53. elif self.max_iter <= 0:
  54. raise ValueError(
  55. descr + "max_iter not supported, should be larger than 0".format(self.max_iter))
  56. if not isinstance(self.tol, (float, int)):
  57. raise ValueError(
  58. descr + "tol not supported, should be float type".format(self.tol))
  59. elif self.tol < 0:
  60. raise ValueError(
  61. descr + "tol not supported, should be larger than or equal to 0".format(self.tol))
  62. if self.random_stat is not None:
  63. if not isinstance(self.random_stat, int):
  64. raise ValueError(descr + "random_stat not supported, should be int type".format(self.random_stat))
  65. elif self.random_stat < 0:
  66. raise ValueError(
  67. descr + "random_stat not supported, should be larger than/equal to 0".format(self.random_stat))