{"id":27532,"date":"2022-08-14T14:27:45","date_gmt":"2022-08-14T08:57:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=27532"},"modified":"2022-08-14T14:27:45","modified_gmt":"2022-08-14T08:57:45","slug":"python-numpy-matrix-ravel-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-matrix-ravel-function\/","title":{"rendered":"Python Numpy matrix.ravel() 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.ravel() Function:<\/strong><\/p>\n We can obtain the flattened matrix from a given matrix using the matrix.ravel() function of the NumPy module.<\/p>\n Syntax:<\/strong><\/p>\n Return Value:<\/strong><\/p>\n The flattened matrix from a given matrix is returned by the ravel() 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 matrix be:<\/p>\n 1\u00a0 \u00a0 \u00a02\u00a0 \u00a0 \u00a03<\/p>\n 4\u00a0 \u00a0 \u00a05\u00a0 \u00a0 \u00a0 6<\/p>\n 7\u00a0 \u00a0 \u00a08\u00a0 \u00a0 \u00a0 9<\/p>\n Matrix after Flattening:<\/p>\n For 2-Dimensional (2D) Matrix<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n For 3-Dimensional (3D) Matrix<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" NumPy Library\u00a0 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 …<\/p>\n matrix.ravel()<\/pre>\n
[1 2 3 4 5 6 7 8 9]<\/pre>\n
Numpy matrix.ravel() Function in Python<\/h2>\n
\n
# Import numpy module using the import keyword\r\nimport numpy as np\r\n \r\n# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing \r\n# some random 2D matrix as an argument to it and store it in a variable\r\ngvn_matrx = np.matrix('[1, 2; 4, 5]')\r\n \r\n# Apply the ravel() function on the given matrix to get the flattened matrix from a given matrix.\r\n# Here, it flattens to a 1-Dimensional matrix.\r\n# Store it in another variable\r\nrslt = gvn_matrx.ravel()\r\n# Print the flattened matrix from a given matrix\r\nprint(\"The flattened matrix from a given matrix:\")\r\nprint(rslt)<\/pre>\n
The flattened matrix from a given matrix:\r\n[[1 2 4 5]]<\/pre>\n
\n
# Import numpy module using the import keyword\r\nimport numpy as np\r\n \r\n# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing \r\n# some random 3D matrix as an argument to it and store it in a variable\r\ngvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')\r\n \r\n# Apply ravel() function on the given matrix to get the flattened matrix from a given matrix.\r\n# Here, it flattens to 1-dimensional matrix\r\n# Store it in another variable\r\nrslt = gvn_matrx.ravel()\r\n# Print the flattened matrix from a given matrix\r\nprint(\"The flattened matrix from a given matrix:\")\r\nprint(rslt)<\/pre>\n
The flattened matrix from a given matrix:\r\n[[ 2 4 1 8 7 3 10 9 5]]<\/pre>\n