forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalg.py
74 lines (59 loc) · 1.98 KB
/
scalg.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
def score(source_data : list, weights : list, *args) -> list:
'''
int list - weights
possible values - 0 / 1
0 if lower values have higher weight in the data set
1 if higher values have higher weight in the data set
==========
Optional arguments:
str - "score_lists"
get a list with all the scores for each piece of data
str - "scores"
get only the final scores for each data set
'''
# getting data
data_lists = []
for item in source_data:
for i in range(len(item)):
try:
data_lists[i].append(float(item[i]))
except IndexError:
data_lists.append([])
data_lists[i].append(float(item[i]))
score_lists = []
# calculating price score
for dlist, weight in zip(data_lists, weights):
mind = min(dlist)
maxd = max(dlist)
score = []
if weight == 0:
for item in dlist:
try:
score.append(1 - ((item - mind) / (maxd - mind)))
except ZeroDivisionError:
score.append(1)
elif weight == 1:
for item in dlist:
try:
score.append((item - mind) / (maxd - mind))
except ZeroDivisionError:
score.append(0)
else:
raise ValueError("Invalid weight of %f provided" % (weight))
score_lists.append(score)
# return score lists
if "score_lists" in args:
return score_lists
# initialize final scores
final_scores = [0 for i in range(len(score_lists[0]))]
# generate final scores
for i, slist in enumerate(score_lists):
for j, ele in enumerate(slist):
final_scores[j] = final_scores[j] + ele
# return only scores
if "scores" in args:
return final_scores
# append scores to source data
for i, ele in enumerate(final_scores):
source_data[i].append(ele)
return source_data