 # numpy array exclude some elements

• Last Update :
• Techknowledgy :

To recap, whenever you want to remove an element, call the numpy.delete() function for a given index.,Call the numpy.delete() function on the array for the given index.,One way to remove multiple elements from a NumPy array is by calling the numpy.delete() function repeatedly for a bunch of indices.,Call the numpy.delete() function on the array with the given index sequence.

For example:

```import numpy as np

arr = np.array([1, 2, 3, 4, 5])

index = 0
arr = np.delete(arr, index)

print(arr)```

Output:

`[2 3 4 5]`

The syntax of numpy.delete() is:

`numpy.delete(arr, obj, axis = None)`

For example, let’s remove the first, second, and third elements from an array of strings:

```import numpy as np

arr = np.array(["Alice", "Bob", "Charlie", "David", "Eric"])
arr = np.delete(arr, [0, 1, 2])

print(arr)```

For example, let’s remove the 2nd column from a 2D array:

```import numpy as np

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

arr = np.delete(arr, 1, axis = 1)

print(arr)```

Suggestion : 2

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

`numpy.delete(a, index)`

```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]````

There is a numpy built-in function to help with that.

```import numpy as np
>>>
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>>
b = np.array([3, 4, 7]) >>>
c = np.setdiff1d(a, b) >>>
c
array([1, 2, 5, 6, 8, 9])```

A Numpy array is immutable, meaning you technically cannot delete an item from it. However, you can construct a new array without the values you don't want, like this:

`b = np.delete(a, [2, 3, 6])`

To delete by value :

`modified_array = np.delete(original_array, np.where(original_array == value_to_delete))`

Using `np.delete` is the fastest way to do it, if we know the indices of the elements that we want to remove. However, for completeness, let me add another way of "removing" array elements using a boolean mask created with the help of `np.isin`. This method allows us to remove the elements by specifying them directly or by their indices:

```import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])```

Remove by indices:

```indices_to_remove = [2, 3, 6]
a = a[~np.isin(np.arange(a.size), indices_to_remove)]```

Remove by elements (don't forget to recreate the original `a` since it was rewritten in the previous line):

```elements_to_remove = a[indices_to_remove] #[3, 4, 7]
a = a[~np.isin(a, elements_to_remove)]```

Not being a numpy person, I took a shot with:

```>>>
import numpy as np
>>>
import itertools >>>
>>>
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>>
index = [2, 3, 6] >>>
a = np.array(list(itertools.compress(a, [i not in index
for i in range(len(a))
]))) >>>
a
array([1, 2, 5, 6, 8, 9])```

According to my tests, this outperforms `numpy.delete()`. I don't know why that would be the case, maybe due to the small size of the initial array?

```python - m timeit - s "import numpy as np" - s "import itertools" - s "a = np.array([1,2,3,4,5,6,7,8,9])" - s "index=[2,3,6]"
"a = np.array(list(itertools.compress(a, [i not in index for i in range(len(a))])))"
100000 loops, best of 3: 12.9 usec per loop

python - m timeit - s "import numpy as np" - s "a = np.array([1,2,3,4,5,6,7,8,9])" - s "index=[2,3,6]"
"np.delete(a, index)"
10000 loops, best of 3: 108 usec per loop```

Even more weirdly, passing `numpy.delete()` a list performs worse than looping through the list and giving it single indices.

```python - m timeit - s "import numpy as np" - s "a = np.array([1,2,3,4,5,6,7,8,9])" - s "index=[2,3,6]"
"for i in index:"
"    np.delete(a, i)"
10000 loops, best of 3: 33.8 usec per loop```

Suggestion : 3

Last Updated : 08 Jun, 2022

Syntax:

`numpy.delete(array_name, index_value)`

For example, we are having an array with 5 elements,

`array1 = [1, 2, 3, 4, 5]`

The indexing starts from 0 to n-1. If we want to delete 2, then 2 element index is 1. So, we can specify

`np.delete(array1, 1)`

Output:

```[1 2 3 4 5]
remaining elements after deleting 1 st element[2 3 4 5]```

Suggestion : 4

To delete multiple elements from a numpy array by index positions, pass the numpy array and list of index positions to be deleted to np.delete() i.e.,obj : Index position or list of index positions of items to be deleted from numpy array arr.,To delete a column from a 2D numpy array using np.delete() we need to pass the axis=1 along with numpy array and index of column i.e.,To delete a row from a 2D numpy array using np.delete() we need to pass the axis=0 along with numpy array and index of row i.e. row number,

Python’s Numpy library provides a method to delete elements from a numpy array based on index position i.e.

`numpy.delete(arr, obj, axis = None)`

First of all import numpy module i.e.

`import numpy as np`

Suppose we have a numpy array of numbers i.e.

```# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])```

