-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregression.py
More file actions
51 lines (43 loc) · 2.21 KB
/
regression.py
File metadata and controls
51 lines (43 loc) · 2.21 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
from function import IdealFunction
def minimise_loss(training_function, list_of_candidate_functions, loss_function):
"""
returns an IdealFunction based on a training function and a list of ideal functions
:param training_function: training function
:param list_of_candidate_functions: list of candidate ideal functions
:param loss_function: the function use to minimise the error
:return: a IdealFunction object
"""
function_with_smallest_error = None
smallest_error = None
for function in list_of_candidate_functions:
error = loss_function(training_function, function)
if ((smallest_error == None) or error < smallest_error):
smallest_error = error
function_with_smallest_error = function
ideal_function = IdealFunction(function=function_with_smallest_error, training_function=training_function,
error=smallest_error)
return ideal_function
def find_classification(point, ideal_functions):
"""
It computes if a point is within the tolerance of a classification
:param point: a dict object in there is an "x" and an "y"
:param ideal_functions: a list of IdealFunction objects
:return:a tuple containing the closest classification if any, and the distance
"""
current_lowest_classification = None
current_lowest_distance = None
for ideal_function in ideal_functions:
try:
locate_y_in_classification = ideal_function.locate_y_based_on_x(point["x"])
except IndexError:
print("This point is not in the classification function")
raise IndexError
# Observe here how the absolute distance is used
distance = abs(locate_y_in_classification - point["y"])
if (abs(distance < ideal_function.tolerance)):
# This procedure makes sure there is handling if there are multiple classifcations possible
# It returns the one with lowest distance
if ((current_lowest_classification == None) or (distance < current_lowest_distance)):
current_lowest_classification = ideal_function
current_lowest_distance = distance
return current_lowest_classification, current_lowest_distance