pipeline-hetero-feature-selection-multiclass.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 HeteroFeatureBinning
  20. from pipeline.component import HeteroFeatureSelection
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  27. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. guest_train_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  34. guest_validate_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  36. host_validate_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  37. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  38. data_transform_0, data_transform_1 = DataTransform(name="data_transform_0"), DataTransform(name='data_transform_1')
  39. reader_0, reader_1 = Reader(name="reader_0"), Reader(name='reader_1')
  40. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  41. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  42. data_transform_0.get_party_instance(
  43. role='guest', party_id=guest).component_param(
  44. with_label=True, output_format="dense")
  45. data_transform_0.get_party_instance(
  46. role='host', party_id=host).component_param(
  47. with_label=False, output_format="dense")
  48. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_validate_data)
  49. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_validate_data)
  50. data_transform_1.get_party_instance(
  51. role='guest', party_id=guest).component_param(
  52. with_label=True, output_format="dense")
  53. data_transform_1.get_party_instance(
  54. role='host', party_id=host).component_param(
  55. with_label=True, output_format="dense")
  56. intersection_0 = Intersection(name="intersection_0")
  57. intersection_1 = Intersection(name="intersection_1")
  58. param = {
  59. "method": "quantile",
  60. "optimal_binning_param": {
  61. "metric_method": "gini",
  62. "min_bin_pct": 0.05,
  63. "max_bin_pct": 0.8,
  64. "init_bucket_method": "quantile",
  65. "init_bin_nums": 100,
  66. "mixture": True
  67. },
  68. "compress_thres": 10000,
  69. "head_size": 10000,
  70. "error": 0.001,
  71. "bin_num": 10,
  72. "bin_indexes": -1,
  73. "bin_names": None,
  74. "category_indexes": [0, 1, 2],
  75. "category_names": None,
  76. "adjustment_factor": 0.5,
  77. "local_only": False,
  78. "transform_param": {
  79. "transform_cols": -1,
  80. "transform_names": None,
  81. "transform_type": "bin_num"
  82. }
  83. }
  84. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **param)
  85. hetero_feature_binning_1 = HeteroFeatureBinning(name="hetero_feature_binning_1")
  86. selection_param = {
  87. "select_col_indexes": -1,
  88. "select_names": [],
  89. "filter_methods": ["iv_filter"],
  90. "iv_param": {
  91. "filter_type": "threshold",
  92. "threshold": 2,
  93. "mul_class_merge_type": "max"
  94. }
  95. }
  96. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0", **selection_param)
  97. hetero_feature_selection_1 = HeteroFeatureSelection(name="hetero_feature_selection_1")
  98. pipeline.add_component(reader_0)
  99. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  100. pipeline.add_component(reader_1)
  101. pipeline.add_component(
  102. data_transform_1, data=Data(
  103. data=reader_1.output.data), model=Model(
  104. data_transform_0.output.model))
  105. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  106. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  107. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  108. pipeline.add_component(hetero_feature_binning_1,
  109. data=Data(data=intersection_1.output.data),
  110. model=Model(hetero_feature_binning_0.output.model))
  111. pipeline.add_component(hetero_feature_selection_0,
  112. data=Data(data=hetero_feature_binning_0.output.data),
  113. model=Model(isometric_model=hetero_feature_binning_0.output.model))
  114. pipeline.add_component(hetero_feature_selection_1,
  115. data=Data(data=hetero_feature_binning_1.output.data),
  116. model=Model(hetero_feature_selection_0.output.model))
  117. pipeline.compile()
  118. pipeline.fit()
  119. # predict
  120. # deploy required components
  121. pipeline.deploy_component([data_transform_0, intersection_0, hetero_feature_selection_0])
  122. predict_pipeline = PipeLine()
  123. # add data reader onto predict pipeline
  124. predict_pipeline.add_component(reader_1)
  125. # add selected components from train pipeline onto predict pipeline
  126. # specify data source
  127. predict_pipeline.add_component(
  128. pipeline, data=Data(
  129. predict_input={
  130. pipeline.data_transform_0.input.data: reader_1.output.data}))
  131. # run predict model
  132. predict_pipeline.predict()
  133. if __name__ == "__main__":
  134. parser = argparse.ArgumentParser("PIPELINE DEMO")
  135. parser.add_argument("-config", type=str,
  136. help="config file")
  137. args = parser.parse_args()
  138. if args.config is not None:
  139. main(args.config)
  140. else:
  141. main()