Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete some elements from numpy array

Tags:

python

numpy

One interesting question:

I would like to delete some elements from a numpy array but just as below simplified example code, it works if didn't delete the last element, but it failure if we wish to delete the last element. Below code works fine:

import numpy as np

values = np.array([0,1,2,3,4,5])
print values
for i in [3,4,1]:
    values = np.delete(values,i)
print values

The output is:

[0 1 2 3 4 5]
[0 2 4]

If we only change 4 to 5, then it will fail:

import numpy as np

values = np.array([0,1,2,3,4,5])
print values
for i in [3,5,1]:
    values = np.delete(values,i)
print values

The error message:

IndexError: index 5 is out of bounds for axis 0 with size 5

Why this error only happen if delete the last element? what's correct way to do such tasks?

like image 798
lucky1928 Avatar asked Feb 27 '16 16:02

lucky1928


People also ask

How do you remove specific elements from an array in Python?

You can use the pop() method to remove an element from the array.

Can you remove elements from an array?

If you want to remove an item from an array, you can use the pop() method to remove the last element or the shift() method to remove the first element.

How do you remove an element from a NumPy array based on condition?

Summary: The most straightforward way to remove an element at a given index from a NumPy array is to call the function np. delete(array, index) that returns a new array with the element removed.

How do I delete a specific row in NumPy?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.


2 Answers

Keep in mind that np.delete(arr, ind) deletes the element at index ind NOT the one with that value.

This means that as you delete things, the array is getting shorter. So you start with

values = [0,1,2,3,4,5]
np.delete(values, 3) 
[0,1,2,4,5]  #deleted element 3 so now only 5 elements in the list
#tries to delete the element at the fifth index but the array indices only go from 0-4
np.delete(values, 5) 

One of the ways you can solve the problem is to sort the indices that you want to delete in descending order (if you really want to delete the array).

inds_to_delete = sorted([3,1,5], reverse=True) # [5,3,1]
# then delete in order of largest to smallest ind

Or:

inds_to_keep = np.array([0,2,4])
values = values[inds_to_keep]
like image 174
Garrett R Avatar answered Sep 30 '22 16:09

Garrett R


A probably faster way (because you don't need to delete every single value but all at once) is using a boolean mask:

values = np.array([0,1,2,3,4,5])
tobedeleted = np.array([False, True, False, True, False, True])
# So index 3, 5 and 1 are True so they will be deleted.
values_deleted = values[~tobedeleted]
#that just gives you what you want.

It is recommended on the numpy reference on np.delete

To your question: You delete one element so the array get's shorter and index 5 is no longer in the array because the former index 5 has now index 4. Delete in descending order if you want to use np.delete.

If you really want to delete with np.delete use the shorthand:

np.delete(values, [3,5,1])

If you want to delete where the values are (not the index) you have to alter the procedure a bit. If you want to delete all values 5 in your array you can use:

values[values != 5]

or with multiple values to delete:

to_delete = (values == 5) | (values == 3)  | (values == 1)
values[~to_delete]

all of these give you the desired result, not sure how your data really looks like so I can't say for sure which will be the most appropriate.

like image 36
MSeifert Avatar answered Sep 30 '22 16:09

MSeifert