【问题标题】:Perform filtering on N Dimensional numpy array对 N 维 numpy 数组执行过滤
【发布时间】:2021-08-28 12:33:06
【问题描述】:

我有两个N维数组,两个数组的维数相同。

  1. a = (n,n,52)
  2. b = (n,n,52)

我正在尝试用变量 b 中的 (n,n) 数组过滤变量 a 中的每个 (n,n) 数组。我正在尝试使用命令

b[a==0 | a>5] = 1

但我收到以下错误

IndexError: boolean index did not match indexed array along dimension 2; dimension is 52 but corresponding boolean dimension is 1

在弄清楚如何使用另一个 N 维数组过滤一个 N 维数组时,我需要一些帮助。

【问题讨论】:

  • “过滤器”是什么意思?怎么过滤?请提供minimal reproducible example 以及所需的结果。
  • 你需要括号:b[(a==0) | (a>5)] = 1
  • 做什么 |代表 ?还是??
  • 感谢@AlexanderS.Brunmayr,这有帮助
  • @PierreD :过滤数组 从现有数组中取出一些元素并从中创建一个新数组称为过滤。在 NumPy 中,您使用布尔索引列表过滤数组。

标签: python python-3.x numpy numpy-ndarray n-dimensional


【解决方案1】:

对于像我这样的非专家:

Filtering Arrays: Getting some elements out of an existing array and creating a new array out of them is called filtering. In NumPy, you filter an array using a boolean index list. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array. [来自NumPy Filter Array]

@AlexanderS.Brunmayr 在这里回答我的个人 stackoverflow python 参考数据库:


import numpy as np

n= 2

a = np.zeros((n,n,2))

b = np.zeros((n,n,2))

a[1,1,0] = 3
a[1,1,1]= 7


print('a : \n',a,'\n b : \n',b,'\n')



print('\n filter : \n',[(a==0) | (a>5)])  ## array filter

b[(a==0) | (a>5)] = 1  ### ------> change b[a==0 | a>5]  to b[(a==0) | (a>5)]


print('a : \n',a,'\n b : \n',b,'\n')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多