hetero_kmeans_param.py 2.3 KB

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