gpt4 book ai didi

python - Numpy ndarray - 如何根据多列的值选择行

转载 作者:行者123 更新时间:2023-12-01 06:55:06 25 4
gpt4 key购买 nike

我有一个对象的 2D 速度值的 x 和 y 分量的 ndarray,例如:

(Pdb) p highspeedentries
array([[ 0. , 52.57],
[-40.58, 70.89],
[-57.32, 76.47],
[-57.92, 65.1 ],
[-52.47, 96.68],
[-45.58, 77.12],
[-37.83, 69.52]])

(Pdb) p type(highspeedentries)
<class 'numpy.ndarray'>

我必须检查是否有任何行包含该行中第一个和第二个组件的值高于 55。我还需要取任何负速度分量的绝对值,但首先我尝试了以下方法,但它们都给出了错误。

(Pdb) p highspeedentries[highspeedentries[:,1] > 55 and highspeedentries[0,:] > 55]
*** ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

(Pdb) p highspeedentries[highspeedentries[:,1] > 55 & highspeedentries[0,:] > 55]
*** TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be
safely coerced to any supported types according to the casting rule ''safe''

(Pdb) p highspeedentries[np.logical_and(highspeedentries[:,1] > 55, highspeedentries[0,:] > 55)]
*** ValueError: operands could not be broadcast together with shapes (7,) (2,)

最佳答案

一种愚蠢的处理方法是循环遍历整个数组来查找您的状态:

for i in highspeedentries:
if (abs(i[0]) > 55 and abs(i[1]) > 55):
print('True')
break
else:
print('False')

或者,您第三次尝试的方向是正确的:

logical_and(abs(highspeedentries[:, 0]) > 55 , abs(highspeedentries[:, 1]) > 55).any()

索引的顺序不正确,如果将 .any() 添加到末尾,您将得到一个值(如果至少有一个元素为 True,则为 True,否则为 False),而不是 bool 值大批。要应用绝对值,您只需在与 55 进行比较之前对数组应用 abs() 即可。

关于python - Numpy ndarray - 如何根据多列的值选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com