-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearRegression.py
62 lines (50 loc) · 1.8 KB
/
LinearRegression.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
import sys
from src.log_tools import *
from src.calculations.gd import gradient_descent
from src.calculations.ne import normal_equation
from src.calculations.gd import obtain_output_from_input
from src.calculations.gd import obtain_random_input
from src.tools.plot_tools import plot_points
from src.tools.input_tools import obtain_gd_input, obtain_ne_input
from src.messages import MESSAGES
from src.settings import default_input_file
from src.args import args_handler
def _main(argv):
args = args_handler(argv)
logging.info(MESSAGES['Application_started'])
if args.file:
try:
f = open(args.file, 'r')
f.close()
except FileNotFoundError:
logging.error(MESSAGES["File_not_found"])
sys.exit()
title = args.file
else:
title = default_input_file
mode = args.mode
if mode not in ['GD', 'NE']:
logging.error(MESSAGES["Wrong_mode"])
sys.exit()
if mode == 'GD':
input_data = obtain_gd_input(title)
result = gradient_descent(input_data)
else:
input_data = obtain_gd_input(title)
matrix_x, matrix_y = obtain_ne_input(title)
result = normal_equation(matrix_x, matrix_y)
if args.input:
try:
input_value = [float(x) for x in args.input.split(' ')]
if len(input_value) != len(input_data[0]) - 1:
raise ValueError
except ValueError:
logging.error(MESSAGES["Wrong_input"])
sys.exit()
else:
input_value = obtain_random_input(input_data[0])
calculated_value = obtain_output_from_input(input_value, result)
logging.info(MESSAGES["Input_result"].format(calculated_value))
plot_points(title, input_data, result, calculated_value)
if __name__ == '__main__':
_main(sys.argv)