forked from FederatedAI/FATE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsklearn-lr-multi.py
More file actions
85 lines (68 loc) · 2.68 KB
/
sklearn-lr-multi.py
File metadata and controls
85 lines (68 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#
# Copyright 2019 The FATE Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import pandas
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import roc_auc_score, precision_score, accuracy_score, recall_score, roc_curve
from pipeline.utils.tools import JobConfig
import os
def main(config="../../config.yaml", param="./lr_config.yaml"):
# obtain config
if isinstance(param, str):
param = JobConfig.load_from_file(param)
assert isinstance(param, dict)
data_guest = param["data_guest"]
data_host = param["data_host"]
idx = param["idx"]
label_name = param["label_name"]
if isinstance(config, str):
config = JobConfig.load_from_file(config)
data_base_dir = config["data_base_dir"]
else:
data_base_dir = config.data_base_dir
config_param = {
"penalty": param["penalty"],
"max_iter": param["max_iter"],
"alpha": param["alpha"],
"learning_rate": "optimal",
"eta0": param["learning_rate"],
"random_state": 123
}
# prepare data
df_guest = pandas.read_csv(os.path.join(data_base_dir, data_guest), index_col=idx)
df_host = pandas.read_csv(os.path.join(data_base_dir, data_host), index_col=idx)
# df_test = pandas.read_csv(data_test, index_col=idx)
df = pandas.concat([df_guest, df_host], axis=0)
# df = df_guest.join(df_host, rsuffix="host")
y_train = df[label_name]
x_train = df.drop(label_name, axis=1)
# y_test = df_test[label_name]
# x_test = df_test.drop(label_name, axis=1)
x_test, y_test = x_train, y_train
# lm = LogisticRegression(max_iter=20)
lm = SGDClassifier(loss="log", **config_param)
lm_fit = lm.fit(x_train, y_train)
y_pred = lm_fit.predict(x_test)
acc = accuracy_score(y_test, y_pred)
result = {"accuracy": acc}
print('multi result', result)
return {}, result
if __name__ == "__main__":
parser = argparse.ArgumentParser("BENCHMARK-QUALITY SKLEARN JOB")
parser.add_argument("-p", "--param", type=str, default="./lr_config.yaml",
help="config file for params")
args = parser.parse_args()
main(args.param)