pipeline-hetero-binning-selection-model-loader.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 HeteroFeatureSelection
  20. from pipeline.component import ModelLoader
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  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": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  33. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  34. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  35. reader_0 = Reader(name="reader_0")
  36. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  37. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  38. data_transform_0 = DataTransform(name="data_transform_0")
  39. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  40. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  41. intersection_0 = Intersection(name="intersection_0")
  42. param = {
  43. "model_id": "guest-10000#host-9999#model",
  44. "model_version": "202108301602196678300",
  45. "component_name": "hetero_feature_binning_0",
  46. "step_index": None
  47. }
  48. model_loader_0 = ModelLoader(name="model_loader_0", **param)
  49. selection_param = {
  50. "name": "hetero_feature_selection_0",
  51. "select_col_indexes": -1,
  52. "select_names": [],
  53. "filter_methods": [
  54. "iv_filter"
  55. ],
  56. "iv_param": {
  57. "metrics": ["iv", "iv", "iv"],
  58. "filter_type": ["threshold", "top_k", "top_percentile"],
  59. "take_high": True,
  60. "threshold": [0.03, 15, 0.7],
  61. "host_thresholds": [[0.15], None, None],
  62. "select_federated": True
  63. }
  64. }
  65. hetero_feature_selection_0 = HeteroFeatureSelection(**selection_param)
  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(model_loader_0)
  70. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  71. model=Model(isometric_model=model_loader_0.output.model))
  72. pipeline.compile()
  73. pipeline.fit()
  74. if __name__ == "__main__":
  75. parser = argparse.ArgumentParser("PIPELINE DEMO")
  76. parser.add_argument("-config", type=str,
  77. help="config file")
  78. args = parser.parse_args()
  79. if args.config is not None:
  80. main(args.config)
  81. else:
  82. main()