sklearn-lr-binary.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import os
  21. from pipeline.utils.tools import JobConfig
  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. print(f"config: {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. "random_state": 105
  44. }
  45. # prepare data
  46. df_guest = pandas.read_csv(os.path.join(data_base_dir, data_guest), index_col=idx)
  47. df_host = pandas.read_csv(os.path.join(data_base_dir, data_host), index_col=idx)
  48. df = df_guest.join(df_host, rsuffix="host")
  49. y = df[label_name]
  50. X = df.drop(label_name, axis=1)
  51. # x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
  52. x_train, x_test, y_train, y_test = X, X, y, y
  53. # lm = LogisticRegression(max_iter=20)
  54. lm = SGDClassifier(loss="log", **config_param)
  55. lm_fit = lm.fit(x_train, y_train)
  56. y_pred = lm_fit.predict(x_test)
  57. y_prob = lm_fit.predict_proba(x_test)[:, 1]
  58. try:
  59. auc_score = roc_auc_score(y_test, y_prob)
  60. except BaseException:
  61. print(f"no auc score available")
  62. return
  63. recall = recall_score(y_test, y_pred, average="macro")
  64. pr = precision_score(y_test, y_pred, average="macro")
  65. acc = accuracy_score(y_test, y_pred)
  66. # y_predict_proba = est.predict_proba(X_test)[:, 1]
  67. fpr, tpr, thresholds = roc_curve(y_test, y_prob)
  68. ks = max(tpr - fpr)
  69. result = {"auc": auc_score, "recall": recall, "precision": pr, "accuracy": acc}
  70. print(result)
  71. print(f"coef_: {lm_fit.coef_}, intercept_: {lm_fit.intercept_}, n_iter: {lm_fit.n_iter_}")
  72. return {}, result
  73. if __name__ == "__main__":
  74. parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
  75. parser.add_argument("-p", "--param", type=str, default="./breast_config.yaml",
  76. help="config file for params")
  77. args = parser.parse_args()
  78. main(param=args.param)