sklearn-lr-multi.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  19. from pipeline.utils.tools import JobConfig
  20. from sklearn.linear_model import SGDClassifier
  21. from sklearn.metrics import precision_score, accuracy_score, recall_score
  22. def main(config="../../config.yaml", param="./vechile_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": 105
  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 = df_guest.join(df_host, rsuffix="host")
  48. y = df[label_name]
  49. X = df.drop(label_name, axis=1)
  50. # lm = LogisticRegression(max_iter=20)
  51. lm = SGDClassifier(loss="log", **config_param, shuffle=False)
  52. lm_fit = lm.fit(X, y)
  53. y_pred = lm_fit.predict(X)
  54. recall = recall_score(y, y_pred, average="macro")
  55. pr = precision_score(y, y_pred, average="macro")
  56. acc = accuracy_score(y, y_pred)
  57. result = {"accuracy": acc}
  58. print(result)
  59. return {}, result
  60. if __name__ == "__main__":
  61. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  62. parser.add_argument("-param", type=str,
  63. help="config file for params")
  64. args = parser.parse_args()
  65. if args.param is not None:
  66. main(args.param)