numpy.ndarray.flat – The NumPy ndarray flat Python Function

NumPy ndarray flat Function: Introduction

Over N-dimensional arrays, the numpy.ndarray.flat() method is used as a 1 D iterator. Otherwise, it’s a numpy.flatiter instance that isn’t a subclass of Python’s built-in iterator object.

Syntax :

numpy.ndarray.flat()

Parameters :

  • index : [tuple(int)] index of the values to iterate

Return: 1-D iteration of the array.

NumPy ndarray flat: Code Examples

Example 01: Working on a 2D array.

# welcome to softhunt.net
# Python Program illustrating
# working of ndarray.flat()

import numpy as np

# Working on 1D iteration of 2D array
array = np.arange(15).reshape(5, 3)
print("2D array : \n",array )

# Using flat() : 1D iterator over range
print("\nUsing Array : ", array.flat[2:6])

# Using flat() to Print 1D represented array
print("\n1D representation of array : \n ->", array.flat[0:15])

Output:

NumPy ndarray

Explanation:

  • Line1 – imports the NumPy library
  • Line2 – Working on the 1D iteration of the 2D array
  • Line3 – Prints the Line2 result
  • Line3 – Using flat() 1D iterator over range and print it
  • Line4 – Using flat() to Print 1D represented array and printing it

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Example 02: Changing the values of an array

# welcome to softhunt.net
# Python Program illustrating
# working of ndarray.flat()

import numpy as np

# Working on 1D iteration of 2D array
array = np.arange(15).reshape(5, 3)
print("2D array : \n",array )

# All elements set to 1
array.flat = 1
print("\nAll Values set to 1 : \n", array)

array.flat[3:6] = 8
array.flat[8:10] = 9
print("Changing values in a range : \n", array)

Output:

NumPy ndarray eg2

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What is actually numpy.flatiter is?

For each array x, x.flat returns a flatiter iterator. It allows you to iterate across N-dimensional arrays in a row-major manner. Either in a for-loop or by executing its next function.

Let’s have look at the code example. Which shows role of this function.

Example 03: Role of numpy.flatitter()

# welcome to softhunt.net
# Python Program illustrating
# working of ndarray.flat()

import numpy as np

# Working on 1D iteration of 2D array
array = np.arange(15).reshape(5, 3)
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:

numpy.flatitter()

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

What does flat do in NumPy?

To build a 1-D iterator across the array, use the flat() function. This is a numpy.flatiter instance, which behaves similarly to Python’s built-in iterator object but is not a subclass of it.

Conclusion

That’s all for NumPy ndarray flat function if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.ravel() – The NumPy ravel Python Function

Leave a Comment