pipeline-hetero-binning-multiclass.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.interface import Model
  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. guest_train_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  33. guest_validate_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  35. host_validate_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  36. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  37. data_transform_0, data_transform_1 = DataTransform(name="data_transform_0"), DataTransform(name='data_transform_1')
  38. reader_0, reader_1 = Reader(name="reader_0"), Reader(name='reader_1')
  39. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  40. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  41. data_transform_0.get_party_instance(
  42. role='guest', party_id=guest).component_param(
  43. with_label=True, output_format="dense")
  44. data_transform_0.get_party_instance(
  45. role='host', party_id=host).component_param(
  46. with_label=False, output_format="dense")
  47. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_validate_data)
  48. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_validate_data)
  49. data_transform_1.get_party_instance(
  50. role='guest', party_id=guest).component_param(
  51. with_label=True, output_format="dense")
  52. data_transform_1.get_party_instance(
  53. role='host', party_id=host).component_param(
  54. with_label=True, output_format="dense")
  55. intersection_0 = Intersection(name="intersection_0")
  56. intersection_1 = Intersection(name="intersection_1")
  57. param = {
  58. "method": "quantile",
  59. "optimal_binning_param": {
  60. "metric_method": "gini",
  61. "min_bin_pct": 0.05,
  62. "max_bin_pct": 0.8,
  63. "init_bucket_method": "quantile",
  64. "init_bin_nums": 100,
  65. "mixture": True
  66. },
  67. "compress_thres": 10000,
  68. "head_size": 10000,
  69. "error": 0.001,
  70. "bin_num": 10,
  71. "bin_indexes": -1,
  72. "bin_names": None,
  73. "category_names": None,
  74. "adjustment_factor": 0.5,
  75. "local_only": False,
  76. "transform_param": {
  77. "transform_cols": -1,
  78. "transform_names": None,
  79. "transform_type": "bin_num"
  80. }
  81. }
  82. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **param)
  83. hetero_feature_binning_0.get_party_instance(
  84. role="guest", party_id=guest).component_param(category_indexes=[0, 1, 2])
  85. hetero_feature_binning_1 = HeteroFeatureBinning(name='hetero_feature_binning_1')
  86. pipeline.add_component(reader_0)
  87. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  88. pipeline.add_component(reader_1)
  89. pipeline.add_component(
  90. data_transform_1, data=Data(
  91. data=reader_1.output.data), model=Model(
  92. data_transform_0.output.model))
  93. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  94. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  95. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  96. pipeline.add_component(hetero_feature_binning_1, data=Data(data=intersection_1.output.data),
  97. model=Model(hetero_feature_binning_0.output.model))
  98. pipeline.compile()
  99. pipeline.fit()
  100. # predict
  101. # deploy required components
  102. pipeline.deploy_component([data_transform_0, intersection_0, hetero_feature_binning_0])
  103. predict_pipeline = PipeLine()
  104. # add data reader onto predict pipeline
  105. predict_pipeline.add_component(reader_1)
  106. # add selected components from train pipeline onto predict pipeline
  107. # specify data source
  108. predict_pipeline.add_component(
  109. pipeline, data=Data(
  110. predict_input={
  111. pipeline.data_transform_0.input.data: reader_1.output.data}))
  112. # run predict model
  113. predict_pipeline.predict()
  114. if __name__ == "__main__":
  115. parser = argparse.ArgumentParser("PIPELINE DEMO")
  116. parser.add_argument("-config", type=str,
  117. help="config file")
  118. args = parser.parse_args()
  119. if args.config is not None:
  120. main(args.config)
  121. else:
  122. main()