Python Numpy matrix.ptp() 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.ptp() Function:

Using the matrix.ptp() method of the Numpy module, we can obtain the peek-to-peek value i.e, is (maximum-minimum) along the axis from a specified matrix.

Syntax:

 matrix.ptp(axis)

Return Value:

The peek-to-peek value from a given matrix is returned by the ptp() function.

Numpy matrix.ptp() Function in Python

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 arguments to it and store it in a variable
  • Print the given matrix
  • Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
  • Here 0 represents along the column, and 1 represents a row.
  • Store it in another variable
  • Print the peek-to-peek value along the specified axis from a given matrix.
  • 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 arguments to it and store it in a variable
gvn_matrx = np.matrix('[10, 1; 6, 3]')
# Print the given matrix
print("The given matrix is:\n", gvn_matrx)    

# Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix
# to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
# Here 0 represents along column, 1 represents row.
# Store it in another variable
rslt = gvn_matrx.ptp(1)
# Print the peek-to-peek value along the specified axis from a given matrix.
print("The peek-to-peek value along the specified axis from a given matrix:")
print(rslt)

Output:

The given matrix is:
[[10 1]
 [ 6 3]]
The peek-to-peek value along the specified axis from a given matrix:
[[9]
 [3]]

Explanation:

Here it prints 10-1 = 9,  6-3=3 i.e, along the row

NOTE:

Here 0 represents along the column, and 1 represents a row.
but generally 0 = row, 1=column.

For 3-Dimensional (3D) Matrix 

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as arguments to it and store it in a variable
  • Print the given matrix
  • Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
  • Here 0 represents along the column, and 1 represents a row.
  • Store it in another variable
  • Print the peek-to-peek value along the specified axis from a given matrix.
  • The Exit of the Program.

Below is the implementation:

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

# Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix
# to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
# Here 0 represents along column, 1 represents row.
# Store it in another variable
rslt = gvn_matrx.ptp(0)
# Print the peek-to-peek value along the specified axis from a given matrix.
print("The peek-to-peek value along the specified axis from a given matrix:")
print(rslt)

Output:

The given matrix is:
[[ 2 4 1]
 [ 8 7 3]
 [10 9 5]]
The peek-to-peek value along the specified axis from a given matrix:
[[8 5 4]]