有没有一种很好的方法来检查numpy数组元素是否在一个范围内?

有没有一种很好的方法来检查numpy数组元素是否在一个范围内?

问题描述:

我想写:有没有一种很好的方法来检查numpy数组元素是否在一个范围内?

assert np.all(0 < a < 2) 

其中anumpy阵列,但它不工作。什么是写这个的好方法?

+0

(如果有问题,我必须现在运行,希望问题很清楚。) –

你可以使用numpy.logical_and

>>> a = np.repeat(1, 10) 
>>> np.logical_and(a > 0, a < 2).all() 
True 

或使用&

>>> ((0 < a) & (a < 2)).all() 
True