classify_label_checker_test.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import unittest
  17. import random
  18. from fate_arch.session import computing_session as session
  19. from federatedml.feature.instance import Instance
  20. from federatedml.util.classify_label_checker import ClassifyLabelChecker, RegressionLabelChecker
  21. class TeskClassifyLabelChecker(unittest.TestCase):
  22. def setUp(self):
  23. session.init("test_label_checker")
  24. self.small_label_set = [Instance(label=i % 5) for i in range(100)]
  25. self.classify_inst = session.parallelize(self.small_label_set, include_key=False, partition=16)
  26. self.regression_label = [Instance(label=random.random()) for i in range(100)]
  27. self.regression_inst = session.parallelize(self.regression_label, partition=16, include_key=False)
  28. self.classify_checker = ClassifyLabelChecker()
  29. self.regression_checker = RegressionLabelChecker()
  30. def test_classify_label_checkert(self):
  31. num_class, classes = self.classify_checker.validate_label(self.classify_inst)
  32. self.assertTrue(num_class == 5)
  33. self.assertTrue(sorted(classes) == [0, 1, 2, 3, 4])
  34. def test_regression_label_checker(self):
  35. self.regression_checker.validate_label(self.regression_inst)
  36. def tearDown(self):
  37. session.stop()
  38. if __name__ == '__main__':
  39. unittest.main()