pipeline-hetero-feature-selection-pearson-predict.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 HeteroFeatureSelection
  21. from pipeline.component import HeteroPearson
  22. from pipeline.component import Intersection
  23. from pipeline.component import Reader
  24. from pipeline.interface import Data
  25. from pipeline.interface import Model
  26. from pipeline.utils.tools import load_job_config
  27. def main(config="../../config.yaml", namespace=""):
  28. # obtain config
  29. if isinstance(config, str):
  30. config = load_job_config(config)
  31. parties = config.parties
  32. guest = parties.guest[0]
  33. host = parties.host[0]
  34. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  36. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  37. reader_0 = Reader(name="reader_0")
  38. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  39. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  40. data_transform_0 = DataTransform(name="data_transform_0")
  41. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  42. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  43. intersection_0 = Intersection(name="intersection_0")
  44. hetero_pearson_0 = HeteroPearson(name='hetero_pearson_0', column_indexes=-1)
  45. hetero_binning_0 = HeteroFeatureBinning(name='hetero_binning_0')
  46. selection_param = {
  47. "name": "hetero_feature_selection_0",
  48. "select_col_indexes": -1,
  49. "select_names": [],
  50. "filter_methods": [
  51. "vif_filter",
  52. "correlation_filter"
  53. ],
  54. "vif_param": {
  55. "threshold": 5
  56. },
  57. "correlation_param": {
  58. "threshold": 0.5,
  59. "select_federated": False
  60. }
  61. }
  62. hetero_feature_selection_0 = HeteroFeatureSelection(**selection_param)
  63. pipeline.add_component(reader_0)
  64. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  65. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  66. pipeline.add_component(hetero_binning_0, data=Data(data=intersection_0.output.data))
  67. pipeline.add_component(hetero_pearson_0, data=Data(data=intersection_0.output.data))
  68. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  69. model=Model(isometric_model=[hetero_pearson_0.output.model,
  70. hetero_binning_0.output.model]))
  71. pipeline.compile()
  72. pipeline.fit()
  73. # predict
  74. # deploy required components
  75. pipeline.deploy_component([data_transform_0, intersection_0,
  76. hetero_binning_0, hetero_feature_selection_0])
  77. predict_pipeline = PipeLine()
  78. # add data reader onto predict pipeline
  79. predict_pipeline.add_component(reader_0)
  80. # add selected components from train pipeline onto predict pipeline
  81. # specify data source
  82. predict_pipeline.add_component(
  83. pipeline, data=Data(
  84. predict_input={
  85. pipeline.data_transform_0.input.data: reader_0.output.data}))
  86. # run predict model
  87. predict_pipeline.predict()
  88. if __name__ == "__main__":
  89. parser = argparse.ArgumentParser("PIPELINE DEMO")
  90. parser.add_argument("-config", type=str,
  91. help="config file")
  92. args = parser.parse_args()
  93. if args.config is not None:
  94. main(args.config)
  95. else:
  96. main()