pipeline-hetero-feature-selection-single-predict.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 FeatureScale
  20. from pipeline.component import FederatedSample
  21. from pipeline.component import HeteroFeatureBinning
  22. from pipeline.component import HeteroFeatureSelection
  23. from pipeline.component import Intersection
  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. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  36. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  37. guest_eval_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  38. host_eval_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  39. # initialize pipeline
  40. pipeline = PipeLine()
  41. # set job initiator
  42. pipeline.set_initiator(role="guest", party_id=guest)
  43. # set participants information
  44. pipeline.set_roles(guest=guest, host=host)
  45. # define Reader components to read in data
  46. reader_0 = Reader(name="reader_0")
  47. # configure Reader for guest
  48. reader_0.get_party_instance(role="guest", party_id=guest).component_param(table=guest_train_data)
  49. # configure Reader for host
  50. reader_0.get_party_instance(role="host", party_id=host).component_param(table=host_train_data)
  51. # define DataTransform components
  52. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  53. # get DataTransform party instance of guest
  54. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role="guest", party_id=guest)
  55. # configure DataTransform for guest
  56. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  57. # get and configure DataTransform party instance of host
  58. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  59. # define Intersection components
  60. intersection_0 = Intersection(name="intersection_0")
  61. pipeline.add_component(reader_0)
  62. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  63. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  64. reader_1 = Reader(name="reader_1")
  65. reader_1.get_party_instance(role="guest", party_id=guest).component_param(table=guest_eval_data)
  66. reader_1.get_party_instance(role="host", party_id=host).component_param(table=host_eval_data)
  67. data_transform_1 = DataTransform(name="data_transform_1")
  68. intersection_1 = Intersection(name="intersection_1")
  69. pipeline.add_component(reader_1)
  70. pipeline.add_component(
  71. data_transform_1, data=Data(
  72. data=reader_1.output.data), model=Model(
  73. data_transform_0.output.model))
  74. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  75. sample_0 = FederatedSample(name="sample_0", fractions=0.9)
  76. pipeline.add_component(sample_0, data=Data(data=intersection_0.output.data))
  77. binning_param = {
  78. "method": "quantile",
  79. "compress_thres": 10000,
  80. "head_size": 10000,
  81. "error": 0.001,
  82. "bin_num": 10,
  83. "bin_indexes": -1,
  84. "bin_names": None,
  85. "category_indexes": None,
  86. "category_names": None,
  87. "adjustment_factor": 0.5,
  88. "local_only": False,
  89. "transform_param": {
  90. "transform_cols": -1,
  91. "transform_names": None,
  92. "transform_type": "bin_num"
  93. }
  94. }
  95. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **binning_param)
  96. pipeline.add_component(hetero_feature_binning_0, data=Data(data=sample_0.output.data))
  97. hetero_feature_binning_1 = HeteroFeatureBinning(name="hetero_feature_binning_1")
  98. pipeline.add_component(hetero_feature_binning_1,
  99. data=Data(data=intersection_1.output.data),
  100. model=Model(hetero_feature_binning_0.output.model))
  101. selection_param = {
  102. "select_col_indexes": -1,
  103. "select_names": [],
  104. "filter_methods": [
  105. "manually",
  106. "iv_value_thres",
  107. "iv_percentile"
  108. ],
  109. "manually_param": {
  110. "filter_out_indexes": [],
  111. "filter_out_names": []
  112. },
  113. "unique_param": {
  114. "eps": 1e-06
  115. },
  116. "iv_value_param": {
  117. "value_threshold": 0.1
  118. },
  119. "iv_percentile_param": {
  120. "percentile_threshold": 0.9
  121. },
  122. "variance_coe_param": {
  123. "value_threshold": 0.3
  124. },
  125. "outlier_param": {
  126. "percentile": 0.95,
  127. "upper_threshold": 2.0
  128. }
  129. }
  130. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0", **selection_param)
  131. pipeline.add_component(hetero_feature_selection_0,
  132. data=Data(data=hetero_feature_binning_0.output.data),
  133. model=Model(isometric_model=[hetero_feature_binning_0.output.model]))
  134. hetero_feature_selection_1 = HeteroFeatureSelection(name="hetero_feature_selection_1")
  135. pipeline.add_component(hetero_feature_selection_1,
  136. data=Data(data=hetero_feature_binning_1.output.data),
  137. model=Model(hetero_feature_selection_0.output.model))
  138. scale_0 = FeatureScale(name="scale_0")
  139. scale_1 = FeatureScale(name="scale_1")
  140. pipeline.add_component(scale_0, data=Data(data=hetero_feature_selection_0.output.data))
  141. pipeline.add_component(scale_1, data=Data(data=hetero_feature_selection_1.output.data),
  142. model=Model(scale_0.output.model))
  143. pipeline.compile()
  144. pipeline.fit()
  145. if __name__ == "__main__":
  146. parser = argparse.ArgumentParser("PIPELINE DEMO")
  147. parser.add_argument("-config", type=str,
  148. help="config file")
  149. args = parser.parse_args()
  150. if args.config is not None:
  151. main(args.config)
  152. else:
  153. main()