local_baseline_test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 numpy as np
  17. import unittest
  18. import uuid
  19. from fate_arch.common import profile
  20. from fate_arch.session import computing_session as session
  21. from federatedml.local_baseline.local_baseline import LocalBaseline
  22. from federatedml.param.local_baseline_param import LocalBaselineParam
  23. from federatedml.feature.instance import Instance
  24. from sklearn.linear_model import LogisticRegression
  25. profile._PROFILE_LOG_ENABLED = False
  26. class TestLocalBaseline(unittest.TestCase):
  27. def setUp(self):
  28. self.job_id = str(uuid.uuid1())
  29. session.init("test_random_sampler_" + self.job_id)
  30. data_num = 100
  31. feature_num = 8
  32. self.prepare_data(data_num, feature_num)
  33. params = LocalBaselineParam()
  34. local_baseline_obj = LocalBaseline()
  35. local_baseline_obj._init_model(params)
  36. local_baseline_obj.need_run = True
  37. local_baseline_obj.header = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"]
  38. local_baseline_obj.model_name = "LogisticRegression"
  39. local_baseline_obj.model_opts = {}
  40. self.local_baseline_obj = local_baseline_obj
  41. def prepare_data(self, data_num, feature_num):
  42. self.X = np.random.randint(0, 10, (data_num, feature_num))
  43. self.y = np.random.randint(0, 2, data_num)
  44. final_result = []
  45. for i in range(data_num):
  46. tmp = self.X[i, :]
  47. inst = Instance(inst_id=i, features=tmp, label=self.y[i])
  48. final_result.append((i, inst))
  49. table = session.parallelize(final_result,
  50. include_key=True,
  51. partition=3)
  52. self.table = table
  53. def test_predict(self):
  54. glm = LogisticRegression().fit(self.X, self.y)
  55. real_predict_result = glm.predict(self.X)
  56. real_predict_result = dict(zip(range(self.X.shape[0]), real_predict_result))
  57. self.local_baseline_obj.model_fit = glm
  58. model_predict_result = self.local_baseline_obj.predict(self.table)
  59. model_predict_result = {v[0]: v[1].features[1] for v in model_predict_result.collect()}
  60. self.assertDictEqual(model_predict_result, real_predict_result)
  61. def tearDown(self):
  62. session.stop()
  63. if __name__ == '__main__':
  64. unittest.main()