pipeline-hetero-feature-selection-fast-sbt.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 HeteroFastSecureBoost
  20. from pipeline.component import HeteroFeatureSelection
  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. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  35. # initialize pipeline
  36. pipeline = PipeLine()
  37. # set job initiator
  38. pipeline.set_initiator(role='guest', party_id=guest)
  39. # set participants information
  40. pipeline.set_roles(guest=guest, host=host)
  41. # define Reader components to read in data
  42. reader_0 = Reader(name="reader_0")
  43. # configure Reader for guest
  44. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  45. # configure Reader for host
  46. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  47. # define DataTransform components
  48. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  49. # get DataTransform party instance of guest
  50. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  51. # configure DataTransform for guest
  52. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  53. # get and configure DataTransform party instance of host
  54. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  55. # define Intersection components
  56. intersection_0 = Intersection(name="intersection_0")
  57. pipeline.add_component(reader_0)
  58. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  59. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  60. fast_sbt_param = {
  61. "task_type": "classification",
  62. "learning_rate": 0.1,
  63. "num_trees": 4,
  64. "subsample_feature_rate": 1,
  65. "n_iter_no_change": False,
  66. "work_mode": "layered",
  67. "guest_depth": 2,
  68. "host_depth": 3,
  69. "tol": 0.0001,
  70. "bin_num": 50,
  71. "metrics": ["Recall", "ks", "auc", "roc"],
  72. "objective_param": {
  73. "objective": "cross_entropy"
  74. },
  75. "encrypt_param": {
  76. "method": "paillier"
  77. },
  78. "predict_param": {
  79. "threshold": 0.5
  80. },
  81. "validation_freqs": 1
  82. }
  83. fast_sbt_0 = HeteroFastSecureBoost(name="fast_secureboost_0", **fast_sbt_param)
  84. pipeline.add_component(fast_sbt_0, data=Data(train_data=intersection_0.output.data))
  85. selection_param = {
  86. "select_col_indexes": -1,
  87. "select_names": [],
  88. "filter_methods": [
  89. "hetero_fast_sbt_filter"
  90. ],
  91. "sbt_param": {
  92. "metrics": "feature_importance",
  93. "filter_type": "threshold",
  94. "take_high": True,
  95. "threshold": 0.03
  96. }}
  97. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0", **selection_param)
  98. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  99. model=Model(isometric_model=fast_sbt_0.output.model))
  100. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  101. pipeline.compile()
  102. pipeline.fit()
  103. if __name__ == "__main__":
  104. parser = argparse.ArgumentParser("PIPELINE DEMO")
  105. parser.add_argument("-config", type=str,
  106. help="config file")
  107. args = parser.parse_args()
  108. if args.config is not None:
  109. main(args.config)
  110. else:
  111. main()