How to Remove Specific Elements in a Numpy Array

How to remove specific elements in a numpy array

Use numpy.delete() - returns a new array with sub-arrays along an axis deleted

numpy.delete(a, index)

For your specific question:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
index = [2, 3, 6]

new_a = np.delete(a, index)

print(new_a) #Prints `[1, 2, 5, 6, 8, 9]`

Note that numpy.delete() returns a new array since array scalars are immutable, similar to strings in Python, so each time a change is made to it, a new object is created. I.e., to quote the delete() docs:

"A copy of arr with the elements specified by obj removed. Note that
delete does not occur in-place
..."

If the code I post has output, it is the result of running the code.

How to add or remove a specific element from a numpy 2d array?

A numpy array (ndarray) is quote:

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size.

So you cannot have rows of different lengths if you want to use the ndarray data structure (with all of its optimizations).

A possible workaround is to have an array of lists

>>> arr=np.array([
[1,2,3],
[4,5,6],
[7,8,9],
[]
])

(note the empty row to escape the ndarray datatype)

so you can delete an element from one of the lists

>>> arr
array([list([1, 2, 3]), list([4, 5, 6]), list([7, 8, 9]), list([])],
dtype=object)
>>> arr[1]=np.delete(arr[1], [1], axis=0)
>>> arr
array([list([1, 2, 3]), array([4, 6]), list([7, 8, 9]), list([])],
dtype=object)

removing elements from a numpy array based on elements of another array

Just make use of argwhere() method to find the indices of those values of 'b' that are present in 'a' and isin() method to check the values inside 'b' is present in 'a':-

indices=np.argwhere(np.isin(a,b))

Finally just delete those values by using delete() method:-

a=np.delete(a,indices)

Now if you print a you will get your desired output:-

array([25,  2, 49, 90, 24,  9])

Removing specific elements from an array in Python

You can use numpy.ravel_multi_index to convert your 2D indices into a flatten index, then delete them from T:

idx = [(0,2),(1,0),(1,1)]

drop = np.ravel_multi_index(np.array(idx).T, dims=inv_r.shape)
# array([2, 3, 4])

T = np.delete(inv_r.flatten(), drop)

output:

array([6080.0941, 7990.7128, 7144.9188, 6211.801 , 8886.9614, 9183.5812])

Delete an element of certain value in numpy array once

You can simply choose one of the indices:

In [3]: np.delete(a, np.where(a == 8)[0][0])
Out[3]: array([1, 1, 2, 6, 8, 8, 9])

Delete some elements from numpy array

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]

Remove specific elements from a numpy array

You should be able to do something like:

import numpy as np

data = [3,2,1,0,10,5]
bad_list = [1, 2]
data = np.asarray(data)
new_list = np.asarray([x for x in data if x not in bad_list])

print("BAD")
print(data)
print("GOOD")
print(new_list)

Yields:

BAD
[ 3 2 1 0 10 5]
GOOD
[ 3 0 10 5]

It is impossible to tell for sure since you did not provide sample data, but the following implementation using your variables should work:

import numpy as np

textcodes= data['CODES'].unique()
sep_list = np.array(['SPCFC_CODE_1','SPCFC_CODE_2','SPCFC_CODE_3','SPCFC_CODE_4'])

final_list = [x for x in textcodes if x not in sep_list]

Is there a way to remove specific elements in an array using numpy functions?

Yes, this is part of numpy's magic indexing. You can use comparison operator or the apply function to produce an array of booleans, with True for the ones to keep and False for the ones to toss. So, for example, to keep all the elements less than 5::

selections = array < 5
array = array[selections]

That will only keep the elements where selections is True.

Of course, since all your values are floats, they aren't going to be divisible by an integer k, but that's another story.

How to delete an object from a numpy array without knowing the index

You can find the index/indices of the object using np.argwhere, and then delete the object(s) using np.delete.

Example:

x = np.array([1,2,3,4,5])
index = np.argwhere(x==3)
y = np.delete(x, index)
print(x, y)


Related Topics



Leave a reply



Submit