5

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

array([[ 1,  1,  2,  0,  4],
       [ 5,  6,  7,  8,  9],
       [10,  0,  0, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22,  0, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44],
       [45, 46, 47, 48, 49]])

我还有另外两个数组,例如:

array([[   0,    0,    0,    0],
       [   0,    0,    0,    0],
       [   0, 2891,    0,    0],
       [   0,    0,    0,    0],
       [   0,    0,    0, 2891]])

array([[  0,   0,   0, 643],
       [  0,   0,   0,   0],
       [  0,   0, 643,   0],
       [  0,   0,   0,   0],
       [  0,   0,   0,   0]])

我想要的是从第二个数组到相应位置的第一个数组中选择值 2891,从第三个数组到相应位置的第一个数组中选择 643,以便最终数组看起来像这样:

array([[   1,    1,    2,  643,    4],
       [   5,    6,    7,    8,    9],
       [  10, 2891,  643,   13,   14],
       [  15,   16,   17,   18,   19],
       [  20,   21,   22, 2891,   24],
       [  25,   26,   27,   28,   29],
       [  30,   31,   32,   33,   34],
       [  35,   36,   37,   38,   39],
       [  40,   41,   42,   43,   44],
       [  45,   46,   47,   48,   49]])

到目前为止,我已经尝试过这个命令:

np.place(a,a<1, np.amax(b))

其中a引用第一个数组并b引用第二个数组。它的作用只是将所有 0 值替换为 2891 值。有人可以帮忙吗?

4

3 回答 3

5

您可以使用非零方法找到y和非零的索引:z

In [9]: y.nonzero()
Out[9]: (array([2, 4]), array([1, 3]))

In [10]: z.nonzero()
Out[10]: (array([0, 2]), array([3, 2]))

您可以通过fancing indexing选择关联的值:

In [11]: y[y.nonzero()]
Out[11]: array([2891, 2891])

您可以将这些值分配x给与

In [13]: x[y.nonzero()] = y[y.nonzero()]

import numpy as np
x = np.array([[ 1,  1,  2,  0,  4],
              [ 5,  6,  7,  8,  9],
              [10,  0,  0, 13, 14],
              [15, 16, 17, 18, 19],
              [20, 21, 22,  0, 24],
              [25, 26, 27, 28, 29],
              [30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39],
              [40, 41, 42, 43, 44],
              [45, 46, 47, 48, 49]])

y = np.array([[   0,    0,    0,    0],
              [   0,    0,    0,    0],
              [   0, 2891,    0,    0],
              [   0,    0,    0,    0],
              [   0,    0,    0, 2891]])

z = np.array([[  0,   0,   0, 643],
              [  0,   0,   0,   0],
              [  0,   0, 643,   0],
              [  0,   0,   0,   0],
              [  0,   0,   0,   0]])

x[y.nonzero()] = y[y.nonzero()]
x[z.nonzero()] = z[z.nonzero()]
print(x)

产量

[[   1    1    2  643    4]
 [   5    6    7    8    9]
 [  10 2891  643   13   14]
 [  15   16   17   18   19]
 [  20   21   22 2891   24]
 [  25   26   27   28   29]
 [  30   31   32   33   34]
 [  35   36   37   38   39]
 [  40   41   42   43   44]
 [  45   46   47   48   49]]
于 2013-05-13T11:49:38.513 回答
3

你的意思是从第二个数组和第三个数组中选择最大值吗?如果是这样,请尝试以下操作:

初始化数据:

In [48]: arr = array([[ 1,  1,  2,  0,  4],
   ....:    [ 5,  6,  7,  8,  9],
   ....:    [10,  0,  0, 13, 14],
   ....:    [15, 16, 17, 18, 19],
   ....:    [20, 21, 22,  0, 24],
   ....:    [25, 26, 27, 28, 29],
   ....:    [30, 31, 32, 33, 34],
   ....:    [35, 36, 37, 38, 39],
   ....:    [40, 41, 42, 43, 44],
   ....:    [45, 46, 47, 48, 49]])

In [49]: arr1 = array([[   0,    0,    0,    0],
   ....:    [   0,    0,    0,    0],
   ....:    [   0, 2891,    0,    0],
   ....:    [   0,    0,    0,    0],
   ....:    [   0,    0,    0, 2891]])

In [50]: arr2 = array([[  0,   0,   0, 643],
   ....:    [  0,   0,   0,   0],
   ....:    [  0,   0, 643,   0],
   ....:    [  0,   0,   0,   0],
   ....:    [  0,   0,   0,   0]])

选择和替换:

In [51]: arr[arr1==arr1.max()] = arr1.max()

In [52]: arr[arr2==arr2.max()] = arr2.max()

In [53]: arr
Out[53]:
array([[   1,    1,    2,  643,    4],
       [   5,    6,    7,    8,    9],
       [  10, 2891,  643,   13,   14],
       [  15,   16,   17,   18,   19],
       [  20,   21,   22, 2891,   24],
       [  25,   26,   27,   28,   29],
       [  30,   31,   32,   33,   34],
       [  35,   36,   37,   38,   39],
       [  40,   41,   42,   43,   44],
       [  45,   46,   47,   48,   49]])
于 2013-05-13T11:48:32.687 回答
0

在 numpy 中,您可以以奇怪的方式为数组下标。1. 使用布尔数组 2. 使用索引

a1[a2 > 0] = a2[a2.nonzero()]
于 2013-05-13T11:56:22.883 回答