8000 BUG: np.random.multinomial(<float>, ...) raises a TypeError in numpy 1.26 · Issue #25061 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: np.random.multinomial(<float>, ...) raises a TypeError in numpy 1.26 #25061
Closed
@1fish2

Description

@1fish2

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.

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:

  1. let you know in case it is a bug to fix
  2. hear if this API change applies to other functions so we can fix them all while updating to numpy 1.26.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0