-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
63 lines (48 loc) · 1.92 KB
/
train.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
55
56
57
58
59
60
61
62
63
import argparse
import json
from srcs.extractData import extract_data
from srcs.normalise import normalise_df, denorm_thetas
from srcs.trainModel import train_model
def options_parser():
"""Use to handle program parameters and options.
"""
parser = argparse.ArgumentParser(
prog='Train',
description='Train a linear regression model',
epilog='Please read the subject before proceeding to understand the input file format.')
parser.add_argument('-f', '--file', type=str, default='data/data.csv', help='the input data file to learn')
parser.add_argument('-e', '--epoch', type=int, default=1000, help='iteration number for training')
parser.add_argument('-l', '--learningRate', type=float, default=0.1, help='learning rate for training model')
parser.add_argument('-p', '--plot', action='store_true', help='show the training')
return parser
def save_thetas(thetas: list[float], file_name: str = 'thetas.json'):
"""
Save the trained model in a json file.
:param thetas: model.
:param file_name: json file.
:return:
"""
try:
data = {'theta0': str(thetas[0]), 'theta1': str(thetas[1])}
with open(file_name, 'w') as file:
json.dump(data, file, indent=4)
except Exception:
raise ValueError('Impossible to save thetas')
def main():
args = options_parser().parse_args()
print("Loading data...")
df = extract_data(args.file)
print("Normalising data...")
normDf = normalise_df(df, ['km', 'price'])
print("Training model...")
thetas = train_model(normDf, epoch=args.epoch, learning_rate=args.learningRate, plot=args.plot)
print("Denormalising model...")
thetas = denorm_thetas(thetas, df)
print("Saving model...")
save_thetas(thetas)
if __name__ == '__main__':
try:
main()
except Exception as e:
print('The programme has just been stopped suddenly')
print(f'Got: {e}')