pipeline-sample-weight-class-dict-feature-engineering.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 HeteroLR
  21. from pipeline.component import SampleWeight
  22. from pipeline.component import Intersection
  23. from pipeline.component import HeteroFeatureBinning
  24. from pipeline.component import HeteroFeatureSelection
  25. from pipeline.component import FeatureScale
  26. from pipeline.component import Reader
  27. from pipeline.interface import Data, 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{namespace}"}
  38. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  39. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  40. reader_0 = Reader(name="reader_0")
  41. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  42. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  43. data_transform_0 = DataTransform(name="data_transform_0")
  44. data_transform_0.get_party_instance(
  45. role='guest',
  46. party_id=guest).component_param(
  47. with_label=True,
  48. label_name="y",
  49. label_type="int",
  50. output_format="dense")
  51. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  52. intersection_0 = Intersection(name="intersection_0")
  53. binning_param = {
  54. "name": 'hetero_feature_binning_0',
  55. "method": "quantile",
  56. "compress_thres": 10000,
  57. "head_size": 10000,
  58. "error": 0.001,
  59. "bin_num": 10,
  60. "bin_indexes": -1,
  61. "bin_names": None,
  62. "category_indexes": None,
  63. "category_names": None,
  64. "adjustment_factor": 0.5,
  65. "local_only": False,
  66. "transform_param": {
  67. "transform_cols": -1,
  68. "transform_names": None,
  69. "transform_type": "bin_num"
  70. }
  71. }
  72. selection_param = {
  73. "name": "hetero_feature_selection_0",
  74. "select_col_indexes": -1,
  75. "select_names": [],
  76. "filter_methods": [
  77. "iv_value_thres"
  78. ],
  79. "iv_value_param": {
  80. "value_threshold": 0.1
  81. }}
  82. hetero_feature_binning_0 = HeteroFeatureBinning(**binning_param)
  83. hetero_feature_selection_0 = HeteroFeatureSelection(**selection_param)
  84. sample_weight_0 = SampleWeight(name="sample_weight_0")
  85. sample_weight_0.get_party_instance(role='guest', party_id=guest).component_param(need_run=True,
  86. class_weight={"0": 1, "1": 2})
  87. sample_weight_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  88. feature_scale_0 = FeatureScale(name="feature_scale_0", method="standard_scale", need_run=True)
  89. hetero_lr_0 = HeteroLR(name="hetero_lr_0", optimizer="nesterov_momentum_sgd", tol=0.001,
  90. alpha=0.01, max_iter=20, early_stop="weight_diff", batch_size=-1,
  91. learning_rate=0.15,
  92. init_param={"init_method": "zeros"})
  93. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary", pos_label=1)
  94. # evaluation_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  95. pipeline.add_component(reader_0)
  96. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  97. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  98. pipeline.add_component(sample_weight_0, data=Data(data=intersection_0.output.data))
  99. pipeline.add_component(hetero_feature_binning_0, data=Data(data=sample_weight_0.output.data))
  100. pipeline.add_component(hetero_feature_selection_0, data=Data(data=hetero_feature_binning_0.output.data),
  101. model=Model(isometric_model=[hetero_feature_binning_0.output.model]))
  102. pipeline.add_component(feature_scale_0, data=Data(hetero_feature_selection_0.output.data))
  103. pipeline.add_component(hetero_lr_0, data=Data(train_data=feature_scale_0.output.data))
  104. pipeline.add_component(evaluation_0, data=Data(data=hetero_lr_0.output.data))
  105. pipeline.compile()
  106. pipeline.fit()
  107. if __name__ == "__main__":
  108. parser = argparse.ArgumentParser("PIPELINE DEMO")
  109. parser.add_argument("-config", type=str,
  110. help="config file")
  111. args = parser.parse_args()
  112. if args.config is not None:
  113. main(args.config)
  114. else:
  115. main()