gbdt-regression.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.ensemble import GradientBoostingRegressor
  20. from sklearn.metrics import mean_absolute_error
  21. from pipeline.utils.tools import JobConfig
  22. def main(config="../../config.yaml", param="./gbdt_config_reg.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 = GradientBoostingRegressor(random_state=0, n_estimators=50)
  44. clf.fit(X, y)
  45. y_predict = clf.predict(X)
  46. result = {"mean_absolute_error": mean_absolute_error(y, y_predict)}
  47. print(result)
  48. return {}, result
  49. if __name__ == "__main__":
  50. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  51. parser.add_argument("-param", type=str,
  52. help="config file for params")
  53. args = parser.parse_args()
  54. if args.config is not None:
  55. main(args.param)
  56. main()