【问题标题】:ValueError in simple Python calculation简单 Python 计算中的 ValueError
【发布时间】:2014-11-05 04:33:13
【问题描述】:

这是我的函数,变量tracks是一个列表,列表的每个元素都是一个n x 3数组:

temp = np.array(np.zeros((n, n)))
for j in range(n-1):
    for w in range(j + 1, n):  
        mindistance = np.zeros(len(tracks[j]))
        for i in range(len(tracks[j])):   
            mindistance[i] = np.linalg.norm(min(np.fabs(np.array(tracks[w]) - tracks[j][i])))
        temp[j][w]=np.sum(mindistance)/len(tracks[j])

我正在尝试计算表示空间中 3d 线的列表数组之间的最小距离,但出现错误:

ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()。

该错误可能与对min() 的调用有关,但我无法解决。以下是错误回溯:

Traceback (most recent call last):

  File "<ipython-input-14-7fb640816626>", line 1, in <module>
    runfile('/Users/G_Laza/Desktop/functions/Main.py', wdir='/Users/G_Laza/Desktop/functions')

  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 580, in runfile
    execfile(filename, namespace)

  File "/Users/G_Laza/Desktop/functions/Main.py", line 42, in <module>
    tempA = distance_calc.dist_calc(len(subset_A), subset_A)  # distance matrix calculation

  File "distance_calc.py", line 23, in dist_calc
    mindistance[i] = np.linalg.norm(min(np.fabs(np.array(tracks[w]) - tracks[j][i])))

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

【问题讨论】:

  • 请发布完整的 Traceback。
  • 在引发错误之前,它会进入嵌套循环多远? np.fabs(np.array(tracks[w]) - tracks[j][i]) 抛出错误时的值是多少?
  • 该值是一个数组,它在第一次计算中停止。
  • 只是检查错误是否在min中,您是否尝试使用np.min()np.amin()
  • 整数、浮点数、布尔值的数组?您了解错误的含义吗 - 在 shell 中尝试if np.ones(4) == 1: pass。您是否尝试过仅使用该行的一部分添加语句以查看计算的哪一部分导致问题,然后查看这些操作数的值以了解其发生原因?

标签: python numpy


【解决方案1】:

发生错误是因为您无法确定完整数组是True 还是False。如果所有元素都是 True 但只有一个,那么数组的布尔状态会是什么?

min 接受一个可迭代的参数并将每个元素与另一个元素进行比较,每个比较results 为一个布尔值。迭代一维 numpy 数组会产生单个元素 - min 适用于一维 numpy 数组。

>>> a
array([-4, -3, -2, -1,  0,  1,  2,  3,  4])
>>> for thing in a:
     print thing,


-4 -3 -2 -1 0 1 2 3 4
>>> min(a)
-4
>>>

遍历二维 numpy 数组会产生行。

>>> b
array([[-4, -3, -2],
       [-1,  0,  1],
       [ 2,  3,  4]])
>>> for thing in b:
     print thing

[-4 -3 -2]
[-1  0  1]
[2 3 4]
>>> 

min 不适用于二维数组,因为它正在比较数组和 - The truth value of an array with more than one element is ambiguous

>>> c
array([0, 1, 2, 3])
>>> c < 2
array([ True,  True, False, False], dtype=bool)
>>> bool(c < 2)

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    bool(c < 2)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 
>>> bool(np.array((True, True)))

Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    bool(np.array((True, True)))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 
>>> bool(np.array((True, False)))

Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    bool(np.array((True, False)))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 

如果您需要找到具有最小值的元素,请使用numpy.aminndarray.min 方法。

>>> 
>>> np.amin(b)
-4
>>> b.min()
-4
>>> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多