pipeline-kmeans-with-feature-engineering.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 HeteroKmeans
  20. from pipeline.component import Intersection
  21. from pipeline.component import HeteroFeatureBinning
  22. from pipeline.component import HeteroFeatureSelection
  23. from pipeline.component import Evaluation
  24. from pipeline.component import Reader
  25. from pipeline.interface import Data
  26. from pipeline.interface import Model
  27. from pipeline.utils.tools import load_job_config
  28. def main(config="../../config.yaml", namespace=""):
  29. # obtain config
  30. if isinstance(config, str):
  31. config = load_job_config(config)
  32. parties = config.parties
  33. guest = parties.guest[0]
  34. host = parties.host[0]
  35. arbiter = parties.arbiter[0]
  36. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  37. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  38. # initialize pipeline
  39. pipeline = PipeLine()
  40. # set job initiator
  41. pipeline.set_initiator(role='guest', party_id=guest)
  42. # set participants information
  43. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  44. # define Reader components to read in data
  45. reader_0 = Reader(name="reader_0")
  46. # configure Reader for guest
  47. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  48. # configure Reader for host
  49. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  50. # define DataTransform components
  51. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  52. # get DataTransform party instance of guest
  53. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  54. # configure DataTransform for guest
  55. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  56. # get and configure DataTransform party instance of host
  57. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  58. # define Intersection components
  59. intersection_0 = Intersection(name="intersection_0")
  60. param = {
  61. "name": 'hetero_feature_binning_0',
  62. "method": 'optimal',
  63. "optimal_binning_param": {
  64. "metric_method": "iv"
  65. },
  66. "bin_indexes": -1
  67. }
  68. hetero_feature_binning_0 = HeteroFeatureBinning(**param)
  69. param = {
  70. "name": 'hetero_feature_selection_0',
  71. "filter_methods": ["manually", "iv_filter"],
  72. "manually_param": {
  73. "filter_out_indexes": [1]
  74. },
  75. "iv_param": {
  76. "metrics": ["iv", "iv"],
  77. "filter_type": ["top_k", "threshold"],
  78. "take_high": [True, True],
  79. "threshold": [10, 0.001]
  80. },
  81. "select_col_indexes": -1
  82. }
  83. hetero_feature_selection_0 = HeteroFeatureSelection(**param)
  84. param = {
  85. "k": 3,
  86. "max_iter": 10
  87. }
  88. hetero_kmeans_0 = HeteroKmeans(name='hetero_kmeans_0', **param)
  89. evaluation_0 = Evaluation(name='evaluation_0', eval_type='clustering')
  90. # add components to pipeline, in order of task execution
  91. pipeline.add_component(reader_0)
  92. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  93. # set data input sources of intersection components
  94. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  95. # set train & validate data of hetero_lr_0 component
  96. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  97. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  98. model=Model(isometric_model=hetero_feature_binning_0.output.model))
  99. pipeline.add_component(hetero_kmeans_0, data=Data(train_data=hetero_feature_selection_0.output.data))
  100. print(f"data: {hetero_kmeans_0.output.data.data[0]}")
  101. pipeline.add_component(evaluation_0, data=Data(data=hetero_kmeans_0.output.data.data[0]))
  102. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  103. pipeline.compile()
  104. # fit model
  105. pipeline.fit()
  106. # query component summary
  107. print(pipeline.get_component("hetero_kmeans_0").get_summary())
  108. if __name__ == "__main__":
  109. parser = argparse.ArgumentParser("PIPELINE DEMO")
  110. parser.add_argument("-config", type=str,
  111. help="config file")
  112. args = parser.parse_args()
  113. if args.config is not None:
  114. main(args.config)
  115. else:
  116. main()