Output:

```Modified Numpy Array by deleting element at index position 2
[4 5 7 8 9 10 11]```

To delete multiple elements from a numpy array by index positions, pass the numpy array and list of index positions to be deleted to np.delete() i.e.

```# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])

# Delete element at index positions 1, 2 and 3
arr = np.delete(arr, [1, 2, 3])

print('Modified Numpy Array by deleting element at index position 1, 2 &amp; 3')
print(arr)```

Suggestion : 5

Changed in version 1.19.0: Boolean indices are now treated as a mask of elements to remove, rather than being cast to the integers 0 and 1.,Indicate indices of sub-arrays to remove along the specified axis.,A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place. If axis is None, out is a flattened array.,Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].

```>>> arr = np.arange(12) + 1 >>>
mask = np.ones(len(arr), dtype = bool) >>>
mask[[0, 2, 4]] = False >>>
```>>> arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]) >>>
arr
array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]) >>>
np.delete(arr, 1, 0)
array([
[1, 2, 3, 4],
[9, 10, 11, 12]
])```
```>>> np.delete(arr, np.s_[::2], 1)
array([
[2, 4],
[6, 8],
[10, 12]
]) >>>
np.delete(arr, [1, 3, 5], None)
array([1, 3, 5, 7, 8, 9, 10, 11, 12])```

Suggestion : 6

You can use the np.delete() function to remove specific elements from a numpy array based on their index. The following is the syntax:,Note that, technically, numpy arrays are immutable. That is, you cannot change them once they are created. The np.delete() function returns a copy of the original array with the specific element deleted. ,You can remove multiple elements from the array based on their indexes. For this, pass the indexes of elements to be deleted as a list to the np.delete() function. ,Another important use case of removing elements from a numpy array is removing elements based on a condition. Use np.where() to get the indexes of elements to remove based on the required condition(s) and then pass it to the np.delete() function.

You can use the `np.delete()` function to remove specific elements from a numpy array based on their index. The following is the syntax:

```import numpy as np
# arr is a numpy array
# remove element at a specific index
arr_new = np.delete(arr, i)
# remove multiple elements based on index
arr_new = np.delete(arr, [i, j, k])```

Pass the array and the index of the element that you want to delete.

```import numpy as np

# create a numpy array
arr = np.array([1, 3, 4, 2, 5])
# remove element at index 2
arr_new = np.delete(arr, 2)
# display the arrays
print("Original array:", arr)
print("After deletion:", arr_new)```

Output:

```Original array: [1 3 4 2 5]
After deletion: [1 3 2 5]```

You can remove multiple elements from the array based on their indexes. For this, pass the indexes of elements to be deleted as a list to the `np.delete()` function.

```# remove element at index 2, 4
arr_new = np.delete(arr, [2, 4])
# display the arrays
print("Original array:", arr)
print("After deletion:", arr_new)```

Another important use case of removing elements from a numpy array is removing elements based on a condition. Use `np.where()` to get the indexes of elements to remove based on the required condition(s) and then pass it to the `np.delete()` function.

```# create a numpy array
arr = np.array([1, 3, 4, 2, 5])
# remove all even elements from the array
arr_new = np.delete(arr, np.where(arr % 2 == 0))
# display the arrays
print("Original array:", arr)
print("After deletion:", arr_new)```

Suggestion : 7

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.,Create a Numpy array that stores the elements that have to be removed from the given array.,obj represents the index/position or a list of indices of the elements that have to be deleted from the numpy array.,Let’s wrap things up. The most convenient way to remove an element from a Numpy array is to use the Numpy libraries delete() method. The other approaches explained in this tutorial can also be followed to get the desired output. Feel free to use the one that suits you.

Example: Consider the following Numpy array as shown below:

```import numpy as np

arr = np.array([10, 20, 30, 40, 50])```

Expected Output:

`[10 30 50]`

Code:

```import numpy as np

arr = np.array([10, 20, 30, 40, 50])
delete_indices = [1, 3]
new_arr = np.delete(arr, delete_indices)
print(new_arr)```

Once the elements have been selected, call the `numpy.delete(arr, obj)` method such that `obj` represents the elements at the indices based on the specified condition.

```import numpy as np

arr = np.array([10, 20, 30, 40, 50])
new_arr = np.delete(arr, np.where((arr == 20) | (arr == 40)))
print(new_arr)```

Ouput:

`[10 30 50]`