gbdt-multi.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_multi.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=50, learning_rate=0.3,)
  46. clf.fit(X, y)
  47. y_pred = clf.predict(X_guest)
  48. acc = accuracy_score(y_guest, y_pred)
  49. result = {"accuracy": acc}
  50. print(result)
  51. return {}, result
  52. if __name__ == "__main__":
  53. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  54. parser.add_argument("-param", type=str,
  55. help="config file for params")
  56. args = parser.parse_args()
  57. if args.config is not None:
  58. main(args.param)
  59. main()