numpy.ndarray.flat() : 1_D iterator over N-dimensional arrays.
It is not a subclass of, Python’s built-in iterator object, otherwise it a numpy.flatiter instance.
Parameters :
index : [tuple(int)] index of the values to iterate
Return :
1-D iteration of array
Code 1 : Working on 2D array
# Python Program illustrating # working of ndarray.flat() import numpy as geek # Working on 1D iteration of 2D array array = geek.arange(15).reshape(3, 5) print("2D array : \n",array ) # Using flat() : 1D iterator over range print("\nUsing Array : ", array.flat[2:6]) # Using flat() to Print 1D repersented array print("\n1D representation of array : \n ->", array.flat[0:15]) |
Output :
2D array : [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] Using Array : [2 3 4 5] 1D representation of array : -> [ 0 1 2 ..., 12 13 14]
Code 2 : Changing the values of array
# Python Program illustrating # working of ndarray.flat() import numpy as geek # Working on 1D iteration of 2D array array = geek.arange(15).reshape(3, 5) print("2D array : \n",array ) # All elements set to 1 array.flat = 1print("\nAll Values set to 1 : \n", array) array.flat[3:6] = 8array.flat[8:10] = 9print("Changing values in a range : \n", array) |
Output :
2D array : [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] All Values set to 1 : [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] Changing values in a range : [[1 1 1 8 8] [8 1 1 9 9] [1 1 1 1 1]]
What actually numpy.flatiter is ?
A flatiter iterator is returned by x.flat for any array x. It allows iterating(in row major manner)over N-dimensional arrays, either in a for-loop or by calling its next method.
Code 3 : Role of numpy.flatitter()
# Python Program illustrating # working of ndarray.flat() import numpy as geek # Working on 1D iteration of 2D array array = geek.arange(15).reshape(3, 5) print("2D array : \n",array ) print("\nID array : \n", array.flat[0:15]) print("\nType of array,flat() : ", type(array.flat)) for i in array.flat: print(i, end = ' ') |
Output :
2D array : [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] ID array : [ 0 1 2 ..., 12 13 14] Type of array,flat() : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
References :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.html#numpy.ndarray.flat
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
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Reading Python File-Like Objects from C | Python
- twitter-text-python (ttp) module - Python
- Python | PRAW - Python Reddit API Wrapper
- Python | Merge Python key values to list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- bin() in Python
- set add() in python
- try-except vs If in Python
- chr() in Python

