2
votes

I want to insert zeros in a numpy array but in a different way that the numpy.insert function works. The numpy.insert function inserts every element before the position of the given index without caring about the other elements included in the same insertion. That is, the indexs given to the function refer to positions before the insertion. I would like a function that considers the indices after the insertion. Let me give an example:

If the input array is array(1, 2, 3, 4) and the indices are [2, 5], the output array should be array(1, 2, 0, 3, 4, 0).

A non-vectorized way to do it would be:

for index in indices:
    input_array = np.insert(input_array, index, 0)

I am looking for a vectorized way to do this, but I cannot find it.

2
What if indices = np.array([2, 6])? - Divakar
Then it makes no sense from my point of view, as you would be asking for the seventh element of an array of length 6 to be 0, and such an element does not exist. - David Masip
Posted timings in my post, if you are interested. - Divakar

2 Answers

1
votes

Here is a vectorized approach that creates an array of zeros and replaces the items accordingly:

In [99]: def insert(arr, ind):
    ...:     new_size = arr.size + ind.size
    ...:     new = np.zeros(new_size)
    ...:     new[~np.in1d(np.arange(new_size), ind)] = arr
    ...:     return new

Demo:

In [100]: arr = np.array([1, 2, 3, 4])

In [101]: ind = np.array([2, 5])

In [102]: 

In [102]: insert(arr, ind)
Out[102]: array([1., 2., 0., 3., 4., 0.])
1
votes

Here's one vectorized way with masking -

def insert_zeros(input_array, indices):
    n = len(input_array) + len(indices)
    mask = np.ones(n, dtype=bool)
    mask[indices] = 0
    out = mask.astype(input_array.dtype)
    out[mask] = input_array
    return out

Sample run -

In [141]: input_array = np.array([1, 2, 3, 4])
     ...: indices  = np.array([2, 5])

In [142]: insert_zeros(input_array, indices)
Out[142]: array([1, 2, 0, 3, 4, 0])

Benchmarking

Scaling up the given sample by 10,000x on a random numbers array and indices for the timing setup and timing the two posted solutions thus far -

In [150]: np.random.seed(0)
     ...: input_array = np.random.randint(0,9,(40000))
     ...: indices = np.sort(np.random.choice(range(len(input_array)), 20000, replace=0))

In [151]: %timeit insert(input_array, indices) # @Kasramvd's soln
     ...: %timeit insert_zeros(input_array, indices) # Posted in this soln
100 loops, best of 3: 4.58 ms per loop
1000 loops, best of 3: 280 µs per loop