Removing every nth element in an array

Question:

How do I remove every nth element in an array?

import numpy as np

x = np.array([0,10,27,35,44,32,56,35,87,22,47,17])
n = 3  # remove every 3rd element

…something like the opposite of x[0::n]? I’ve tried this, but of course it doesn’t work:

for i in np.arange(0,len(x),n):
    x = np.delete(x,i)
Asked By: Medulla Oblongata

||

Answers:

You’re close… Pass the entire arange as subslice to delete instead of attempting to delete each element in turn, eg:

import numpy as np

x = np.array([0,10,27,35,44,32,56,35,87,22,47,17])
x = np.delete(x, np.arange(0, x.size, 3))
# [10 27 44 32 35 87 47 17]
Answered By: Jon Clements

I just add another way with reshaping if the length of your array is a multiple of n:

import numpy as np

x = np.array([0,10,27,35,44,32,56,35,87,22,47,17])
x = x.reshape(-1,3)[:,1:].flatten()
# [10 27 44 32 35 87 47 17]

On my computer it runs almost twice faster than the solution with np.delete (between 1.8x and 1.9x to be honnest).

You can also easily perfom fancy operations, like m deletions each n values etc.

Answered By: Remy F

Here’s a super fast version for 2D arrays: Remove every m-th row and n-th column from a 2D array (assuming the shape of the array is a multiple of (n, m)):

array2d = np.arange(60).reshape(6, 10)
m, n = (3, 5)
remove = lambda x, q: x.reshape(x.shape[0], -1, q)[..., 1:].reshape(x.shape[0], -1).T

remove(remove(array2d, n), m)

returns:

array([[11, 12, 13, 14, 16, 17, 18, 19],
       [21, 22, 23, 24, 26, 27, 28, 29],
       [41, 42, 43, 44, 46, 47, 48, 49],
       [51, 52, 53, 54, 56, 57, 58, 59]])

To generalize for any shape use padding or reduce the input array depending on your situation.


Speed comparison:

from time import time

'remove'
start = time()
for _ in range(100000):
    res = remove(remove(array2d, n), m)
time() - start

'delete'
start = time()
for _ in range(100000):
    tmp = np.delete(array2d, np.arange(0, array2d.shape[0], m), axis=0)
    res = np.delete(tmp, np.arange(0, array2d.shape[1], n), axis=1)
time() - start

"""
'remove'
0.3835930824279785
'delete'
3.173515558242798
"""

So, compared to numpy.delete the above method is significantly faster.

Answered By: isCzech
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.