hetero_pearson_test.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 uuid
  18. from fate_arch.session import computing_session as session
  19. import numpy as np
  20. class TestStatistics(unittest.TestCase):
  21. def setUp(self):
  22. session.init((str(uuid.uuid1())))
  23. def test_standardized(self):
  24. from federatedml.statistic.correlation import hetero_pearson
  25. raw_data = np.random.rand(200, 100)
  26. expect = (raw_data - np.mean(raw_data, axis=0)) / np.std(raw_data, axis=0)
  27. data_table = session.parallelize([row for row in raw_data], partition=10, include_key=False)
  28. n, standardized, _, _ = hetero_pearson.standardize(data_table)
  29. standardized_data = np.array([row[1] for row in standardized.collect()])
  30. self.assertEqual(n, standardized_data.shape[0])
  31. self.assertEqual(raw_data.shape, standardized_data.shape)
  32. self.assertAlmostEqual(np.linalg.norm(standardized_data - expect), 0.0)
  33. if __name__ == '__main__':
  34. unittest.main()