【问题标题】:How do I compare non-zero values over multiple 2D numpy arrays?如何比较多个 2D numpy 数组的非零值?
【发布时间】:2016-01-06 22:48:32
【问题描述】:

假设我有两个用随机整数填充的二维数组。 我希望第一个数组保留所有值为 leq 5 的索引,第二个数组保留所有值为 leq 6 的索引。为简单起见,所有其他索引都可以设置为零。

table1 = np.array(([1,3],[5,7]))
table2  = np.array(([2,4],[6,8]))

print table1
print table2 ,'\n'

table1_tol_i = np.where(table1[:,:] > 5)
table1[table1_tol_i] = 0
print table1 

table2_tol_i = np.where(table2[:,:] > 4)
table2[table2_tol_i] = 0
print table2, '\n'

[[1 3]
 [5 7]]
[[2 4]
 [6 8]] 

[[1 3]
 [5 0]]
[[2 4]
 [0 0]]

然后我想只保留两个表都具有非零值的索引。我想要

[[1 3]
 [0 0]]
[[2 4]
 [0 0]]

我尝试了以下方法,但它没有给我想要的东西

nonzero_i = (np.where(table1 != 0) and (np.where(table2 != 0 )))
print table1[nonzero_i]
print table2[nonzero_i]

[1 3]
[2 4]

【问题讨论】:

  • 您的描述准确地描述了我想要达到的目标,但不知道如何实现。

标签: python numpy


【解决方案1】:

正在仅保留两个表都具有非零值的索引 - 这就是为什么当您使用 nonzero_i 索引到 table1table2 时,您只返回第一行 (其中仅包含非零值)。

这个逻辑可以通过构造一个boolean index array来更简单地表达:

# boolean array that is True wherever both table1 & table2 are nonzero
valid = (table1 != 0) & (table2 != 0)

# or alternatively:
# valid = np.logical_and(table1, table2)

您可以使用它直接索引到 table1table2


实际上想要做的似乎是将值 0 分配给 table1table2 为 0 的任何位置。

如果你想要一份副本,你可以使用np.wherenp.where(condition, val_where_true, val_where_false)语法:

print(repr(np.where(valid, table1, 0)))
# array([[1, 3],
#        [0, 0]])

print(repr(np.where(valid, table2, 0)))
# array([[2, 4],
#        [0, 0]])

或者如果你想修改table1table2,你可以这样做

table1[~valid] = 0

table2[~valid] = 0

~ 是“按位非”运算符,它反转布尔索引数组)。

【讨论】:

    【解决方案2】:

    获取表1为0的所有索引:

    table1_zeros = np.where(table1[:,:] == 0)

    获取表 2 为 0 的所有索引:

    table2_zeros = np.where(table2[:,:] == 0)

    将两个表的值都设置为 0:

    table1[table1_zeros] = 0
    table1[table2_zeros] = 0
    table2[table1_zeros] = 0
    table2[table2_zeros] = 0
    

    您也可以使用tableX_tol_i 代替tableX_zeros 一步完成。

    【讨论】:

      【解决方案3】:
      table1 = np.array(([1,3],[5,7]))
      table2  = np.array(([2,4],[6,8]))
      table3  = np.zeros((2,2))
      for k in range(0,2):
          for j in range(0,2):
              if table1[k][j] > 5:
                  table1[k][j] = 0
              if table1[k][j] <= 5:
                  table1[k][j] = table1[k][j]
              if table2[k][j] > 6:
                  table2[k][j] = 0
              if table2[k][j] <= 6:
                  table2[k][j] = table2[k][j]
      x=0
      y=0
      for k in range(0,2):
          for j in range(0,2):
              if table1[k][j] != 0:
                  table3[x,y] = table1[k][j] 
                  x = x+1
      x=0
      y=1
      for k in range(0,2):
          for j in range(0,2):
              if table2[k][j] != 0:
                  table3[x,y] = table2[k][j] 
                  x = x+1   
      print (table3)   
      

      我认为这是最简单的方法,你想要什么。如果您的数组大小发生变化,您可以修改代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        相关资源
        最近更新 更多