布尔值作为索引的Python效果(a[a==0]=1)
我目前正在实现我在github上看到的一些代码 () 这里的关注点如下:布尔值作为索引的Python效果(a[a==0]=1),python,arrays,numpy,indexing,boolean-indexing,Python,Arrays,Numpy,Indexing,Boolean Indexing,我目前正在实现我在github上看到的一些代码 () 这里的关注点如下: def prepro(I): """ prepro 210x160x3 uint8 frame into 6400 (80x80) 1D float vector """ I = I[35:195] # crop I = I[::2,::2,0] # downsample by factor of 2 I[I == 144] = 0 # erase background (backgroun
def prepro(I):
""" prepro 210x160x3 uint8 frame into 6400 (80x80) 1D
float vector """
I = I[35:195] # crop
I = I[::2,::2,0] # downsample by factor of 2
I[I == 144] = 0 # erase background (background type 1)
I[I == 109] = 0 # erase background (background type 2)
I[I != 0] = 1 # everything else (paddles, ball) just set to 1
return I.astype(np.float).ravel()
为了训练神经网络,作者正在对一幅图像进行预处理。我感到困惑的是:
I[I == 144] = 0 # erase background (background type 1)
I[I == 109] = 0 # erase background (background type 2)
I[I != 0] = 1 # everything else (paddles, ball) just set
我认为作者希望将列表中值为144(109,而不是0)的所有元素设置为特定值。但如果我是正确的,布尔值在python中只表示0或1。因此,将列表与整数进行比较将始终导致False,因此为0
这使得I[I==x]I[0]:x是整数
那么为什么还要费心这么做呢
我在这里遗漏了什么?NumPy数组有点不同;它们的用法类似于MATLAB中的用法
I==144
生成与I
维度相同的逻辑数组,其中I
中144
的所有位置都是true
,其他所有位置都是false
(其他表达式也是如此。)
使用这样的逻辑数组进行索引意味着索引为
true
的所有位置都将受到赋值的影响。NumPy数组有点不同;它们的用法类似于MATLAB中的用法
I==144
生成与I
维度相同的逻辑数组,其中I
中144
的所有位置都是true
,其他所有位置都是false
(其他表达式也是如此。)
使用这种逻辑数组进行索引意味着索引为true
的所有位置都将受到分配的影响