【问题标题】:How to replace every tenth (nonzero) value in a 2d np.array by zero如何将 2d np.array 中的每十分之一(非零)值替换为零
【发布时间】:2018-08-05 22:47:39
【问题描述】:

我一直在尝试,但没有成功。

for x in range(0,len(A),10):
    for y in range (0,len(A),10):
        if x > 0:
            if y > 0:
                A[index] = 0

【问题讨论】:

  • 在您的示例中A 是什么?
  • 你能举一些输入和输出的例子吗? “每十分之一(非零)值”是模棱两可的,并且与您的代码正在执行的操作完全不同。您可能会发现这很有帮助:How to loop through 2D numpy array using x and y coordinates without getting out of bounds error?
  • A 是一个 219 x 219 的起点-终点行程矩阵,其中大多数值为 0,但也有一些值,2% 为非零。在这些非零值中,我希望有 90% 的零值和 10% 的非零值。

标签: python arrays


【解决方案1】:

一种方法是使用np.nonzero 查找非零元素的索引,然后简单地将它们的一部分设置为零:

i = np.nonzero(A)
A[i[0][::10], i[1][::10]] = 0

例如:

In [128]: A = np.random.randint(0, 2, (8,8))

In [129]: A
Out[129]: 
array([[0, 0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 0, 1, 1, 0, 1],
       [0, 0, 0, 1, 0, 1, 1, 0],
       [0, 0, 1, 0, 1, 0, 1, 0],
       [0, 0, 0, 1, 0, 1, 1, 0],
       [1, 1, 0, 0, 0, 1, 0, 1],
       [0, 1, 1, 1, 0, 0, 1, 0],
       [1, 1, 1, 1, 1, 0, 1, 0]])

In [130]: i = np.nonzero(A)

In [131]: A[i[0][::10], i[1][::10]] = 0

In [132]: A
Out[132]: 
array([[0, 0, 0, 1, 1, 1, 1, 0],
       [0, 1, 1, 0, 1, 1, 0, 1],
       [0, 0, 0, 0, 0, 1, 1, 0],
       [0, 0, 1, 0, 1, 0, 1, 0],
       [0, 0, 0, 1, 0, 1, 1, 0],
       [1, 0, 0, 0, 0, 1, 0, 1],
       [0, 1, 1, 1, 0, 0, 1, 0],
       [1, 1, 1, 0, 1, 0, 1, 0]])

这会将第 0、第 10、第 20 等非零索引设置为 0。如果您更喜欢第 9、第 19 等,可以更改偏移量:

A[i[0][10-1::10], i[1][10-1::10]] = 0

【讨论】:

    【解决方案2】:

    这就是我解决问题的方法:

    index=0
    for x in range(0, 219):
        for y in range(0, 219):
            if (index+1) % 10 == 0:
                A[x][y] = 0
            index+=1
    print(A)
    

    对于任何看到它的人: 我有一个 219x219 np.array,我希望将每 10 个非零值替换为零。

    【讨论】:

      【解决方案3】:

      如果我理解您想要正确执行的操作,这应该很容易通过在循环外部设置一个计数器变量来跟踪您到目前为止看到的非零元素的数量和nditer遍历数组:

      # a = np.array(...)
      count = 0
      for x in np.nditer(a, op_flags=['readwrite']):
          if x != 0:
              count += 1
              if count % 10 == 0:
                  x[...] = 0
      

      测试数组:

      [[  0.   1.   2.   3.   4.   0.   5.   6.   0.   7.   0.   0.]
       [  8.   0.   9.   0.  10.   0.  11.  12.   0.   0.   0.   0.]
       [  0.   0.   0.   0.   0.  13.  14.   0.   0.   0.  15.  16.]
       [  0.   0.   0.   0.   0.  17.  18.   0.   0.   0.   0.  19.]
       [ 20.   0.  21.  22.   0.   0.   0.  23.   0.  24.   0.   0.]
       [ 25.   0.   0.  26.  27.   0.  28.   0.  29.   0.   0.   0.]
       [  0.   0.  30.  31.   0.   0.  32.   0.  33.  34.  35.  36.]
       [ 37.   0.   0.   0.  38.   0.  39.   0.  40.  41.   0.   0.]
       [  0.  42.  43.   0.  44.   0.  45.  46.  47.   0.  48.  49.]
       [  0.  50.  51.  52.   0.  53.   0.  54.  55.   0.  56.   0.]]
      

      之后:

      [[  0.   1.   2.   3.   4.   0.   5.   6.   0.   7.   0.   0.]
       [  8.   0.   9.   0.   0.   0.  11.  12.   0.   0.   0.   0.]
       [  0.   0.   0.   0.   0.  13.  14.   0.   0.   0.  15.  16.]
       [  0.   0.   0.   0.   0.  17.  18.   0.   0.   0.   0.  19.]
       [  0.   0.  21.  22.   0.   0.   0.  23.   0.  24.   0.   0.]
       [ 25.   0.   0.  26.  27.   0.  28.   0.  29.   0.   0.   0.]
       [  0.   0.   0.  31.   0.   0.  32.   0.  33.  34.  35.  36.]
       [ 37.   0.   0.   0.  38.   0.  39.   0.   0.  41.   0.   0.]
       [  0.  42.  43.   0.  44.   0.  45.  46.  47.   0.  48.  49.]
       [  0.   0.  51.  52.   0.  53.   0.  54.  55.   0.  56.   0.]]
      

      注意:10、20、30、40 和 50 已更改为 0。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-05
        • 2015-08-24
        • 2021-01-04
        • 2018-01-05
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多