numpy中的二进制操作

Rya*_*an 1 python numpy

我有一个看起来像这样的数组,

array([[[-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    ..., 
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024]],

   [[-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    ..., 
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024]],

   [[-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    ..., 
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024]],

   ..., 
   [[-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
    [-1024, -1024, -1024, ..., -1024, -1024, -1024],
Run Code Online (Sandbox Code Playgroud)

它是一个带有许多独特元素的冗长数组,如何进行操作,如果值大于100,则使值为1,否则使所有值为0.

我试过了

resulted  = np.array([0 if x < 100 else 1 for x in new_one])
Run Code Online (Sandbox Code Playgroud)

但我知道,

在()----> 1中的ValueError Traceback(最近的最后一次调用)结果= np.array([0如果x <100,则为new_one中的x为1)

<ipython-input-72-77697094b8bd> in <listcomp>(.0)
----> 1 resulted  = np.array([0 if x < 100 else 1 for x in new_one])

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)

关于我如何能够实现这一目标的任何想法?提前感谢.

Flo*_*oor 6

布尔值的Astype int会给你想要的ie

arr = np.array([[[-1024, -1024, -1024, 0, -1024, -1024, -1024],
[-1024, -1024, -1024, 150, -1024, -1024, -1024],
[-1024, -1024, -1024,300, -1024, -1024, -1024]]])


(arr>100).astype(int)

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