MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / calculate_each_score

Function calculate_each_score

other/scoring_algorithm.py:41–77  ·  view source on GitHub ↗

>>> calculate_each_score([[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]], ... [0, 0, 1]) [[1.0, 0.0, 0.33333333333333337], [0.75, 0.0, 1.0], [0.25, 1.0, 0.0]]

(
    data_lists: list[list[float]], weights: list[int]
)

Source from the content-addressed store, hash-verified

39
40
41def calculate_each_score(
42 data_lists: list[list[float]], weights: list[int]
43) -> list[list[float]]:
44 """
45 >>> calculate_each_score([[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]],
46 ... [0, 0, 1])
47 [[1.0, 0.0, 0.33333333333333337], [0.75, 0.0, 1.0], [0.25, 1.0, 0.0]]
48 """
49 score_lists: list[list[float]] = []
50 for dlist, weight in zip(data_lists, weights):
51 mind = min(dlist)
52 maxd = max(dlist)
53
54 score: list[float] = []
55 # for weight 0 score is 1 - actual score
56 if weight == 0:
57 for item in dlist:
58 try:
59 score.append(1 - ((item - mind) / (maxd - mind)))
60 except ZeroDivisionError:
61 score.append(1)
62
63 elif weight == 1:
64 for item in dlist:
65 try:
66 score.append((item - mind) / (maxd - mind))
67 except ZeroDivisionError:
68 score.append(0)
69
70 # weight not 0 or 1
71 else:
72 msg = f"Invalid weight of {weight:f} provided"
73 raise ValueError(msg)
74
75 score_lists.append(score)
76
77 return score_lists
78
79
80def generate_final_scores(score_lists: list[list[float]]) -> list[float]:

Callers 1

procentual_proximityFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected