0

I have been trying that but didn't work.

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
  • What is `A` in your example? – Gabriel Aug 05 '18 at 22:49
  • Can you give some example input and output? "Every tenth (non-zero) value" is ambiguous and also something completely different from what your code is doing. You may find this helpful: [How to loop through 2D numpy array using x and y coordinates without getting out of bounds error?](https://stackoverflow.com/q/30499857) – Bernhard Barker Aug 05 '18 at 22:53
  • A is a 219 by 219 origin-destination trip matrix where most of the values are 0s but there are some values, 2% which are non zeros. Of those nonzeros I would like to have 90% of zeros and 10% of nonzeros. – Kacper Rossa Aug 05 '18 at 23:17

3 Answers3

1

One way would be to use np.nonzero to find the indices of the nonzero elements, and then simply set a slice of them to zero:

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

For example:

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]])

This sets the 0th, 10th, 20th, etc. nonzero indices to 0. If you'd prefer the 9th, 19th, and so on, you can change the offset:

A[i[0][10-1::10], i[1][10-1::10]] = 0
DSM
  • 342,061
  • 65
  • 592
  • 494
0

This is how I solved the problem:

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)

For anyone looking at that: I had a 219x219 np.array where I wanted to have every 10th nonzero value replaced with zero.

0

If I'm understanding what you want to do correctly, this should be fairly easy to do by having a counter variable outside the loop to keep track of how many non-zero elements you've seen so far and nditer to iterate over the array:

# 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

Test array:

[[  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.]]

After:

[[  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.]]

Note: 10, 20, 30, 40 and 50 have been changed to 0.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138