The Wayback Machine - https://web.archive.org/web/20240604194415/https://www.geeksforgeeks.org/numpy-delete-python/
Open In App

numpy.delete() in Python

Last Updated : 09 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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  
                flattened array

Return : 

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

Code 1 : Deletion from 1D array 
 

Python





Output : 
 

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

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

deleting arr 3 times : 
 [0 3 4]
Shape :  (4,)

Code 2 : 
 

Python




# Python Program illustrating
# numpy.delete()
 
import numpy as geek
 
#Working on 1D
arr = geek.arange(12).reshape(3, 4)
print("arr : \n", arr)
print("Shape : ", arr.shape)
 
# deletion from 2D array
a = geek.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 = geek.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)

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

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

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

Code 3: Deletion performed using Boolean Mask 
 

Python




# Python Program illustrating
# numpy.delete()
 
import numpy as geek
 
arr = geek.arange(5)
print("Original array : ", arr)
mask = geek.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]

References : 
https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html
Note : 
These codes won’t run on online IDE’s. Please run them on your systems to explore the working 

 



Similar Reads

Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Python | Numpy numpy.ndarray.__ge__()
With the help of numpy.ndarray.__ge__() method of Numpy, We can find that which element in an array is greater then or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ge__($self, value, /) Return: self>=value Example #1 : In this example
1 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__neg__()
With the help of numpy.ndarray.__neg__() method of Numpy, one can multiply each and every element of an array with -1. Hence, the resultant array having values like positive values becomes negative and negative values become positive. Syntax: ndarray.__neg__($self, /) Return: -self Example #1 : In this example we can see that after applying numpy._
1 min read
Python | Numpy numpy.ndarray.__pos__()
With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__pos__(), we get the simple array that can be the sa
1 min read
Python | Numpy numpy.ndarray.__truediv__()
With the help of Numpy numpy.ndarray.__truediv__(), we can divide a particular value that is provided as a parameter in the ndarray.__truediv__() method. Value will be divided to each and every element in a numpy array. Syntax: ndarray.__truediv__($self, value, /) Return: self/value Example #1 : In this example, we can see that each element in an a
1 min read
Practice Tags :