pipeline-hetero-binning-woe-binning.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 argparse
  17. from pipeline.backend.pipeline import PipeLine
  18. from pipeline.component import DataTransform
  19. from pipeline.component import HeteroFeatureBinning
  20. from pipeline.component import Intersection
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.utils.tools import load_job_config
  24. def main(config="../../config.yaml", namespace=""):
  25. if isinstance(config, str):
  26. config = load_job_config(config)
  27. parties = config.parties
  28. guest = parties.guest[0]
  29. host = parties.host[0]
  30. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  31. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  32. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  33. reader_0 = Reader(name="reader_0")
  34. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  35. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  36. data_transform_0 = DataTransform(name="data_transform_0")
  37. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  38. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  39. intersection_0 = Intersection(name="intersection_0")
  40. param = {
  41. "method": "quantile",
  42. "compress_thres": 10000,
  43. "head_size": 10000,
  44. "error": 0.001,
  45. "bin_num": 10,
  46. "bin_indexes": -1,
  47. "bin_names": None,
  48. "category_indexes": None,
  49. "category_names": None,
  50. "adjustment_factor": 0.5,
  51. "local_only": False,
  52. "transform_param": {
  53. "transform_type": None
  54. }
  55. }
  56. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **param)
  57. hetero_feature_binning_0.get_party_instance(role="guest",
  58. party_id=guest). \
  59. component_param(transform_param={"transform_cols": [
  60. 0,
  61. 1,
  62. 2
  63. ],
  64. "transform_names": None,
  65. "transform_type": "woe"})
  66. pipeline.add_component(reader_0)
  67. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  68. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  69. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  70. pipeline.compile()
  71. pipeline.fit()
  72. pipeline.deploy_component([data_transform_0, intersection_0, hetero_feature_binning_0])
  73. predict_pipeline = PipeLine()
  74. # add data reader onto predict pipeline
  75. predict_pipeline.add_component(reader_0)
  76. # add selected components from train pipeline onto predict pipeline
  77. # specify data source
  78. predict_pipeline.add_component(
  79. pipeline, data=Data(
  80. predict_input={
  81. pipeline.data_transform_0.input.data: reader_0.output.data}))
  82. # run predict model
  83. predict_pipeline.predict()
  84. if __name__ == "__main__":
  85. parser = argparse.ArgumentParser("PIPELINE DEMO")
  86. parser.add_argument("-config", type=str,
  87. help="config file")
  88. args = parser.parse_args()
  89. if args.config is not None:
  90. main(args.config)
  91. else:
  92. main()