ValueError: 数组的真值

huangapple go评论70阅读模式
英文:

ValueError: The truth value of an array

问题

以下是您提供的代码的中文翻译部分:

我正在编写一个Python 3.11程序出现了以下错误

Traceback (most recent call last):
File "Égalisation\Python Version\Wiener Filter\main.py", line 14, in
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Égalisation\Python Version\Wiener Filter\main.py", line 14, in symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^
File "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()


当我运行这个简单的代码时:

```python
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)]

<details>
<summary>英文:</summary>

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

Traceback (most recent call last):
File "&#201;galisation\Python Version\Wiener Filter\main.py", line 14, in <module>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "&#201;galisation\Python Version\Wiener Filter\main.py", line 14, in <listcomp>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^
File "\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)]



</details>


# 答案1
**得分**: 0

Sounds like this is due to a [regression in Python 3.11](https://github.com/python/cpython/issues/100805) 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:
```py
A = list(np.arange(-np.sqrt(M)+1, np.sqrt(M), 2))
symb = [random.choice(A) for _ in range(nbsymb)]
英文:

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)]

huangapple
  • 本文由 发表于 2023年2月9日 03:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定