pipeline-hetero-feature-selection-multi-iso.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 DataStatistics
  19. from pipeline.component import DataTransform
  20. from pipeline.component import HeteroFeatureBinning
  21. from pipeline.component import HeteroFeatureSelection
  22. from pipeline.component import HeteroSecureBoost
  23. from pipeline.component import Intersection
  24. from pipeline.component import PSI
  25. from pipeline.component import Reader
  26. from pipeline.interface import Data
  27. from pipeline.interface import Model
  28. from pipeline.utils.tools import load_job_config
  29. def main(config="../../config.yaml", namespace=""):
  30. # obtain config
  31. if isinstance(config, str):
  32. config = load_job_config(config)
  33. parties = config.parties
  34. guest = parties.guest[0]
  35. hosts = parties.host
  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. guest_eval_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  39. host_eval_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  40. # initialize pipeline
  41. pipeline = PipeLine()
  42. # set job initiator
  43. pipeline.set_initiator(role='guest', party_id=guest)
  44. # set participants information
  45. pipeline.set_roles(guest=guest, host=hosts)
  46. # define Reader components to read in data
  47. reader_0 = Reader(name="reader_0")
  48. # configure Reader for guest
  49. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  50. # configure Reader for host
  51. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  52. # define DataTransform components
  53. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  54. # get DataTransform party instance of guest
  55. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  56. # configure DataTransform for guest
  57. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  58. # get and configure DataTransform party instance of host
  59. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  60. # define Intersection components
  61. intersection_0 = Intersection(name="intersection_0")
  62. pipeline.add_component(reader_0)
  63. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  64. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  65. binning_param = {
  66. "method": "quantile",
  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_indexes": None,
  74. "category_names": None,
  75. "adjustment_factor": 0.5,
  76. "local_only": False,
  77. "transform_param": {
  78. "transform_cols": -1,
  79. "transform_names": None,
  80. "transform_type": "bin_num"
  81. }
  82. }
  83. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **binning_param)
  84. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  85. statistic_param = {
  86. "statistics": ["95%", "coefficient_of_variance", "stddev"],
  87. "column_indexes": -1,
  88. "column_names": []
  89. }
  90. statistic_0 = DataStatistics(name="statistic_0", **statistic_param)
  91. pipeline.add_component(statistic_0, data=Data(data=intersection_0.output.data))
  92. reader_1 = Reader(name="reader_1")
  93. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  94. reader_1.get_party_instance(role='host', party_id=hosts).component_param(table=host_eval_data)
  95. data_transform_1 = DataTransform(name="data_transform_1")
  96. intersection_1 = Intersection(name="intersection_1")
  97. pipeline.add_component(reader_1)
  98. pipeline.add_component(
  99. data_transform_1, data=Data(
  100. data=reader_1.output.data), model=Model(
  101. data_transform_0.output.model))
  102. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  103. psi_param = {
  104. "name": "psi_0",
  105. "max_bin_num": 20
  106. }
  107. psi_0 = PSI(**psi_param)
  108. pipeline.add_component(psi_0, data=Data(train_data=intersection_0.output.data,
  109. validate_data=intersection_1.output.data))
  110. secureboost_param = {
  111. "task_type": "classification",
  112. "learning_rate": 0.1,
  113. "num_trees": 5,
  114. "subsample_feature_rate": 1,
  115. "n_iter_no_change": False,
  116. "tol": 0.0001,
  117. "bin_num": 50,
  118. "objective_param": {
  119. "objective": "cross_entropy"
  120. },
  121. "encrypt_param": {
  122. "method": "paillier"
  123. },
  124. "predict_param": {
  125. "threshold": 0.5
  126. }
  127. }
  128. secureboost_0 = HeteroSecureBoost(name="secureboost_0", **secureboost_param)
  129. pipeline.add_component(secureboost_0, data=Data(train_data=intersection_0.output.data))
  130. selection_param = {
  131. "select_col_indexes": -1,
  132. "select_names": [],
  133. "filter_methods": [
  134. "iv_filter",
  135. "statistic_filter",
  136. "psi_filter",
  137. "hetero_sbt_filter"
  138. ],
  139. "iv_param": {
  140. "metrics": ["iv", "iv", "iv"],
  141. "filter_type": ["threshold", "top_k", "top_percentile"],
  142. "take_high": True,
  143. "threshold": [0.03, 15, 0.7],
  144. "host_thresholds": [[0.15], None, None],
  145. "select_federated": True
  146. },
  147. "statistic_param": {
  148. "metrics": ["skewness", "skewness", "kurtosis", "median"],
  149. "filter_type": "threshold",
  150. "take_high": [True, False, False, True],
  151. "threshold": [-10, 10, 2, -1.5]
  152. },
  153. "psi_param": {
  154. "metrics": "psi",
  155. "filter_type": "threshold",
  156. "take_high": False,
  157. "threshold": -0.1
  158. },
  159. "sbt_param": {
  160. "metrics": "feature_importance",
  161. "filter_type": "threshold",
  162. "take_high": True,
  163. "threshold": 0.03
  164. }}
  165. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0", **selection_param)
  166. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  167. model=Model(isometric_model=[hetero_feature_binning_0.output.model,
  168. statistic_0.output.model,
  169. psi_0.output.model,
  170. secureboost_0.output.model]))
  171. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  172. pipeline.compile()
  173. pipeline.fit()
  174. if __name__ == "__main__":
  175. parser = argparse.ArgumentParser("PIPELINE DEMO")
  176. parser.add_argument("-config", type=str,
  177. help="config file")
  178. args = parser.parse_args()
  179. if args.config is not None:
  180. main(args.config)
  181. else:
  182. main()