5

Say I have a 2-D numpy array A of size 20 x 10.

I also have an array of length 20, del_ind.

I want to delete an element from each row of A according to del_ind, to get a resultant array of size 20 x 9.

How can I do this?

I looked into np.delete with a specified axis = 1, but this only deletes element from the same position for each row.

Thanks for the help

2
  • You might consider masked arrays. Otherwise, numpy is not really designed for this, you will probably have to build a new array. This is because of the way data is stored in strides of memory Commented Nov 23, 2015 at 22:54
  • would you propose a solution using masked arrays or any other method (building a new array or something) as long as it is faster than a for loop? Commented Nov 23, 2015 at 22:56

3 Answers 3

5

You will probably have to build a new array.

Fortunately you can avoid python loops for this task, using fancy indexing:

h, w = 20, 10
A = np.arange(h*w).reshape(h, w)
del_ind = np.random.randint(0, w, size=h)
mask = np.ones((h,w), dtype=bool)
mask[range(h), del_ind] = False
A_ = A[mask].reshape(h, w-1)

Demo with a smaller dataset:

>>> h, w = 5, 4
>>> %paste
A = np.arange(h*w).reshape(h, w)
del_ind = np.random.randint(0, w, size=h)
mask = np.ones((h,w), dtype=bool)
mask[range(h), del_ind] = False
A_ = A[mask].reshape(h, w-1)

## -- End pasted text --
>>> A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])
>>> del_ind
array([2, 2, 1, 1, 0])
>>> A_
array([[ 0,  1,  3],
       [ 4,  5,  7],
       [ 8, 10, 11],
       [12, 14, 15],
       [17, 18, 19]])
Sign up to request clarification or add additional context in comments.

2 Comments

np.delete either uses a keep mask like this, or copies slices - and does so with a lot more code.
actually I was thinking of the same direction but this code is neater than what I wrote. Thank you!
0

Numpy isn't known for inplace edits; it's mainly intended for statically sized matrices. For that reason, I'd recommend doing this by copying the intended elements to a new array.

Assuming that it's sufficient to delete one column from every row:

def remove_indices(arr, indices):
    result = np.empty((arr.shape[0], arr.shape[1] - 1)) 

    for i, (delete_index, row) in enumerate(zip(indices, arr)):
        result[i] = np.delete(row, delete_index)

    return result

Comments

-1

If you need to remove each element of a row according to its index in the row, you can achieve this in Python using NumPy. For example, if you want to delete the maximum element in each row, you can follow the steps below:

import numpy as np

# Your 2D NumPy array
a = np.random.random((10, 3))

# Find indices of max elements along each row
max_idx = a.argmax(axis=1) + (np.arange(a.shape[0]) * a.shape[1])

# Delete elements at specified indices
a = np.delete(a, max_idx).reshape(a.shape[0], -1)

In this example, max_idx is calculated by adding the row-wise maximum indices to the corresponding row indices adjusted for the array shape. The np.delete function is then used to remove elements at these indices, and the array is reshaped to the expected shape.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.