简体   繁体   English

NumPy 错误:具有多个元素的数组的真值不明确。 不使用 Boolean 逻辑时使用 a.any() 或 a.all()

[英]NumPy error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() when not using Boolean logic

I'm trying to convert a 2d NumPy array of complex numbers into another 2d array RGB values for an image, by mapping a function over the array.我正在尝试通过将 function 映射到数组上,将复数的二维 NumPy 数组转换为图像的另一个二维数组 RGB 值。

This is the function:这是 function:

import numpy as np
import colorsys

def ComplexToRGB(complex):
    angle = ((np.angle(complex,deg=True)+360)%360)/360
    magnitude = np.absolute(complex)
    rawColor =  colorsys.hls_to_rgb(angle,0.5,1-np.log2(magnitude)%1)
    return rawColor

I map this function over an array by passing the array as input: I map 这个 function 通过将数组作为输入传递给数组:

print(ComplexToRGB(foo))

where foo is foo=np.linspace(-1-1j,1+1j,11) .其中foofoo=np.linspace(-1-1j,1+1j,11)

When I pass a single complex number as an input, I don't get an error, but when I try to use an array as an input, I get the rather mysterious error当我将单个复数作为输入传递时,我没有收到错误,但是当我尝试使用数组作为输入时,我收到了相当神秘的错误

Exception has occurred: ValueError
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

  File "C:\Users\trevo\Documents\GitHub\NumPy-Complex-Function-Grapher\colorTest.py", line 7, in ComplexToRGB
    rawColor =  colorsys.hls_to_rgb(angle,0.5,1-np.log2(magnitude)%1)
  File "C:\Users\trevo\Documents\GitHub\NumPy-Complex-Function-Grapher\colorTest.py", line 12, in <module>
    print(ComplexToRGB(foo))

I understand that you can't use Boolean conditionals when you map over an array, but I don't have any logic in my function, just math, so I'm not user where the error is coming from.我知道当您在数组上使用 map 时,您不能使用 Boolean 条件条件,但是我的 function 中没有任何逻辑,所以我的错误来自哪里,而不是用户。 Thanks for your help!谢谢你的帮助!

With a single complex number:用一个复数:

In [3]: ComplexToRGB(1+3j)
Out[3]: (0.6041685072417726, 0.6695179762781593, 0.33048202372184066)

same thing with a single element array:与单个元素数组相同:

In [4]: ComplexToRGB(np.array(1+3j))
Out[4]: (0.6041685072417726, 0.6695179762781593, 0.33048202372184066)
In [5]: ComplexToRGB(np.array([1+3j]))
Out[5]: (array([0.60416851]), array([0.66951798]), array([0.33048202]))

but with a 2 element array:但有一个 2 元素数组:

In [6]: ComplexToRGB(np.array([1+3j]*2))
Traceback (most recent call last):
  File "<ipython-input-6-a1b8e92d5dcc>", line 1, in <module>
    ComplexToRGB(np.array([1+3j]*2))
  File "<ipython-input-1-05948df7c0bd>", line 6, in ComplexToRGB
    rawColor =  colorsys.hls_to_rgb(angle,0.5,1-np.log2(magnitude)%1)
  File "/usr/lib/python3.8/colorsys.py", line 99, in hls_to_rgb
    if s == 0.0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

You may not have any explicit logic in your code, but the Python colorsys code does do a if test.您的代码中可能没有任何明确的逻辑,但 Python colorsys代码确实进行了if测试。 colorsys is a base Python module, and is not written with numpy arrays in mind. colorsys是一个基础 Python 模块,在编写时并未考虑numpy arrays。

Read the full traceback, and if necessary, read the docs of the relevant function(s).阅读完整的回溯,如有必要,阅读相关函数的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NumPy 错误:包含多个元素的数组的真值不明确。 使用 a.any() 或 a.all() - NumPy Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 具有多个元素的数组的真值是不明确的。 使用 a.any() 或 a.all() - Numpy 库 - The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() - Numpy Library Numpy Softmax-包含多个元素的数组的真值不明确。 使用a.any()或a.all() - Numpy Softmax - The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() numpy:具有多个元素的数组的真值不明确。 使用a.any()或a.all() - Numpy: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Numpy:ValueError:具有多个元素的数组的真值不明确。 使用 a.any() 或 a.all() - Numpy: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() GridSearchCV-错误:具有多个元素的数组的真值不明确。 使用a.any()或a.all() - GridSearchCV - Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 错误:具有多个元素的数组的真值不明确。 使用 a.any() 或 a.all() - the Error : The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Pyomo 错误:具有多个元素的数组的真值不明确。 使用 a.any() 或 a.all() - Pyomo Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Python 错误:包含多个元素的数组的真值不明确。 使用 a.any() 或 a.all() - Python Error : the truth value of an array with more than one element is ambiguous. use a.any() or a.all() 错误:具有多个元素的数组的真值不明确。 使用a.any()或a.all() - Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM