# Python Numpy matrix.partition() Function

NumPy Library

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.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.partition() Function:

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.

Syntax:

 matrix.partition(index)

Return Value:

The partitioned matrix is returned by the partition() function.

## Numpy matrix.partition() Function in Python

For 1-Dimensional (1D) Matrix(array)

Approach:

• Import numpy module using the import keyword
• Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
• Print the given matrix
• Apply partition() function on the given matrix by passing some random index value as an argument to it to partition the given matrix.
• Here index value = 3, and the value at that 3rd index is 4. The values less than 4 (i.e, 1,2) are moved to the left, and others (6) are moved to the right.
• Print the given matrix after partition.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 4]')
# Print the given matrix
print("The given matrix is:")
print(gvn_matrx)

# Apply partition() function on the given matrix by passing some random index value as an argument to it
# to partition the given matrix.
# Here index value = 3, the value at the 3rd index is 4. The values less than 4 (i.e, 1,2) are
# moved to the left and others (6) are moved to the right
gvn_matrx.partition(3)
# Print the given matrix after partition.
print("The given matrix after partition is:")
print(gvn_matrx)

Output:

The given matrix is:
[[2 1 6 4]]
The given matrix after partition is:
[[1 2 4 6]]

Explanation:

Here index value = 3, the value at the 3rd index is 4.
The values less than 4 (i.e, 1 and 2) are moved to the left and
other number 6 is moved to the right

For 2-Dimensional (2D) Matrix

Approach:

• Import numpy module using the import keyword
• Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
• Print the given matrix
• Apply partition() function on the given matrix by passing some random index value as an argument to it to partition the given matrix.
• Print the given matrix after partition.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[10, 1; 2, 7]')
# Print the given matrix
print("The given matrix is:")
print(gvn_matrx)

# Apply partition() function on the given matrix by passing some random index value as an argument to it
# to partition the given matrix.
gvn_matrx.partition(1)
# Print the given matrix after partition.
print("The given matrix after partition is:")
print(gvn_matrx)

Output:

The given matrix is:
[[10 1]
[ 2 7]]
The given matrix after partition is:
[[ 1 10]
[ 2 7]]