numpy.ndarray.flatten() Function Examples
There are various examples of numpy.ndarray.flatten()
function, here we are discussing some generally used examples of numpy.ndarray.flatten()
Function those are following.
- Numpy Flatten Function
- numpy.ndarray.flatten() in Fortran Order
- Concatenate Flattened Arrays
- Initialize a Flattened Array with Zeros
- Find Maximum Value in Flattened Array
Numpy Flatten Function
In this example code uses the numpy library to create a 2D array ‘arr’. The `flatten()` function is then applied to ‘arr’, converting it into a 1D array ‘gfg’, which is printed. The result is a flattened version of the original 2D array.
Python3
# importing numpy as geek import numpy as geek arr = geek.array([[ 5 , 6 ], [ 7 , 8 ]]) gfg = arr.flatten() print ( gfg ) |
Output :
[5 6 7 8]
numpy.ndarray.flatten() in Fortran Order
In this example This code uses the NumPy library to create a 2×2 array ‘arr’. The `flatten(‘F’)` function is then applied to flatten the array in column-major order (‘F’) and the result is printed.
Python3
# importing numpy as geek import numpy as geek arr = geek.array([[ 5 , 6 ], [ 7 , 8 ]]) gfg = arr.flatten( 'F' ) print ( gfg ) |
Output :
[5 6 7 8]
Concatenate Flattened Arrays
In this example code uses NumPy to create two 2D arrays, `array1` and `array2`. It then flattens both arrays and concatenates them into a single 1D array named `concatenated_array`. Finally, it prints the original arrays and the concatenated result.
Python3
import numpy as np # Create two 2D arrays array1 = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) array2 = np.array([[ 7 , 8 , 9 ], [ 10 , 11 , 12 ]]) # Flatten the arrays and concatenate them concatenated_array = np.concatenate((array1.flatten(), array2.flatten())) print ( "Array 1:" ) print (array1) print ( "\nArray 2:" ) print (array2) print ( "\nConcatenated Array:" ) print (concatenated_array) |
Output :
Array 1:
[[1 2 3]
[4 5 6]]
Array 2:
[[ 7 8 9]
[10 11 12]]
Concatenated Array:
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Initialize a Flattened Array with Zeros
In this example code uses the NumPy library to create a 2D array named `original_array`. It then flattens this array and creates a new flattened array called `flattened_zeros` with the same shape, initialized with zeros. Finally, it prints both the original 2D array and the flattened array filled with zeros.
Python3
import numpy as np # Create a 2D array original_array = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) # Flatten the array and initialize a new flattened array with zeros flattened_zeros = np.zeros_like(original_array.flatten()) print ( "Original Array:" ) print (original_array) print ( "\nFlattened Zeros Array:" ) print (flattened_zeros) |
Output :
Original Array:
[[1 2 3]
[4 5 6]]
Flattened Zeros Array:
[0 0 0 0 0 0]
Find Maximum Value in Flattened Array
In this example The code uses NumPy to create a 3×3 array named `original_array`. It then flattens the array, finds the maximum value in the flattened version, and prints the original array along with the maximum value.
Python3
import numpy as np # Create a 3x3 array original_array = np.array([[ 4 , 12 , 8 ], [ 5 , 9 , 10 ], [ 7 , 6 , 11 ]]) # Flatten the array and find the maximum value max_value = original_array.flatten(). max () print ( "Original Array:" ) print (original_array) print ( "\nMaximum Value in Flattened Array:" , max_value) |
Output:
Original Array:
[[ 4 12 8]
[ 5 9 10]
[ 7 6 11]]
Maximum Value in Flattened Array: 12
Numpy ndarray.flatten() function | Python
In this article, we will explore the syntax, definition, and usage of the NumPy `ndarray.flatten()` function. We will provide a comprehensive explanation along with an illustrative example to enhance understanding.
Contact Us