pipeline-hetero-binning-bucket-missing-value.py 4.8 KB

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