gbdt-binary.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. import os
  18. import pandas as pd
  19. from sklearn.metrics import roc_auc_score, precision_score, accuracy_score, recall_score
  20. from sklearn.ensemble import GradientBoostingClassifier
  21. from pipeline.utils.tools import JobConfig
  22. def main(config="../../config.yaml", param="./gbdt_config_binary.yaml"):
  23. # obtain config
  24. if isinstance(param, str):
  25. param = JobConfig.load_from_file(param)
  26. data_guest = param["data_guest"]
  27. data_host = param["data_host"]
  28. idx = param["idx"]
  29. label_name = param["label_name"]
  30. print('config is {}'.format(config))
  31. if isinstance(config, str):
  32. config = JobConfig.load_from_file(config)
  33. data_base_dir = config["data_base_dir"]
  34. print('data base dir is', data_base_dir)
  35. else:
  36. data_base_dir = config.data_base_dir
  37. # prepare data
  38. df_guest = pd.read_csv(os.path.join(data_base_dir, data_guest), index_col=idx)
  39. df_host = pd.read_csv(os.path.join(data_base_dir, data_host), index_col=idx)
  40. df = df_guest.join(df_host, rsuffix='host')
  41. y = df[label_name]
  42. X = df.drop(label_name, axis=1)
  43. clf = GradientBoostingClassifier(random_state=0, n_estimators=120 if 'epsilon' in data_guest else 50)
  44. clf.fit(X, y)
  45. y_prob = clf.predict(X)
  46. try:
  47. auc_score = roc_auc_score(y, y_prob)
  48. except BaseException:
  49. print(f"no auc score available")
  50. return
  51. result = {"auc": auc_score}
  52. print(result)
  53. return {}, result
  54. if __name__ == "__main__":
  55. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  56. parser.add_argument("-param", type=str,
  57. help="config file for params")
  58. args = parser.parse_args()
  59. if args.config is not None:
  60. main(args.param)
  61. main()