gbdt-binary.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 pandas as pd
  18. import os
  19. from sklearn.metrics import roc_auc_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 = pd.concat([df_guest, df_host], axis=0)
  41. y = df[label_name]
  42. X = df.drop(label_name, axis=1)
  43. X_guest = df_guest.drop(label_name, axis=1)
  44. y_guest = df_guest[label_name]
  45. clf = GradientBoostingClassifier(n_estimators=120 if 'epsilon' in data_guest else 50, learning_rate=0.1)
  46. clf.fit(X, y)
  47. y_prob = clf.predict(X_guest)
  48. try:
  49. auc_score = roc_auc_score(y_guest, y_prob)
  50. except BaseException:
  51. print(f"no auc score available")
  52. return
  53. result = {"auc": auc_score}
  54. import time
  55. print(result)
  56. print(data_guest)
  57. time.sleep(3)
  58. return {}, result
  59. if __name__ == "__main__":
  60. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  61. parser.add_argument("-param", type=str,
  62. help="config file for params")
  63. args = parser.parse_args()
  64. if args.config is not None:
  65. main(args.param)
  66. main()