{"id":27598,"date":"2022-08-16T00:02:10","date_gmt":"2022-08-15T18:32:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=27598"},"modified":"2022-08-16T00:02:10","modified_gmt":"2022-08-15T18:32:10","slug":"python-numpy-matrix-flatten-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-matrix-flatten-function\/","title":{"rendered":"Python Numpy matrix.flatten() Function"},"content":{"rendered":"

NumPy Library\u00a0<\/strong><\/p>\n

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.<\/p>\n

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype\u2019s values.<\/p>\n

Numpy matrix.flatten() Function:<\/strong><\/p>\n

We can flatten a given matrix using the matrix.flatten() method of the Numpy module, which returns a one-dimensional matrix as an output.<\/p>\n

Syntax:<\/strong><\/p>\n

 matrix.flatten()<\/pre>\n

Return Value:<\/strong><\/p>\n

The flattened 1-Dimensional matrix from a given matrix is returned by the flatten() function.<\/p>\n

What is a Flattened matrix?<\/strong><\/p>\n

Flattening a matrix means flattening the given n-dimensional matrix to a one-Dimensional(1D) matrix.<\/p>\n

Example:<\/strong><\/p>\n

Let the matrix be:<\/p>\n

[[ 1 2 3]\r\n [ 4 5 6]\r\n [ 7 8 9]]<\/pre>\n

Matrix after Flattening:<\/strong><\/p>\n

[1 2 3 4 5 6 7 8 9]<\/pre>\n

Numpy matrix.flatten() Function in Python<\/h2>\n

For 2-Dimensional (2D) Matrix<\/strong><\/p>\n

Approach:<\/strong><\/p>\n