Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: The truth value of an array

I work on a Python 3.11 program and I have this error:

Traceback (most recent call last):
  File "C:\Users\LWWB0754\OneDrive - orange.com\Docs\Égalisation\Python Version\Wiener Filter\main.py", line 14, in <module>
    symb = [random.choice(A) for _ in range(nbsymb)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LWWB0754\OneDrive - orange.com\Docs\Égalisation\Python Version\Wiener Filter\main.py", line 14, in <listcomp>
    symb = [random.choice(A) for _ in range(nbsymb)]
            ^^^^^^^^^^^^^^^^
  File "C:\Users\LWWB0754\AppData\Local\Programs\Python\Python311\Lib\random.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)]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading