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 in 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.cumsum() Function:
Using the matrix.cumsum() function of the NumPy module, we can find the cumulative sum of a given matrix and returns the output as a 1-Dimensional matrix.
Syntax:
matrix.cumsum()
Return Value:
The cumulative sum of a given matrix is returned by the cumsum() function.
Cumulative sum:
The cumulative sum denotes “how much so far.” The cumulative sum is defined as the sum of a given sequence that grows or increases with more additions.
Numpy matrix.cumsum() Function in Python
For 1-Dimensional (1D) Matrix
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
- Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix.
- Store it in another variable
- Print the cumulative sum of 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(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('[1, 4, 2]') # Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix. # Store it in another variable rslt = gvn_matrx.cumsum() # Print the cumulative sum of a given matrix. print("The cumulative sum of a given matrix:") print(rslt)
Output:
The cumulative sum of a given matrix: [[1 5 7]]
Explanation:
Here it prints 1, adds 1+4 = 5, and adds that result to the next element i.e, 5+2=7
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
- Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix.
- Store it in another variable
- Print the cumulative sum of 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 an argument to it and store it in a variable gvn_matrx = np.matrix('[1, 2; 4, 5]') # Apply cumsum() function on the given matrix to get the cumulative sum of a given matrix. # Store it in another variable rslt = gvn_matrx.cumsum() # Print the cumulative sum of a given matrix. print("The cumulative sum of a given matrix:") print(rslt)
Output:
The cumulative sum of a given matrix: [[ 1 3 7 12]]