Python Numpy ndarray flatten()














































Python Numpy ndarray flatten()



Description
NumPy generally stands for Numerical Python and it is a general purpose array processing package.it always provides a high performance multidimensional array object. Numpy is usually for creating an array of n dimensions.

Method Name :numpy_array.flatten(order=’C’)
Description :By this function we can flatten a matrix to one dimension in python returns a copy of an
array.

Method Name :matrix.ravel()
Description :By this function we can get the flattened matrix from a given matrix.

In this article, I will tell you about the parameter and its description
Parameter Name: order
Description :'C'− row-major (default. 'F': column-major 'A': flatten in column-major order, if a is Fortran contiguous in memory, row-major order otherwise 'K': flatten a in the order the elements occur in the memory.

Return :Flattened 1-D matrix


The first step would be to give syntax
command:
numpy_array.flatten(order=’C’)

Example :
Code :
import numpy as np a = np.arange(16).reshape(4,4) print ('The original array is:' ) print (a ) print ('\n') # default is column-major print ('The flattened array is:' ) print (a.flatten() ) print ('\n' ) print ('The flattened array in F-style ordering:') print (a.flatten(order = 'F'))

Output :
Example :
Code :
import numpy as np # make a matrix with numpy gfg = np.matrix('[1, 2, 3; 4, 5, 6; 7, 8, -9]') # applying matrix.ravel() method abc = gfg.ravel() print(abc)

Output :


Comments