gbdt-regression.py 2.4 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 mean_absolute_error
  20. from sklearn.metrics import mean_squared_error
  21. from sklearn.ensemble import GradientBoostingRegressor
  22. from pipeline.utils.tools import JobConfig
  23. def main(config="../../config.yaml", param="./gbdt_config_multi.yaml"):
  24. # obtain config
  25. if isinstance(param, str):
  26. param = JobConfig.load_from_file(param)
  27. data_guest = param["data_guest"]
  28. data_host = param["data_host"]
  29. idx = param["idx"]
  30. label_name = param["label_name"]
  31. print('config is {}'.format(config))
  32. if isinstance(config, str):
  33. config = JobConfig.load_from_file(config)
  34. data_base_dir = config["data_base_dir"]
  35. print('data base dir is', data_base_dir)
  36. else:
  37. data_base_dir = config.data_base_dir
  38. # prepare data
  39. df_guest = pd.read_csv(os.path.join(data_base_dir, data_guest), index_col=idx)
  40. df_host = pd.read_csv(os.path.join(data_base_dir, data_host), index_col=idx)
  41. df = pd.concat([df_guest, df_host], axis=0)
  42. y = df[label_name]
  43. X = df.drop(label_name, axis=1)
  44. X_guest = df_guest.drop(label_name, axis=1)
  45. y_guest = df_guest[label_name]
  46. clf = GradientBoostingRegressor(n_estimators=40)
  47. clf.fit(X, y)
  48. y_predict = clf.predict(X_guest)
  49. result = {"mean_squared_error": mean_squared_error(y_guest, y_predict),
  50. "mean_absolute_error": mean_absolute_error(y_guest, y_predict)
  51. }
  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()