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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, DataStatistics, HeteroPearson
  20. from pipeline.component import HeteroLR, OneHotEncoder
  21. from pipeline.component import HeteroFeatureBinning
  22. from pipeline.component import HeteroFeatureSelection
  23. from pipeline.component import FeatureScale
  24. from pipeline.component import Intersection
  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. host = parties.host[0]
  36. arbiter = parties.arbiter[0]
  37. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment_sid{namespace}"}
  38. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment_sid{namespace}"}
  39. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).\
  40. set_roles(guest=guest, host=host, arbiter=arbiter)
  41. reader_0 = Reader(name="reader_0")
  42. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  43. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  44. data_transform_0 = DataTransform(name="data_transform_0", with_match_id=True)
  45. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  46. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  47. intersection_0 = Intersection(name="intersection_0")
  48. feature_scale_0 = FeatureScale(name='feature_scale_0', method="standard_scale",
  49. need_run=True)
  50. binning_param = {
  51. "method": "quantile",
  52. "compress_thres": 10000,
  53. "head_size": 10000,
  54. "error": 0.001,
  55. "bin_num": 10,
  56. "bin_indexes": -1,
  57. "adjustment_factor": 0.5,
  58. "local_only": False,
  59. "need_run": True,
  60. "transform_param": {
  61. "transform_cols": -1,
  62. "transform_type": "bin_num"
  63. }
  64. }
  65. hetero_feature_binning_0 = HeteroFeatureBinning(name='hetero_feature_binning_0',
  66. **binning_param)
  67. statistic_0 = DataStatistics(name='statistic_0', statistics=["95%"])
  68. pearson_0 = HeteroPearson(name='pearson_0', column_indexes=-1)
  69. onehot_0 = OneHotEncoder(name='onehot_0')
  70. selection_param = {
  71. "name": "hetero_feature_selection_0",
  72. "select_col_indexes": -1,
  73. "select_names": [],
  74. "filter_methods": [
  75. "manually",
  76. "unique_value",
  77. "iv_filter",
  78. "coefficient_of_variation_value_thres",
  79. "outlier_cols"
  80. ],
  81. "manually_param": {
  82. "filter_out_indexes": [
  83. 0,
  84. 1,
  85. 2
  86. ],
  87. "filter_out_names": [
  88. "x3"
  89. ]
  90. },
  91. "unique_param": {
  92. "eps": 1e-06
  93. },
  94. "iv_param": {
  95. "metrics": ["iv", "iv", "iv"],
  96. "filter_type": ["threshold", "top_k", "top_percentile"],
  97. "threshold": [0.001, 100, 0.99]
  98. },
  99. "variance_coe_param": {
  100. "value_threshold": 0.3
  101. },
  102. "outlier_param": {
  103. "percentile": 0.95,
  104. "upper_threshold": 2.0
  105. }}
  106. hetero_feature_selection_0 = HeteroFeatureSelection(**selection_param)
  107. lr_param = {
  108. "name": "hetero_lr_0",
  109. "penalty": "L2",
  110. "optimizer": "rmsprop",
  111. "tol": 0.0001,
  112. "alpha": 0.01,
  113. "max_iter": 30,
  114. "early_stop": "diff",
  115. "batch_size": 320,
  116. "learning_rate": 0.15,
  117. "init_param": {
  118. "init_method": "zeros"
  119. },
  120. "sqn_param": {
  121. "update_interval_L": 3,
  122. "memory_M": 5,
  123. "sample_size": 5000,
  124. "random_seed": None
  125. },
  126. "cv_param": {
  127. "n_splits": 5,
  128. "shuffle": False,
  129. "random_seed": 103,
  130. "need_cv": False
  131. }
  132. }
  133. hetero_lr_0 = HeteroLR(**lr_param)
  134. evaluation_0 = Evaluation(name='evaluation_0')
  135. pipeline.add_component(reader_0)
  136. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  137. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  138. pipeline.add_component(feature_scale_0, data=Data(data=intersection_0.output.data))
  139. pipeline.add_component(hetero_feature_binning_0, data=Data(data=feature_scale_0.output.data))
  140. pipeline.add_component(statistic_0, data=Data(data=feature_scale_0.output.data))
  141. pipeline.add_component(pearson_0, data=Data(data=feature_scale_0.output.data))
  142. pipeline.add_component(hetero_feature_selection_0, data=Data(data=hetero_feature_binning_0.output.data),
  143. model=Model(isometric_model=[hetero_feature_binning_0.output.model,
  144. statistic_0.output.model]))
  145. pipeline.add_component(onehot_0, data=Data(data=hetero_feature_selection_0.output.data))
  146. pipeline.add_component(hetero_lr_0, data=Data(train_data=onehot_0.output.data))
  147. pipeline.add_component(evaluation_0, data=Data(data=hetero_lr_0.output.data))
  148. pipeline.compile()
  149. pipeline.fit()
  150. if __name__ == "__main__":
  151. parser = argparse.ArgumentParser("PIPELINE DEMO")
  152. parser.add_argument("-config", type=str,
  153. help="config file")
  154. args = parser.parse_args()
  155. if args.config is not None:
  156. main(args.config)
  157. else:
  158. main()