sklearn-lr-multi.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  18. from sklearn.linear_model import SGDClassifier
  19. from sklearn.metrics import roc_auc_score, precision_score, accuracy_score, recall_score, roc_curve
  20. from pipeline.utils.tools import JobConfig
  21. import os
  22. def main(config="../../config.yaml", param="./lr_config.yaml"):
  23. # obtain config
  24. if isinstance(param, str):
  25. param = JobConfig.load_from_file(param)
  26. assert isinstance(param, dict)
  27. data_guest = param["data_guest"]
  28. data_host = param["data_host"]
  29. idx = param["idx"]
  30. label_name = param["label_name"]
  31. if isinstance(config, str):
  32. config = JobConfig.load_from_file(config)
  33. data_base_dir = config["data_base_dir"]
  34. else:
  35. data_base_dir = config.data_base_dir
  36. config_param = {
  37. "penalty": param["penalty"],
  38. "max_iter": param["max_iter"],
  39. "alpha": param["alpha"],
  40. "learning_rate": "optimal",
  41. "eta0": param["learning_rate"],
  42. "random_state": 123
  43. }
  44. # prepare data
  45. df_guest = pandas.read_csv(os.path.join(data_base_dir, data_guest), index_col=idx)
  46. df_host = pandas.read_csv(os.path.join(data_base_dir, data_host), index_col=idx)
  47. # df_test = pandas.read_csv(data_test, index_col=idx)
  48. df = pandas.concat([df_guest, df_host], axis=0)
  49. # df = df_guest.join(df_host, rsuffix="host")
  50. y_train = df[label_name]
  51. x_train = df.drop(label_name, axis=1)
  52. # y_test = df_test[label_name]
  53. # x_test = df_test.drop(label_name, axis=1)
  54. x_test, y_test = x_train, y_train
  55. # lm = LogisticRegression(max_iter=20)
  56. lm = SGDClassifier(loss="log", **config_param)
  57. lm_fit = lm.fit(x_train, y_train)
  58. y_pred = lm_fit.predict(x_test)
  59. acc = accuracy_score(y_test, y_pred)
  60. result = {"accuracy": acc}
  61. print('multi result', result)
  62. return {}, result
  63. if __name__ == "__main__":
  64. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  65. parser.add_argument("-p", "--param", type=str, default="./lr_config.yaml",
  66. help="config file for params")
  67. args = parser.parse_args()
  68. main(args.param)