gpt4 book ai didi

python - 从 3D 数组中选择非无限数据

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

我尝试从 3D 数组的第一行中过滤具有非无限数据的列。

这是我的尝试:

r1只是一个如何选择第一行大于2的条目的例子。

r2:A,我认为,与非无限过滤器类似的构造不起作用。什么是正确的方法?

import numpy as np

data = np.array([
[[1,2,3,4], [5,6,7,8]],
[[1,3,5,6], [8,1,3,2]],
[[np.Inf,1,1,8], [5,8,1,9]]
])

r1 = data[np.where(data[...,0] > 2)]
print(r1)

r2 = data[np.where(not np.isinf(data[...,0]))]
print(r2)

这给出了以下内容。结果 r1 是正确的,因为它根据大于 2 的数字进行选择。

[[ 5.  6.  7.  8.]
[ 8. 1. 3. 2.]
[inf 1. 1. 8.]
[ 5. 8. 1. 9.]]
Traceback (most recent call last): ...
r2 = data[np.where(not np.isinf(data[...,0]))]

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

最佳答案

您应该使用 ~(二进制非),而不是 not 来反转 bool 数组,因为 ~ 正在向元素广播否定,同时not 会尝试获取整个数组的 bool 值(不支持,因为它不明确):

r2 = data[~np.isinf(data[...,0])

输出:

array([[1., 2., 3., 4.],
[5., 6., 7., 8.],
[1., 3., 5., 6.],
[8., 1., 3., 2.],
[5., 8., 1., 9.]])

关于python - 从 3D 数组中选择非无限数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70591256/

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