Description
Describe the issue:
Up through numpy 1.25.2, random.multinomial()
accepted a float as the first argument n, whereas numpy 1.26 does not. (In our code, that value comes from a computation involving scipy.constants.Avogadro
.)
In numpy 1.25.2 (tweaking an example from the docs):
>>> import numpy as np
>>> np.random.multinomial(20.0, [1/6.]*6, size=1)
array([[4, 5, 2, 6, 3, 0]])
In numpy 1.26.0 and 1.26.1:
>>> import numpy as np
>>> np.random.multinomial(20.0, [1/6.]*6, size=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "numpy/random/mtrand.pyx", line 4256, in numpy.random.mtrand.RandomState.multinomial
TypeError: 'float' object cannot be interpreted as an integer
Q. Is this an intentional API change?
I don't see it in the release notes.
- The doc for multinomial's first argument says
n: int
. - mtrand.pyx declares n as
np.npy_intp
. - mtrand.pyi mtrand.pyi declares n as
_ArrayLikeInt_co
. - __init__.cython-30.pxd declares
npy_intp
asPy_ssize_t
. - Cython's UFuncs.pyx declares
npy_intp
asint
.
So I guess what changed was shifting API definitions from Cython's UFuncs.pyx to NumPy's __init__.cython-30.pxd
Environment: macOS 13.6, Intel CPU, Python 3.11.6.
Also: Ubuntu Linux, Intel CPU, Python 3.11.6.
Reproduce the code example:
import numpy as np
np.random.multinomial(20.0, [1/6.]*6, size=1)
Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "numpy/random/mtrand.pyx", line 4256, in numpy.random.mtrand.RandomState.multinomial
TypeError: 'float' object cannot be interpreted as an integer
Runtime information:
import sys, numpy; print(numpy.__version__); print(sys.version)
1.26.1
3.11.6 (main, Oct 31 2023, 22:06:12) [Clang 15.0.0 (clang-1500.0.40.1)]
print(numpy.show_runtime())
[{'numpy_version': '1.26.1',
'python': '3.11.6 (main, Oct 31 2023, 22:06:12) [Clang 15.0.0 '
'(clang-1500.0.40.1)]',
'uname': uname_result(system='Darwin', node='jerrys-mbp.lan', release='22.6.0', version='Darwin Kernel Version 22.6.0: Fri Sep 15 13:39:52 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_X86_64', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': '/usr/local/var/pyenv/versions/3.11.6/envs/test1/lib/python3.11/site-packages/numpy/.dylibs/libopenblas64_.0.dylib',
'internal_api': 'openblas',
'num_threads': 1,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23.dev'}]
None
Context for the issue:
I'll adjust our calling code to convert these values to int
, so fixing this is not a priority.
I'd like to:
- let you know in case it is a bug to fix
- hear if this API change applies to other functions so we can fix them all while updating to numpy 1.26.1