About :
numpy.delete(array, object, axis = None) : returns a new array with the deletion of sub-arrays along with the mentioned axis.
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
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 Program illustrating # numpy.delete() import numpy as geek #Working on 1D arr = geek.arange(5) print("arr : \n", arr) print("Shape : ", arr.shape) # deletion from 1D array object = 2a = geek.delete(arr, object) print("\ndeleteing arr 2 times : \n", a) print("Shape : ", a.shape) object = [1, 2] b = geek.delete(arr, object) print("\ndeleteing arr 3 times : \n", b) print("Shape : ", a.shape) |
Output :
arr : [0 1 2 3 4] Repeating arr 2 times : [0 0 1 1 2 2 3 3 4 4] Shape : (10,) Repeating arr 3 times : [0 0 0 ..., 4 4 4] Shape : (15,)
Code 2 :
# 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) 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) deleteing arr 3 times : [ 0 3 4 5 6 7 8 9 10 11] Shape : (3, 3)
Code 3 : Deletion performed using Boolean Mask
# 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]] = Falseprint("\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-ID. Please run them on your systems to explore the working
.
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python - Read blob object in python using wand library
- MySQL-Connector-Python module in Python
- Reading Python File-Like Objects from C | Python
- Python | PRAW - Python Reddit API Wrapper
- Important differences between Python 2.x and Python 3.x with examples
- twitter-text-python (ttp) module - Python
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Use of min() and max() in Python
- Python PIP
- try-except vs If in Python
- Python if else

