-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaivalidation.py
82 lines (60 loc) · 2.44 KB
/
aivalidation.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import copy
import math
from random import randrange
from datamanager import *
def get_original_grades(set_to_predict):
return [row[-1] for row in set_to_predict]
def make_subset(dataset, req_size):
subset = []
while len(subset) < req_size:
index = randrange(len(dataset))
subset.append(dataset[-1])
dataset = dataset[:-1]
return convertToNumpyArray(subset) # TODO zamienić to potem na coś bardziej wydajnego (w funkcjach niżej też)
# divides data into two data sets with given coefficient
def two_subset_divide(data, degree):
learn_data = convertToNumpyArray(data[:int(len(data) * degree)])
test_data = convertToNumpyArray(data[int(len(data) * degree):])
return learn_data, test_data
# divides data into given amount (k) of sets
def cross_validation(dataset, k):
subsets = []
set_size = int(len(dataset) / k)
new_set = copy.deepcopy(dataset)
for i in range(k):
subset = make_subset(new_set, set_size)
subsets.append(subset)
return convertToNumpyArray(subsets)
# calculates ratio of classes guessed right
def count_ratio_guessed_right(original, predicted):
right = 0
for i in range(len(original)):
if original[i] == predicted[i]:
right += 1
return right / float(len(original))
# calculates ratio of classes guessed right with rounding
def count_rounded_ratio_guessed_right(original_list, predicted_list):
right = 0
for original, predicted in zip(original_list, predicted_list):
if round(float(original)) == round(float(predicted)):
right += 1
return right / float(len(original_list))
def count_rights(rights):
return sum(rights) / len(rights)
def mean_loss(original, predicted, k):
loses = []
for i in range(len(original)):
loss = (original[i] - predicted[i]) ** 2
loses.append(loss)
return sum(loses) / len(loses)
def mse_loss(mean_loses, k):
return 1/k * sum(mean_loses)
def algorithm_validation(test_set, ai, bounds):
predicted = ai.predict(test_set)
predicted = np.interp(predicted, (predicted.min(), predicted.max()), bounds)
actual = get_original_grades(test_set)
mean_loses = mean_loss(actual, predicted, 2)
guessed_right = count_ratio_guessed_right(actual, predicted)
guessed_rounded = count_rounded_ratio_guessed_right(actual, predicted)
loss_score = mse_loss(mean_loses, 2)
return loss_score, guessed_right, guessed_rounded, predicted, actual