如何在numpy上编码

时间:2017-03-16 02:03:02

标签: python numpy

raster,raster_B8a_166 - numpy数组(xsize / xsize)

for i in range(xsize):
    for j in range(xsize):
        if raster[i][j] == 166 and raster_B8a_166[i][j]:
            raster[i][j] = 39
        elif raster[i][j] == 166 and raster_B8a_166[i][j] == False:
            raster[i][j] = 11

1 个答案:

答案 0 :(得分:0)

以下是两个1D阵列的示例。相同的代码应该适用于一对2D数组。

请注意,对于numpy数组,a == 166将返回一个形状与a相同的Truth数组。然后logical_and获取两个真值数组,执行逻辑和,并返回第三个真值数组。最后,您可以通过向其中提供真值数组来返回或设置numpy数组中的特定项目。

import numpy as np

a = np.array([1, 2, 166, 166])
b = np.array([True, False, True, False])

a[np.logical_and((a == 166), (b == True))] = 39
a[np.logical_and((a == 166), (b == False))] = 11

print(a)