{"id":27552,"date":"2022-08-30T21:06:27","date_gmt":"2022-08-30T15:36:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=27552"},"modified":"2022-08-30T21:06:27","modified_gmt":"2022-08-30T15:36:27","slug":"python-numpy-matrix-partition","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-matrix-partition\/","title":{"rendered":"Python Numpy matrix.partition() 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 into 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.partition() Function:<\/strong><\/p>\n Using the matrix.partition() method of the Numpy module, we can partition the matrix in such a manner that the index value that we pass sorts the matrix in such a way that all the values smaller than that value are moved to the left, and others are moved to the right.<\/p>\n Syntax:<\/strong><\/p>\n Return Value:<\/strong><\/p>\n The partitioned matrix is returned by the partition() function.<\/p>\n For 1-Dimensional (1D) Matrix(array)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/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 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.partition(index)<\/pre>\n
Numpy matrix.partition() 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(1-Dimensional) using the matrix() function of numpy module by passing \r\n# some random 1D matrix as an argument to it and store it in a variable\r\ngvn_matrx = np.matrix('[2, 1, 6, 4]')\r\n# Print the given matrix\r\nprint(\"The given matrix is:\") \r\nprint(gvn_matrx) \r\n\r\n# Apply partition() function on the given matrix by passing some random index value as an argument to it\r\n# to partition the given matrix.\r\n# Here index value = 3, the value at the 3rd index is 4. The values less than 4 (i.e, 1,2) are \r\n# moved to the left and others (6) are moved to the right\r\ngvn_matrx.partition(3)\r\n# Print the given matrix after partition.\r\nprint(\"The given matrix after partition is:\")\r\nprint(gvn_matrx)<\/pre>\n
The given matrix is:\r\n[[2 1 6 4]]\r\nThe given matrix after partition is:\r\n[[1 2 4 6]]<\/pre>\n
Here index value = 3, the value at the 3rd index is 4. \r\nThe values less than 4 (i.e, 1 and 2) are moved to the left and \r\nother number 6 is moved to the right<\/pre>\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('[10, 1; 2, 7]')\r\n# Print the given matrix\r\nprint(\"The given matrix is:\") \r\nprint(gvn_matrx) \r\n\r\n# Apply partition() function on the given matrix by passing some random index value as an argument to it\r\n# to partition the given matrix.\r\ngvn_matrx.partition(1)\r\n# Print the given matrix after partition.\r\nprint(\"The given matrix after partition is:\")\r\nprint(gvn_matrx)<\/pre>\n
The given matrix is:\r\n[[10 1]\r\n [ 2 7]]\r\nThe given matrix after partition is:\r\n[[ 1 10]\r\n [ 2 7]]<\/pre>\n","protected":false},"excerpt":{"rendered":"