pipeline-hetero-lr-feature-engineering.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 Evaluation
  20. from pipeline.component import FeatureScale
  21. from pipeline.component import HeteroFeatureBinning
  22. from pipeline.component import HeteroFeatureSelection
  23. from pipeline.component import HeteroLR
  24. from pipeline.component import Intersection
  25. from pipeline.component import OneHotEncoder
  26. from pipeline.component import Reader
  27. from pipeline.interface import Data
  28. from pipeline.interface import Model
  29. from pipeline.utils.tools import load_job_config
  30. def main(config="../../config.yaml", namespace=""):
  31. # obtain config
  32. if isinstance(config, str):
  33. config = load_job_config(config)
  34. parties = config.parties
  35. guest = parties.guest[0]
  36. host = parties.host[0]
  37. arbiter = parties.arbiter[0]
  38. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  39. host_train_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=host, arbiter=arbiter)
  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=host).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=host).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. feature_scale_0 = FeatureScale(name='feature_scale_0', method="standard_scale",
  66. need_run=True)
  67. pipeline.add_component(feature_scale_0, data=Data(data=intersection_0.output.data))
  68. binning_param = {
  69. "method": "quantile",
  70. "compress_thres": 10000,
  71. "head_size": 10000,
  72. "error": 0.001,
  73. "bin_num": 10,
  74. "bin_indexes": -1,
  75. "adjustment_factor": 0.5,
  76. "local_only": False,
  77. "need_run": True,
  78. "transform_param": {
  79. "transform_cols": -1,
  80. "transform_type": "bin_num"
  81. }
  82. }
  83. hetero_feature_binning_0 = HeteroFeatureBinning(name='hetero_feature_binning_0',
  84. **binning_param)
  85. pipeline.add_component(hetero_feature_binning_0, data=Data(data=feature_scale_0.output.data))
  86. selection_param = {
  87. "select_col_indexes": -1,
  88. "filter_methods": [
  89. "manually",
  90. "iv_value_thres",
  91. "iv_percentile"
  92. ],
  93. "manually_param": {
  94. "filter_out_indexes": None
  95. },
  96. "iv_value_param": {
  97. "value_threshold": 1.0
  98. },
  99. "iv_percentile_param": {
  100. "percentile_threshold": 0.9
  101. },
  102. "need_run": True
  103. }
  104. hetero_feature_selection_0 = HeteroFeatureSelection(name='hetero_feature_selection_0',
  105. **selection_param)
  106. pipeline.add_component(hetero_feature_selection_0, data=Data(data=hetero_feature_binning_0.output.data),
  107. model=Model(isometric_model=[hetero_feature_binning_0.output.model]))
  108. onehot_param = {
  109. "transform_col_indexes": -1,
  110. "transform_col_names": None,
  111. "need_run": True
  112. }
  113. one_hot_encoder_0 = OneHotEncoder(name='one_hot_encoder_0', **onehot_param)
  114. pipeline.add_component(one_hot_encoder_0, data=Data(data=hetero_feature_selection_0.output.data))
  115. lr_param = {
  116. "penalty": "L2",
  117. "optimizer": "rmsprop",
  118. "tol": 1e-05,
  119. "alpha": 0.01,
  120. "max_iter": 10,
  121. "early_stop": "diff",
  122. "batch_size": -1,
  123. "learning_rate": 0.15,
  124. "init_param": {
  125. "init_method": "random_uniform"
  126. },
  127. "cv_param": {
  128. "n_splits": 5,
  129. "shuffle": False,
  130. "random_seed": 103,
  131. "need_cv": False
  132. }
  133. }
  134. hetero_lr_0 = HeteroLR(name="hetero_lr_0", **lr_param)
  135. pipeline.add_component(hetero_lr_0, data=Data(train_data=one_hot_encoder_0.output.data))
  136. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  137. pipeline.add_component(evaluation_0, data=Data(data=hetero_lr_0.output.data))
  138. pipeline.compile()
  139. pipeline.fit()
  140. if __name__ == "__main__":
  141. parser = argparse.ArgumentParser("PIPELINE DEMO")
  142. parser.add_argument("-config", type=str,
  143. help="config file")
  144. args = parser.parse_args()
  145. if args.config is not None:
  146. main(args.config)
  147. else:
  148. main()