icoa-cli 2.19.713 → 2.19.715
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/setter/icoa_metrics.py +426 -0
- package/dist/commands/ai4ctf.js +1 -1
- package/dist/commands/ctf4ai-demo.js +1 -1
- package/dist/commands/ctf4vla.js +1 -1
- package/dist/commands/exam.js +1 -1
- package/dist/commands/ipynb.js +1 -1
- package/dist/commands/kaggle.js +1 -1
- package/dist/commands/setter.js +1 -1
- package/dist/lib/kaggle.js +1 -1
- package/dist/lib/setter-scorer.js +101 -0
- package/dist/lib/setter.js +1 -1
- package/dist/lib/term-image.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
"""icoa_metrics — ready-made metrics for a task's scorer.py. Standard library only.
|
|
2
|
+
|
|
3
|
+
Every metric takes two lists of the same length, row for row: metric(pred, truth)
|
|
4
|
+
pred what the contestant submitted for one column (strings, as read from the CSV)
|
|
5
|
+
truth the same column of the labels file
|
|
6
|
+
|
|
7
|
+
Rules shared with the built-in metrics: a missing, unparseable or non-finite prediction counts as 0;
|
|
8
|
+
class names are compared after trimming spaces.
|
|
9
|
+
|
|
10
|
+
classes accuracy balanced_accuracy macro_f1 weighted_f1 mcc f1 (labels 0/1) qwk (grades)
|
|
11
|
+
probabilities auc ap logloss brier
|
|
12
|
+
numbers mse rmse mae rmsle mape smape r2 pearson spearman kendall_tau
|
|
13
|
+
several columns / groups of rows
|
|
14
|
+
joint_accuracy exact_match group_kendall_tau
|
|
15
|
+
all_correct group_all_correct by_group
|
|
16
|
+
combining weighted({"name": (weight, value), ...})
|
|
17
|
+
"""
|
|
18
|
+
import math
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"accuracy", "balanced_accuracy", "macro_f1", "weighted_f1", "mcc", "f1", "qwk",
|
|
22
|
+
"auc", "ap", "logloss", "brier",
|
|
23
|
+
"mse", "rmse", "mae", "rmsle", "mape", "smape", "r2", "pearson", "spearman", "kendall_tau",
|
|
24
|
+
"by_group", "all_correct", "joint_accuracy", "group_all_correct", "exact_match",
|
|
25
|
+
"group_kendall_tau", "weighted",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
_EPS = 2.220446049250313e-16
|
|
29
|
+
_INF = float("inf")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# ── reading values ───────────────────────────────────────────────────────────────────────────────────────────
|
|
33
|
+
def _s(v):
|
|
34
|
+
return "" if v is None else str(v).strip()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _num(v):
|
|
38
|
+
"""A prediction as a number; missing / unparseable / non-finite = 0.0."""
|
|
39
|
+
try:
|
|
40
|
+
x = float(v)
|
|
41
|
+
except (TypeError, ValueError):
|
|
42
|
+
return 0.0
|
|
43
|
+
return x if math.isfinite(x) else 0.0
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _tnum(v):
|
|
47
|
+
"""A label as a number; a label that is not a number is the setter's mistake, so say so."""
|
|
48
|
+
try:
|
|
49
|
+
return float(v)
|
|
50
|
+
except (TypeError, ValueError):
|
|
51
|
+
raise ValueError("label %r is not a number" % (v,)) from None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _pos(v):
|
|
55
|
+
"""True when the value is the number 1 (the positive class of a 0 / 1 label)."""
|
|
56
|
+
try:
|
|
57
|
+
return _s(v) != "" and float(v) == 1.0
|
|
58
|
+
except (TypeError, ValueError):
|
|
59
|
+
return False
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _same_len(pred, truth):
|
|
63
|
+
pred, truth = list(pred), list(truth)
|
|
64
|
+
if len(pred) != len(truth):
|
|
65
|
+
raise ValueError("pred has %d values, truth has %d — they must be the same rows" % (len(pred), len(truth)))
|
|
66
|
+
return pred, truth
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _pairs(pred, truth):
|
|
70
|
+
pred, truth = _same_len(pred, truth)
|
|
71
|
+
return [(_num(p), _tnum(t)) for p, t in zip(pred, truth)]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _prob_pairs(pred, truth):
|
|
75
|
+
pred, truth = _same_len(pred, truth)
|
|
76
|
+
return [(_num(p), 1 if _pos(t) else 0) for p, t in zip(pred, truth)]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _str_pairs(pred, truth):
|
|
80
|
+
pred, truth = _same_len(pred, truth)
|
|
81
|
+
return [(_s(p), _s(t)) for p, t in zip(pred, truth)]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _mean_of(pt, f):
|
|
85
|
+
if not pt:
|
|
86
|
+
return _INF
|
|
87
|
+
return sum(f(p, t) for p, t in pt) / len(pt)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# ── classes ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
91
|
+
def accuracy(pred, truth):
|
|
92
|
+
pt = _str_pairs(pred, truth)
|
|
93
|
+
if not pt:
|
|
94
|
+
return 0.0
|
|
95
|
+
return sum(1 for p, t in pt if p == t) / len(pt)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _per_class(pt):
|
|
99
|
+
out = []
|
|
100
|
+
for lab in sorted(set(t for _, t in pt)):
|
|
101
|
+
tp = sum(1 for p, t in pt if p == lab and t == lab)
|
|
102
|
+
fp = sum(1 for p, t in pt if p == lab and t != lab)
|
|
103
|
+
fn = sum(1 for p, t in pt if p != lab and t == lab)
|
|
104
|
+
out.append((tp, fp, fn))
|
|
105
|
+
return out
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _f1_of(tp, fp, fn):
|
|
109
|
+
denom = 2 * tp + fp + fn
|
|
110
|
+
return 2 * tp / denom if denom else 0.0
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def macro_f1(pred, truth):
|
|
114
|
+
cls = _per_class(_str_pairs(pred, truth))
|
|
115
|
+
if not cls:
|
|
116
|
+
return 0.0
|
|
117
|
+
return sum(_f1_of(*c) for c in cls) / len(cls)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def balanced_accuracy(pred, truth):
|
|
121
|
+
cls = _per_class(_str_pairs(pred, truth))
|
|
122
|
+
if not cls:
|
|
123
|
+
return 0.0
|
|
124
|
+
return sum(tp / (tp + fn) for tp, _, fn in cls) / len(cls)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def weighted_f1(pred, truth):
|
|
128
|
+
pt = _str_pairs(pred, truth)
|
|
129
|
+
cls = _per_class(pt)
|
|
130
|
+
if not cls:
|
|
131
|
+
return 0.0
|
|
132
|
+
return sum((tp + fn) * _f1_of(tp, fp, fn) for tp, fp, fn in cls) / len(pt)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def mcc(pred, truth):
|
|
136
|
+
pt = _str_pairs(pred, truth)
|
|
137
|
+
n = len(pt)
|
|
138
|
+
if n == 0:
|
|
139
|
+
return 0.0
|
|
140
|
+
tc, pc, c = {}, {}, 0
|
|
141
|
+
for p, t in pt:
|
|
142
|
+
tc[t] = tc.get(t, 0) + 1
|
|
143
|
+
pc[p] = pc.get(p, 0) + 1
|
|
144
|
+
if p == t:
|
|
145
|
+
c += 1
|
|
146
|
+
cov_tp = c * n - sum(v * pc.get(k, 0) for k, v in tc.items())
|
|
147
|
+
cov_tt = n * n - sum(v * v for v in tc.values())
|
|
148
|
+
cov_pp = n * n - sum(v * v for v in pc.values())
|
|
149
|
+
if cov_tt == 0 or cov_pp == 0:
|
|
150
|
+
return 0.0
|
|
151
|
+
return cov_tp / math.sqrt(cov_tt * cov_pp)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def f1(pred, truth):
|
|
155
|
+
"""Labels 0 / 1: F1 of class 1."""
|
|
156
|
+
pred, truth = _same_len(pred, truth)
|
|
157
|
+
tp = fp = fn = 0
|
|
158
|
+
for p, t in zip(pred, truth):
|
|
159
|
+
p, y = _pos(p), _pos(t)
|
|
160
|
+
if p and y:
|
|
161
|
+
tp += 1
|
|
162
|
+
elif p and not y:
|
|
163
|
+
fp += 1
|
|
164
|
+
elif y and not p:
|
|
165
|
+
fn += 1
|
|
166
|
+
return _f1_of(tp, fp, fn)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def qwk(pred, truth):
|
|
170
|
+
"""Quadratic weighted kappa: grades span min..max of the labels; a prediction is rounded half-up and clamped."""
|
|
171
|
+
pred, truth = _same_len(pred, truth)
|
|
172
|
+
ts = [int(math.floor(_tnum(t) + 0.5)) for t in truth]
|
|
173
|
+
if not ts:
|
|
174
|
+
return 0.0
|
|
175
|
+
lo, hi = min(ts), max(ts)
|
|
176
|
+
n = hi - lo + 1
|
|
177
|
+
if n < 2:
|
|
178
|
+
return 0.0
|
|
179
|
+
ps = [min(max(int(math.floor(_num(p) + 0.5)), lo), hi) for p in pred]
|
|
180
|
+
ht, hp, obs = [0] * n, [0] * n, 0
|
|
181
|
+
for t, p in zip(ts, ps):
|
|
182
|
+
ht[t - lo] += 1
|
|
183
|
+
hp[p - lo] += 1
|
|
184
|
+
obs += (p - t) ** 2
|
|
185
|
+
exp = sum((i - j) ** 2 * ht[i] * hp[j] for i in range(n) for j in range(n)) / len(ts)
|
|
186
|
+
if exp == 0:
|
|
187
|
+
return 0.0
|
|
188
|
+
return 1 - obs / exp
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# ── probabilities (labels 0 / 1, prediction = probability of 1) ──────────────────────────────────────────────
|
|
192
|
+
def _ranks(xs):
|
|
193
|
+
"""Average ranks, 1-based; ties share the mean of their positions."""
|
|
194
|
+
order = sorted(range(len(xs)), key=lambda i: xs[i])
|
|
195
|
+
out = [0.0] * len(xs)
|
|
196
|
+
i = 0
|
|
197
|
+
while i < len(order):
|
|
198
|
+
j = i
|
|
199
|
+
while j + 1 < len(order) and xs[order[j + 1]] == xs[order[i]]:
|
|
200
|
+
j += 1
|
|
201
|
+
for k in range(i, j + 1):
|
|
202
|
+
out[order[k]] = (i + j) / 2 + 1
|
|
203
|
+
i = j + 1
|
|
204
|
+
return out
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def auc(pred, truth):
|
|
208
|
+
pt = _prob_pairs(pred, truth)
|
|
209
|
+
n1 = sum(y for _, y in pt)
|
|
210
|
+
n0 = len(pt) - n1
|
|
211
|
+
if n1 == 0 or n0 == 0:
|
|
212
|
+
return 0.5
|
|
213
|
+
r = _ranks([p for p, _ in pt])
|
|
214
|
+
u = sum(r[i] for i in range(len(pt)) if pt[i][1] == 1) - n1 * (n1 + 1) / 2
|
|
215
|
+
return u / (n1 * n0)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def ap(pred, truth):
|
|
219
|
+
"""Average precision (area under the precision-recall curve); tied scores enter together."""
|
|
220
|
+
pt = sorted(_prob_pairs(pred, truth), key=lambda x: -x[0])
|
|
221
|
+
n1 = sum(y for _, y in pt)
|
|
222
|
+
if n1 == 0:
|
|
223
|
+
return 0.0
|
|
224
|
+
total, tp, seen, prev, i = 0.0, 0, 0, 0.0, 0
|
|
225
|
+
while i < len(pt):
|
|
226
|
+
j = i
|
|
227
|
+
while j < len(pt) and pt[j][0] == pt[i][0]:
|
|
228
|
+
tp += pt[j][1]
|
|
229
|
+
seen += 1
|
|
230
|
+
j += 1
|
|
231
|
+
rec = tp / n1
|
|
232
|
+
total += (rec - prev) * (tp / seen)
|
|
233
|
+
prev = rec
|
|
234
|
+
i = j
|
|
235
|
+
return total
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def logloss(pred, truth):
|
|
239
|
+
def one(p0, y):
|
|
240
|
+
p = min(max(p0, 1e-15), 1 - 1e-15)
|
|
241
|
+
return -math.log(p) if y == 1 else -math.log(1 - p)
|
|
242
|
+
|
|
243
|
+
return _mean_of(_prob_pairs(pred, truth), one)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def brier(pred, truth):
|
|
247
|
+
return _mean_of(_prob_pairs(pred, truth), lambda p, y: (p - y) ** 2)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
# ── numbers ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
251
|
+
def mse(pred, truth):
|
|
252
|
+
return _mean_of(_pairs(pred, truth), lambda p, t: (p - t) ** 2)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def rmse(pred, truth):
|
|
256
|
+
return math.sqrt(mse(pred, truth))
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def mae(pred, truth):
|
|
260
|
+
return _mean_of(_pairs(pred, truth), lambda p, t: abs(p - t))
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def rmsle(pred, truth):
|
|
264
|
+
return math.sqrt(
|
|
265
|
+
_mean_of(_pairs(pred, truth), lambda p, t: (math.log1p(max(p, 0.0)) - math.log1p(max(t, 0.0))) ** 2)
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def mape(pred, truth):
|
|
270
|
+
return _mean_of(_pairs(pred, truth), lambda p, t: abs(p - t) / max(abs(t), _EPS))
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def smape(pred, truth):
|
|
274
|
+
def one(p, t):
|
|
275
|
+
d = abs(p) + abs(t)
|
|
276
|
+
return 2 * abs(p - t) / d if d else 0.0
|
|
277
|
+
|
|
278
|
+
return _mean_of(_pairs(pred, truth), one)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def r2(pred, truth):
|
|
282
|
+
pt = _pairs(pred, truth)
|
|
283
|
+
if not pt:
|
|
284
|
+
return 0.0
|
|
285
|
+
mean = sum(t for _, t in pt) / len(pt)
|
|
286
|
+
tot = sum((t - mean) ** 2 for _, t in pt)
|
|
287
|
+
if tot == 0:
|
|
288
|
+
return 0.0
|
|
289
|
+
return 1 - sum((p - t) ** 2 for p, t in pt) / tot
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _corr(xs, ys):
|
|
293
|
+
n = len(xs)
|
|
294
|
+
if n == 0:
|
|
295
|
+
return 0.0
|
|
296
|
+
mx, my = sum(xs) / n, sum(ys) / n
|
|
297
|
+
sxy = sum((x - mx) * (y - my) for x, y in zip(xs, ys))
|
|
298
|
+
sxx = sum((x - mx) ** 2 for x in xs)
|
|
299
|
+
syy = sum((y - my) ** 2 for y in ys)
|
|
300
|
+
if sxx == 0 or syy == 0:
|
|
301
|
+
return 0.0
|
|
302
|
+
return sxy / math.sqrt(sxx * syy)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def pearson(pred, truth):
|
|
306
|
+
pt = _pairs(pred, truth)
|
|
307
|
+
return _corr([p for p, _ in pt], [t for _, t in pt])
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def spearman(pred, truth):
|
|
311
|
+
pt = _pairs(pred, truth)
|
|
312
|
+
return _corr(_ranks([p for p, _ in pt]), _ranks([t for _, t in pt]))
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def kendall_tau(pred, truth, normalised=False):
|
|
316
|
+
"""Kendall tau-b (ties handled), -1 .. 1; 0 when either side is constant or there are fewer than 2 rows.
|
|
317
|
+
normalised=True maps it to 0 .. 1 as (tau + 1) / 2."""
|
|
318
|
+
pt = _pairs(pred, truth)
|
|
319
|
+
conc = disc = tie_p = tie_t = 0
|
|
320
|
+
for i in range(len(pt)):
|
|
321
|
+
for j in range(i + 1, len(pt)):
|
|
322
|
+
dp, dt = pt[i][0] - pt[j][0], pt[i][1] - pt[j][1]
|
|
323
|
+
if dp == 0:
|
|
324
|
+
tie_p += 1
|
|
325
|
+
if dt == 0:
|
|
326
|
+
tie_t += 1
|
|
327
|
+
if dp * dt > 0:
|
|
328
|
+
conc += 1
|
|
329
|
+
elif dp * dt < 0:
|
|
330
|
+
disc += 1
|
|
331
|
+
n0 = len(pt) * (len(pt) - 1) // 2
|
|
332
|
+
denom = math.sqrt((n0 - tie_p) * (n0 - tie_t))
|
|
333
|
+
tau = (conc - disc) / denom if denom else 0.0
|
|
334
|
+
return (tau + 1) / 2 if normalised else tau
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
# ── several columns, groups of rows ──────────────────────────────────────────────────────────────────────────
|
|
338
|
+
def _groups(keys):
|
|
339
|
+
"""Row numbers of each group, groups in order of first appearance."""
|
|
340
|
+
out = {}
|
|
341
|
+
for i, k in enumerate(keys):
|
|
342
|
+
out.setdefault(_s(k), []).append(i)
|
|
343
|
+
return list(out.values())
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def by_group(keys, fn, *columns):
|
|
347
|
+
"""Apply fn inside each group of rows and return the mean over groups.
|
|
348
|
+
by_group(truth["week"], mae, pred["mm"], truth["mm"])"""
|
|
349
|
+
keys = list(keys)
|
|
350
|
+
cols = [list(c) for c in columns]
|
|
351
|
+
for c in cols:
|
|
352
|
+
if len(c) != len(keys):
|
|
353
|
+
raise ValueError("by_group: a column has %d values, the keys have %d" % (len(c), len(keys)))
|
|
354
|
+
groups = _groups(keys)
|
|
355
|
+
if not groups:
|
|
356
|
+
return 0.0
|
|
357
|
+
return sum(fn(*[[c[i] for i in rows] for c in cols]) for rows in groups) / len(groups)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def _col_list(cols):
|
|
361
|
+
"""Accept one column (a list of values) or a list of columns."""
|
|
362
|
+
cols = list(cols)
|
|
363
|
+
if cols and not isinstance(cols[0], (list, tuple)):
|
|
364
|
+
return [cols]
|
|
365
|
+
return [list(c) for c in cols]
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def all_correct(pred_cols, truth_cols):
|
|
369
|
+
"""Per row: True when every listed column is right. all_correct([pred["a"], pred["b"]], [truth["a"], truth["b"]])"""
|
|
370
|
+
pc, tc = _col_list(pred_cols), _col_list(truth_cols)
|
|
371
|
+
if len(pc) != len(tc):
|
|
372
|
+
raise ValueError("all_correct: %d prediction columns against %d label columns" % (len(pc), len(tc)))
|
|
373
|
+
if not pc:
|
|
374
|
+
return []
|
|
375
|
+
for p, t in zip(pc, tc):
|
|
376
|
+
_same_len(p, t)
|
|
377
|
+
return [all(_s(p[i]) == _s(t[i]) for p, t in zip(pc, tc)) for i in range(len(tc[0]))]
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def joint_accuracy(pred_cols, truth_cols):
|
|
381
|
+
"""Share of rows where every listed column is right."""
|
|
382
|
+
ok = all_correct(pred_cols, truth_cols)
|
|
383
|
+
return sum(ok) / len(ok) if ok else 0.0
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def group_all_correct(keys, pred_cols, truth_cols):
|
|
387
|
+
"""Share of groups in which every row is right in every listed column."""
|
|
388
|
+
ok = all_correct(pred_cols, truth_cols)
|
|
389
|
+
keys = list(keys)
|
|
390
|
+
if len(keys) != len(ok):
|
|
391
|
+
raise ValueError("group_all_correct: the keys have %d values, the columns have %d" % (len(keys), len(ok)))
|
|
392
|
+
groups = _groups(keys)
|
|
393
|
+
if not groups:
|
|
394
|
+
return 0.0
|
|
395
|
+
return sum(1 for rows in groups if all(ok[i] for i in rows)) / len(groups)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
exact_match = group_all_correct
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def group_kendall_tau(keys, pred, truth, normalised=True):
|
|
402
|
+
"""Kendall tau inside each group, mean over groups (0 .. 1 by default)."""
|
|
403
|
+
return by_group(keys, lambda p, t: kendall_tau(p, t, normalised=normalised), pred, truth)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
# ── combining ────────────────────────────────────────────────────────────────────────────────────────────────
|
|
407
|
+
def weighted(parts):
|
|
408
|
+
"""weighted({"rain": (0.6, 0.81), "amount": (0.4, 0.55)}) -> {"score": 0.706, "parts": {...}}
|
|
409
|
+
The weights must add up to 1."""
|
|
410
|
+
if not parts:
|
|
411
|
+
raise ValueError("weighted: give at least one part")
|
|
412
|
+
total = 0.0
|
|
413
|
+
score = 0.0
|
|
414
|
+
values = {}
|
|
415
|
+
for name, pair in parts.items():
|
|
416
|
+
try:
|
|
417
|
+
w, v = pair
|
|
418
|
+
w, v = float(w), float(v)
|
|
419
|
+
except (TypeError, ValueError):
|
|
420
|
+
raise ValueError('weighted: part "%s" must be (weight, value), two numbers' % name) from None
|
|
421
|
+
total += w
|
|
422
|
+
score += w * v
|
|
423
|
+
values[str(name)] = v
|
|
424
|
+
if abs(total - 1.0) > 1e-9:
|
|
425
|
+
raise ValueError("weighted: the weights add up to %.6g — they must add up to 1" % total)
|
|
426
|
+
return {"score": score, "parts": values}
|