-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlogistic_regression.py
54 lines (37 loc) · 1.48 KB
/
logistic_regression.py
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
import json
import argparse
from pathlib import Path
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
def _logistic_regression(args):
# Open and reads file "data"
with open(args.data) as data_file:
data = json.load(data_file)
# The excted data type is 'dict', however since the file
# was loaded as a json object, it is first loaded as a string
# thus we need to load again from such string in order to get
# the dict-type object.
data = json.loads(data)
x_train = data['x_train']
y_train = data['y_train']
x_test = data['x_test']
y_test = data['y_test']
# Initialize and train the model
model = LogisticRegression()
model.fit(x_train, y_train)
# Get predictions
y_pred = model.predict(x_test)
# Get accuracy
accuracy = accuracy_score(y_test, y_pred)
# Save output into file
with open(args.accuracy, 'w') as accuracy_file:
accuracy_file.write(str(accuracy))
if __name__ == '__main__':
# Defining and parsing the command-line arguments
parser = argparse.ArgumentParser(description='My program description')
parser.add_argument('--data', type=str)
parser.add_argument('--accuracy', type=str)
args = parser.parse_args()
# Creating the directory where the output file will be created (the directory may or may not exist).
Path(args.accuracy).parent.mkdir(parents=True, exist_ok=True)
_logistic_regression(args)