sklearn-lr-binary.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. data_test = param["data_test"]
  30. idx = param["idx"]
  31. label_name = param["label_name"]
  32. if isinstance(config, str):
  33. config = JobConfig.load_from_file(config)
  34. data_base_dir = config["data_base_dir"]
  35. else:
  36. data_base_dir = config.data_base_dir
  37. config_param = {
  38. "penalty": param["penalty"],
  39. "max_iter": 100,
  40. "alpha": param["alpha"],
  41. "learning_rate": "optimal",
  42. "eta0": param["learning_rate"]
  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. y_prob = lm_fit.predict_proba(x_test)[:, 1]
  60. auc_score = roc_auc_score(y_test, y_prob)
  61. recall = recall_score(y_test, y_pred, average="macro")
  62. pr = precision_score(y_test, y_pred, average="macro")
  63. acc = accuracy_score(y_test, y_pred)
  64. # y_predict_proba = est.predict_proba(X_test)[:, 1]
  65. fpr, tpr, thresholds = roc_curve(y_test, y_prob)
  66. ks = max(tpr - fpr)
  67. result = {"auc": auc_score}
  68. print(f"result: {result}")
  69. print(f"coef_: {lm_fit.coef_}, intercept_: {lm_fit.intercept_}, n_iter: {lm_fit.n_iter_}")
  70. return {}, result
  71. if __name__ == "__main__":
  72. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  73. parser.add_argument("-p", "--param", type=str, default="./lr_config.yaml",
  74. help="config file for params")
  75. args = parser.parse_args()
  76. main(args.param)