NumPy- delete

NumPy- delete

In this chapter, we will discuss about NumPy delete. This function returns a new array with the specified subarray deleted from the input array. As in case of insert() function, if the axis parameter is not used, the input array is flattened. The function takes the following parameters −

Numpy.delete(arr, obj, axis)

Where,

Sr.No.Parameter & Description
1arrInput array
2objCan be a slice, an integer or array of integers, indicating the subarray to be deleted from the input array
3axisThe axis along which to delete the given subarray. If not given, arr is flattened

Example Of NumPy delete

import numpy as np 
a = np.arange(12).reshape(3,4) 

print 'First array:' 
print a 
print '\n'  

print 'Array flattened before delete operation as axis not used:' 
print np.delete(a,5) 
print '\n'  

print 'Column 2 deleted:'  
print np.delete(a,1,axis = 1) 
print '\n'  

print 'A slice containing alternate values from array deleted:' 
a = np.array([1,2,3,4,5,6,7,8,9,10]) 
print np.delete(a, np.s_[::2])

Its output would be as follows −

First array:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

Array flattened before delete operation as axis not used:
[ 0 1 2 3 4 6 7 8 9 10 11]

Column 2 deleted:
[[ 0 2 3]
 [ 4 6 7]
 [ 8 10 11]]

A slice containing alternate values from array deleted:
[ 2 4 6 8 10]

Next Topic – Click Here

This Post Has 3 Comments

Leave a Reply