bucket_binning_test.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 numpy as np
  18. from fate_arch.session import computing_session as session
  19. session.init("123")
  20. from federatedml.feature.binning.bucket_binning import BucketBinning
  21. from federatedml.feature.instance import Instance
  22. from federatedml.param.feature_binning_param import FeatureBinningParam
  23. from federatedml.feature.binning.iv_calculator import IvCalculator
  24. class TestBucketBinning(unittest.TestCase):
  25. def setUp(self):
  26. # eggroll.init("123")
  27. self.data_num = 1000
  28. self.feature_num = 200
  29. self.bin_num = 10
  30. final_result = []
  31. numpy_array = []
  32. for i in range(self.data_num):
  33. if 100 < i < 500:
  34. continue
  35. tmp = i * np.ones(self.feature_num)
  36. inst = Instance(inst_id=i, features=tmp, label=i % 2)
  37. tmp_pair = (str(i), inst)
  38. final_result.append(tmp_pair)
  39. numpy_array.append(tmp)
  40. table = session.parallelize(final_result,
  41. include_key=True,
  42. partition=10)
  43. header = ['x' + str(i) for i in range(self.feature_num)]
  44. anonymous_header = ["guest_9999_x" + str(i) for i in range(self.feature_num)]
  45. self.table = table
  46. self.table.schema = {'header': header,
  47. "anonymous_header": anonymous_header}
  48. self.numpy_table = np.array(numpy_array)
  49. self.cols = [1, 2]
  50. def test_bucket_binning(self):
  51. bin_param = FeatureBinningParam(bin_num=self.bin_num, bin_indexes=self.cols)
  52. bucket_bin = BucketBinning(bin_param)
  53. split_points = bucket_bin.fit_split_points(self.table)
  54. split_point = list(split_points.values())[0]
  55. for kth, s_p in enumerate(split_point):
  56. expect_s_p = (self.data_num - 1) / self.bin_num * (kth + 1)
  57. self.assertEqual(s_p, expect_s_p)
  58. iv_calculator = IvCalculator(0.5,
  59. "guest",
  60. 9999)
  61. iv_res = iv_calculator.cal_local_iv(self.table, split_points=split_points,
  62. bin_cols_map={"x1": 1, "x2": 2})
  63. # for col_name, iv_attr in bucket_bin.bin_results.all_cols_results.items():
  64. for col_name, iv_attr in iv_res.bin_results[0].all_cols_results.items():
  65. # print('col_name: {}, iv: {}, woe_array: {}'.format(col_name, iv_attr.iv, iv_attr.woe_array))
  66. assert abs(iv_attr.iv - 0.00364386529386804) < 1e-6
  67. def tearDown(self):
  68. # self.table.destroy()
  69. session.stop()
  70. if __name__ == '__main__':
  71. unittest.main()