pipeline-homo-recursive-binning.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import json
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import DataTransform
  20. from pipeline.component.homo_feature_binning import HomoFeatureBinning
  21. from pipeline.component.reader import Reader
  22. from pipeline.component.scale import FeatureScale
  23. from pipeline.interface.data import Data
  24. from pipeline.utils.tools import load_job_config
  25. def main(config="../../config.yaml", namespace=""):
  26. # obtain config
  27. if isinstance(config, str):
  28. config = load_job_config(config)
  29. parties = config.parties
  30. guest = parties.guest[0]
  31. host = parties.host[0]
  32. arbiter = parties.arbiter[0]
  33. guest_train_data = {"name": "breast_homo_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  35. # initialize pipeline
  36. pipeline = PipeLine()
  37. # set job initiator
  38. pipeline.set_initiator(role='guest', party_id=guest)
  39. # set participants information
  40. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  41. # define Reader components to read in data
  42. reader_0 = Reader(name="reader_0")
  43. # configure Reader for guest
  44. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  45. # configure Reader for host
  46. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  47. # define DataTransform components
  48. data_transform_0 = DataTransform(
  49. name="data_transform_0",
  50. with_label=True,
  51. output_format="dense") # start component numbering at 0
  52. homo_binning_0 = HomoFeatureBinning(name='homo_binning_0', sample_bins=1000, method="recursive_query")
  53. # add components to pipeline, in order of task execution
  54. pipeline.add_component(reader_0)
  55. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  56. # set data input sources of intersection components
  57. pipeline.add_component(homo_binning_0, data=Data(data=data_transform_0.output.data))
  58. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  59. pipeline.compile()
  60. # fit model
  61. pipeline.fit()
  62. # query component summary
  63. # print(json.dumps(pipeline.get_component("homo_binning_0").get_summary(), indent=4, ensure_ascii=False))
  64. if __name__ == "__main__":
  65. parser = argparse.ArgumentParser("PIPELINE DEMO")
  66. parser.add_argument("-config", type=str,
  67. help="config file")
  68. args = parser.parse_args()
  69. if args.config is not None:
  70. main(args.config)
  71. else:
  72. main()