What is the numpy.delete() function in Python?

Overview

The delete() function in Python is used to delete the sub-arrays of a given array along a specified axis.

Syntax

The delete() function takes the syntax below:

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

Parameter value

The delete() function takes the following parameter values:

  • arr: This is the input array. It is a required parameter.
  • obj: This specifies the index number or position of the sub-array of a to be deleted. It is a required parameter.
  • axis: This is the axis along which the sub-array of the input array is to be deleted. It is an optional parameter.

Return value

The delete() function returns a copy of the input array, a , except that the elements specified by obj are removed.

Example

import numpy as np
# creating an input array
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
# calling the tile() function
my_array = np.delete(a, 1, axis=0)
# printing the input array and the new array
print(a)
print(my_array)

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an input array, a, using the array() function.
  • Line 6: We call the delete()function on the input array. The result is assigned to a variable, my_array.
  • Line 9: We print the new array, my_array.