ValueError: The truth value of an array
Question:
I work on a Python 3.11 program and I have this error:
Traceback (most recent call last):
File "ÉgalisationPython VersionWiener Filtermain.py", line 14, in <module>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "ÉgalisationPython VersionWiener Filtermain.py", line 14, in <listcomp>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^
File "ProgramsPythonPython311Librandom.py", line 369, in choice
if not seq:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
When I run this simple code:
import numpy as np
import random
nbsymb = 8
M = 16
A = np.arange(-np.sqrt(M)+1, np.sqrt(M), 2)
symb = [random.choice(A) for _ in range(8)]
Answers:
Sounds like this is due to a regression in Python 3.11 which was later fixed. Upgrade to Python >=3.11.2
Numpy has its own random stuff, perhaps you should use that instead: https://numpy.org/doc/stable/reference/random/
If for some reason you can’t upgrade, choose random elements from a list instead of a numpy
array:
A = list(np.arange(-np.sqrt(M)+1, np.sqrt(M), 2))
symb = [random.choice(A) for _ in range(nbsymb)]
I work on a Python 3.11 program and I have this error:
Traceback (most recent call last):
File "ÉgalisationPython VersionWiener Filtermain.py", line 14, in <module>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "ÉgalisationPython VersionWiener Filtermain.py", line 14, in <listcomp>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^
File "ProgramsPythonPython311Librandom.py", line 369, in choice
if not seq:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
When I run this simple code:
import numpy as np
import random
nbsymb = 8
M = 16
A = np.arange(-np.sqrt(M)+1, np.sqrt(M), 2)
symb = [random.choice(A) for _ in range(8)]
Sounds like this is due to a regression in Python 3.11 which was later fixed. Upgrade to Python >=3.11.2
Numpy has its own random stuff, perhaps you should use that instead: https://numpy.org/doc/stable/reference/random/
If for some reason you can’t upgrade, choose random elements from a list instead of a numpy
array:
A = list(np.arange(-np.sqrt(M)+1, np.sqrt(M), 2))
symb = [random.choice(A) for _ in range(nbsymb)]