根据其他数组的阈值创建新的2d numpy数组

app*_*pel 3 python arrays numpy

我有3个2d阵列,我想用它来初始化一个新的2d阵列.新的2d阵列应填充位置(x,y)的0或1,具体取决于其他3个阵列的(x,y)位置的值.

例如,我有这3个2d阵列:

A = [[2, 3, 6],    B = [[5, 9, 0],    C = [[2, 7, 6],
     [9, 8, 3],         [2, 4, 3],         [2, 1, 6],
     [1, 0, 5]]         [4, 5, 1]]         [4, 6, 8]]
Run Code Online (Sandbox Code Playgroud)

和逻辑功能:

D = (A > 4 && B < 5 && C > 5)
Run Code Online (Sandbox Code Playgroud)

这应该创建2d数组:

D = [[0, 0, 1], 
     [0, 0, 0],
     [0, 0, 1]]
Run Code Online (Sandbox Code Playgroud)

现在我可以使用2 for循环来做到这一点,但我想知道是否有更快的numpy方式?

编辑:

以下是我的真实代码示例:

val_max = 10000
a = np.asarray(array_a)
b = np.asarray(array_b)
d = ((a >= val_max) and (b >= val_max)).astype(int)
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

Traceback (most recent call last):
  File "analyze.py", line 70, in <module>
    d = ((a >= val_max) and (b >= val_max)).astype(int)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)

编辑2:

我应该使用&运算符而不是and(类似于'|'vs. or)

jti*_*usj 5

给定A,B和C,您只需将它们转换为numpy数组并使用以下方法计算D:

import numpy as np

A = np.asarray(A)
B = np.asarray(B)
C = np.asarray(C)

D = ((A > 4) & (B < 5) & (C > 5)).astype(int)
Run Code Online (Sandbox Code Playgroud)