numpy.delete() – The NumPy delete Python Function

Introduction to NumPy delete

The numpy.delete() function returns a new array with the deletion of sub-arrays along with the mentioned axis.

Syntax:

numpy.delete(array, object, axis = None)

Parameters:

  • array : [array_like] Input array.
  • object : [int, array of ints] Sub-array to delete
  • axis: Axis along which we want to delete sub-arrays. By default, it object is applied to a flattened array

Return: An array with sub-array being deleted as per the mentioned object along a given axis.

Code Examples of NumPy delete

Example 01: Deletion from 1D array

# welcome to softhunt.net
# Python Program illustrating
# numpy.delete()

import numpy as np

#Working on 1D
arr = np.arange(5)
print("arr : \n", arr)
print("Shape : ", arr.shape)

# deletion from 1D array

object = 2
a = np.delete(arr, object)
print("\ndeleteing {} from array : \n {}".format(object,a))
print("Shape : ", a.shape)

object = [1, 2]
b = np.delete(arr, object)
print("\ndeleteing {} from array : \n {}".format(object,a))
print("Shape : ", a.shape)

Output:

arr : 
 [0 1 2 3 4]
Shape :  (5,)

deleteing 2 from array : 
 [0 1 3 4]
Shape :  (4,)

deleteing [1, 2] from array : 
 [0 1 3 4]
Shape :  (4,)

Example 02:

# welcome to softhunt.net
# Python Program illustrating
# numpy.delete()

import numpy as np

#Working on 1D
arr = np.arange(12).reshape(3, 4)
print("arr : \n", arr)
print("Shape : ", arr.shape)

# deletion from 2D array
a = np.delete(arr, 1, 0)
'''
		[[ 0 1 2 3]
		[ 4 5 6 7] -> deleted
		[ 8 9 10 11]]
'''
print("\ndeleteing arr 2 times : \n", a)
print("Shape : ", a.shape)

# deletion from 2D array
a = np.delete(arr, 1, 1)
'''
		[[ 0 1* 2 3]
		[ 4 5* 6 7]
		[ 8 9* 10 11]]
			^
			Deletion
'''
print("\ndeleteing arr 2 times : \n", a)
print("Shape : ", a.shape)

Output:

arr : 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Shape :  (3, 4)

deleteing arr 2 times : 
 [[ 0  1  2  3]
 [ 8  9 10 11]]
Shape :  (2, 4)

deleteing arr 2 times : 
 [[ 0  2  3]
 [ 4  6  7]
 [ 8 10 11]]
Shape :  (3, 3)

Example 03: Deletion performed using Boolean Mask

# welcome to softhunt.net
# Python Program illustrating
# numpy.delete()

import numpy as np

arr = np.arange(5)
print("Original array : ", arr)
mask = np.ones(len(arr), dtype=bool)

# Equivalent to np.delete(arr, [0,2,4], axis=0)
mask[[0,2]] = False
print("\nMask set as : ", mask)
result = arr[mask,...]
print("\nDeletion Using a Boolean Mask : ", result)

Output:

Original array :  [0 1 2 3 4]

Mask set as :  [False  True False  True  True]

Deletion Using a Boolean Mask :  [1 3 4]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What does NumPy delete do?

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

Why is NumPy faster than for loop?

Because of the following reasons, NumPy Arrays are quicker than Python Lists: A collection of homogenous data types stored in contiguous memory spaces is referred to as an array. A list, on the other hand, is a collection of disparate data types store in non-contiguous memory locations in Python.

Is Julia faster than NumPy?

For small arrays (up to 1000 elements) Julia is actually faster than Python/NumPy. For intermediate size arrays (100,000 elements), Julia is nearly 2.5 times slower (and in fact, without the sum, Julia is up to 4 times slower). Finally, at the largest array sizes, Julia catches up again. (It is unclear to me why; it seems like the Python/NumPy performance should scale linearly above n=100,000, but it does not.)

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.repeat() – The NumPy repeat Python Function

Leave a Comment