Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weighted average where one weight is infinite

Tags:

python

numpy

Using NumPy's weighted average, I expected an element with infinite weighting to dominate the result, but instead it returns NaN,

>>> np.average([1,2], weights=[np.inf, 1])
nan

Was this an intentional design? It seems counter-intuitive.


EDIT: here's a more simple example:

>>> np.average([1], weights=[np.inf])
nan
like image 307
Garrett Avatar asked Nov 25 '25 15:11

Garrett


1 Answers

Though not intentional, it is mathematically correct.

You end up with a formula like infinity/infinity. The result depends on which infinitiy is larger. And that is nonsense.

You need concrete numbers as weights, so you could use very large ones.

like image 58
sweber Avatar answered Nov 27 '25 06:11

sweber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!