Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does numpy.trapz behave differently with booleans than with ints?

Tags:

python

numpy

Booleans in Python are implemented as ints and usually there is no difference in numpy and other packages when changing 0 by False and 1 by True. Why then these two computations differ?

>>> import numpy as np
>>> np.trapz([1,1])
1.0
>>> np.trapz([True, True])
0.5

The same thing happens with scipy.integrate.

like image 231
matiasg Avatar asked Sep 06 '25 03:09

matiasg


1 Answers

Found it. It seems to be a bug: https://github.com/numpy/numpy/issues/12858

As pointed in the issue, the key is that

>>> True + True
2
>>> np.True_ + np.True_
True
like image 73
matiasg Avatar answered Sep 07 '25 20:09

matiasg