-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_regression.py
110 lines (88 loc) · 3 KB
/
layer_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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Implementation of the analysis of layer-wise regression.
The purpose of this code is, given a dataset, to check whether there is always a
dominance layer that will, using a regression, estimate the correct missing
value.
"""
import regression as reg
import numpy as np
import copy
from sklearn import linear_model
from sklearn.model_selection import cross_val_predict
from helpers import NULL
import helpers
from scipy import stats
from sklearn.metrics import mean_squared_error as sk_mean_squared_error
def layer_regression_all(A):
"""Implementation of the analysis of layer-wise regression.
Returns a list of estimation for each of the layers.
"""
incomplete = [alt for alt in A if NULL in alt][0]
complete_alts = [alt for alt in A if NULL not in alt]
c = incomplete.index(NULL)
layers = compute_layers(complete_alts)
res = []
for layer in layers:
dataset = layer + [incomplete]
estimation = reg.get_regression(dataset)
# helpers.printmatrix(dataset)
# print(":: ", estimation, "\n")
# helpers.printmatrix(dataset)
res.append(float(estimation))
return res
def layer_regression_guess_layer(A):
"""Implementation of the analysis of layer-wise regression.
Returns a list of estimation for each of the layers.
"""
incomplete = [alt for alt in A if NULL in alt][0]
complete_alts = [alt for alt in A if NULL not in alt]
c = incomplete.index(NULL)
# helpers.printmatrix(complete_alts)
layers = compute_layers(complete_alts[:])
res = None
for layer in layers:
# helpers.printmatrix(layer)
if res is None and pareto_equivalent(incomplete, layer):
# print("here")
# helpers.printmatrix(incomplete)
res = float(reg.get_regression(layer + [incomplete]))
# print(complete_alts)
if res is None:
res = min([alt[c] for alt in complete_alts])
return res
def pareto_equivalent(a, A):
"""Check if alternative a is pareto equivalent to all alternatives in A."""
res = True
for dom in A:
if dominates(a, dom):
res = False
return res
def compute_layers(A):
"""Compute the layers."""
A.sort(reverse=True)
layers = []
layer = []
while (len(A) > 0):
i = 0
while (i < len(A)):
a = A[i]
dominated = False
for dominant in layer:
if dominates(a, dominant):
dominated = True
if not dominated:
layer.append(A.pop(i))
else:
i += 1
layers.append(layer)
layer = []
return layers
def dominates(dominated, dominant):
"""Check whether these alternatives are really dominated.
If an evaluation is missing on one criterion, it is note taken into account.
"""
res = True
for ev1, ev2 in zip(dominated, dominant):
# print(ev1, ev2)
if res and ev1 != NULL and ev2 != NULL and ev1 > ev2:
res = False
return res