首页 > 解决方案 > 如何为数组中的某些元素获取零值?

问题描述

我的输出数组如下所示。然后我想让所有元素为零,其值介于(0.75 to 1.06) (0,75 > output > 1.06).

array([2.        , 1.72787724, 1.45575448, 1.18363171, 0.91150895,
       0.63938619, 0.36726343, 0.09514066, 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
        0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.12235294,
       0.43529412, 0.74823529, 1.06117647, 1.37411765, 1.68705882,
       2.        ])

我试过np.where(0.75 >output >1.06,0,1)了,但它不起作用:

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

请帮我解决这样的问题。

提前致谢。

标签: pythonnumpy

解决方案


您可以使用 numpys 内置的逻辑运算符来执行此操作。尝试:np.logical_and(np.where(output > 0.75), np.where(output < 1.06))


推荐阅读