pipeline-hetero-feature-selection-manually.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 DataStatistics
  19. from pipeline.component import DataTransform
  20. from pipeline.component import HeteroFeatureBinning
  21. from pipeline.component import HeteroFeatureSelection
  22. from pipeline.component import Intersection
  23. from pipeline.component import Reader
  24. from pipeline.interface import Data
  25. from pipeline.interface import Model
  26. from pipeline.utils.tools import load_job_config
  27. def main(config="../../config.yaml", namespace=""):
  28. # obtain config
  29. if isinstance(config, str):
  30. config = load_job_config(config)
  31. parties = config.parties
  32. guest = parties.guest[0]
  33. host = parties.host[0]
  34. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  36. # initialize pipeline
  37. pipeline = PipeLine()
  38. # set job initiator
  39. pipeline.set_initiator(role='guest', party_id=guest)
  40. # set participants information
  41. pipeline.set_roles(guest=guest, host=host)
  42. # define Reader components to read in data
  43. reader_0 = Reader(name="reader_0")
  44. # configure Reader for guest
  45. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  46. # configure Reader for host
  47. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  48. # define DataTransform components
  49. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  50. # get DataTransform party instance of guest
  51. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  52. # configure DataTransform for guest
  53. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  54. # get and configure DataTransform party instance of host
  55. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  56. # define Intersection components
  57. intersection_0 = Intersection(name="intersection_0")
  58. pipeline.add_component(reader_0)
  59. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  60. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  61. binning_param = {
  62. "method": "quantile",
  63. "compress_thres": 10000,
  64. "head_size": 10000,
  65. "error": 0.001,
  66. "bin_num": 10,
  67. "bin_indexes": -1,
  68. "bin_names": None,
  69. "category_indexes": None,
  70. "category_names": None,
  71. "adjustment_factor": 0.5,
  72. "local_only": False,
  73. "transform_param": {
  74. "transform_cols": -1,
  75. "transform_names": None,
  76. "transform_type": "bin_num"
  77. }
  78. }
  79. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **binning_param)
  80. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  81. statistic_param = {
  82. "statistics": ["95%", "coefficient_of_variance", "stddev"],
  83. "column_indexes": -1,
  84. "column_names": []
  85. }
  86. statistic_0 = DataStatistics(name="statistic_0", **statistic_param)
  87. pipeline.add_component(statistic_0, data=Data(data=intersection_0.output.data))
  88. selection_param = {
  89. "select_col_indexes": -1,
  90. "select_names": [],
  91. "filter_methods": [
  92. "manually",
  93. "unique_value",
  94. "iv_value_thres",
  95. "coefficient_of_variation_value_thres",
  96. "iv_percentile",
  97. "outlier_cols"
  98. ],
  99. "unique_param": {
  100. "eps": 1e-06
  101. },
  102. "iv_value_param": {
  103. "value_threshold": 0.1
  104. },
  105. "iv_percentile_param": {
  106. "percentile_threshold": 0.9
  107. },
  108. "variance_coe_param": {
  109. "value_threshold": 0.3
  110. },
  111. "outlier_param": {
  112. "percentile": 0.95,
  113. "upper_threshold": 2.0
  114. }}
  115. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0", **selection_param)
  116. hetero_feature_selection_0.get_party_instance(role='guest',
  117. party_id=guest).component_param(
  118. manually_param={"filter_out_indexes": [
  119. 0,
  120. 1,
  121. 2
  122. ],
  123. "filter_out_names": [
  124. "x3"
  125. ]
  126. })
  127. pipeline.add_component(hetero_feature_selection_0,
  128. data=Data(data=intersection_0.output.data),
  129. model=Model(isometric_model=[hetero_feature_binning_0.output.model,
  130. statistic_0.output.model]))
  131. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  132. pipeline.compile()
  133. pipeline.fit()
  134. if __name__ == "__main__":
  135. parser = argparse.ArgumentParser("PIPELINE DEMO")
  136. parser.add_argument("-config", type=str,
  137. help="config file")
  138. args = parser.parse_args()
  139. if args.config is not None:
  140. main(args.config)
  141. else:
  142. main()