pipeline-hetero-binning-missing-value.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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": "ionosphere_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  31. host_train_data = {"name": "ionosphere_scale_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", label_name="label")
  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. data_transform_1 = DataTransform(name="data_transform_1", output_format="sparse", label_name="label")
  40. data_transform_1.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  41. data_transform_1.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  42. intersection_0 = Intersection(name="intersection_0")
  43. intersection_1 = Intersection(name="intersection_1")
  44. param = {
  45. "method": "quantile",
  46. "compress_thres": 10000,
  47. "head_size": 10000,
  48. "error": 0.001,
  49. "bin_num": 10,
  50. "bin_indexes": -1,
  51. "bin_names": None,
  52. "category_indexes": None,
  53. "category_names": None,
  54. "adjustment_factor": 0.5,
  55. "local_only": False,
  56. "transform_param": {
  57. "transform_type": None
  58. }
  59. }
  60. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **param)
  61. hetero_feature_binning_0.get_party_instance(role="guest", party_id=guest).component_param(
  62. transform_param={"transform_cols": [
  63. 0,
  64. 1,
  65. 2
  66. ],
  67. "transform_names": None,
  68. "transform_type": "bin_num"}
  69. )
  70. hetero_feature_binning_1 = HeteroFeatureBinning(name="hetero_feature_binning_1", **param)
  71. hetero_feature_binning_0.get_party_instance(role="guest", party_id=guest).component_param(
  72. transform_param={"transform_cols": [
  73. 0,
  74. 1,
  75. 2
  76. ],
  77. "transform_names": None,
  78. "transform_type": "bin_num"}
  79. )
  80. pipeline.add_component(reader_0)
  81. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  82. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  83. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  84. pipeline.add_component(data_transform_1, data=Data(data=reader_0.output.data))
  85. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  86. pipeline.add_component(hetero_feature_binning_1, data=Data(data=intersection_1.output.data))
  87. pipeline.compile()
  88. pipeline.fit()
  89. pipeline.deploy_component([data_transform_0, intersection_0, hetero_feature_binning_0])
  90. predict_pipeline = PipeLine()
  91. # add data reader onto predict pipeline
  92. predict_pipeline.add_component(reader_0)
  93. # add selected components from train pipeline onto predict pipeline
  94. # specify data source
  95. predict_pipeline.add_component(
  96. pipeline, data=Data(
  97. predict_input={
  98. pipeline.data_transform_0.input.data: reader_0.output.data}))
  99. # run predict model
  100. predict_pipeline.predict()
  101. if __name__ == "__main__":
  102. parser = argparse.ArgumentParser("PIPELINE DEMO")
  103. parser.add_argument("-config", type=str,
  104. help="config file")
  105. args = parser.parse_args()
  106. if args.config is not None:
  107. main(args.config)
  108. else:
  109. main()