Hey, readers! In this article, we will be focusing on Essential 6 NumPy Linear Algebraic functions, in detail.
So, let us begin!! 🙂
Overview of NumPy module – Features
As we all have seen in the previous article of the NumPy module, it has its role spread across various domains such as Machine Learning, Data science, etc.
Altogether, NumPy module offers us various functions to create an Array and work/manipulate data according to the requirement. We can also relate NumPy to mathematical functions we need in day to day tasks for calculations.
In the course of this topic, we will be focusing on the below functions offered by NumPy Array–
- Rank of a matrix
- Inverse array
- Exponential value for an array
- Dot product
- Linear Equations
Let us have a look at them in the upcoming sections.
1. Determinant of a NumPy Array
The basic linear algebraic function that we come across is calculating the Determinant of a matrix. We can calculate the determinant using the NumPy module through numpy.linalg.det() function. It treats an array as a matrix and calculates the determinant of the same.
2. Rank of a matrix
The numpy.linalg.matrix_rank() function enables us to calculate the Rank of a matrix in NumPy. It accepts a NumPy array as an input parameter and returns the rank of the array treating it as a matrix.
3. Inverse of a NumPy Array
With NumPy Array module, we can get the inverse of an array at fingertips using inv() function.
4. Exponential values
With NumPy array we can easily calculate the exponent of an array for a customized power. That is, we can raise a number as power to the array (matrix) and fetch the value for exponent of an array.
Implementation — Matrix functions in NumPy
Let us now try to implement the above discussed functions through the below example–
import numpy x = numpy.array([ [1, 2, 3], [4, 5, 6], [7, -2, 9]]) print("Rank: ", numpy.linalg.matrix_rank(x)) det_mat = numpy.linalg.det(x) print("\nExponent:\n", numpy.linalg.matrix_power(x, 2)) print("\nDeterminant: ",det_mat) inv_mat = numpy.linalg.inv(x) print("\nInverse: ",inv_mat)
Rank: 3 Exponent: [[ 30 6 42] [ 66 21 96] [ 62 -14 90]] Determinant: -59.999999999999986 Inverse: [[-0.95 0.4 0.05 ] [-0.1 0.2 -0.1 ] [ 0.71666667 -0.26666667 0.05 ]]
5. Dot product with NumPy Array
NumPy Array module enables us to have product operations in place easily for 1-D as well as multi-dimensional arrays.
For 1-D arrays, it performs scalar multiplication of arrays. On the other side, for multi-dimensional arrays, it performs array/matrix multiplication of the array elements.
We make use of numpy.dot() function to get the Dot Product for NumPy Array.
import numpy as np sc_dot = np.dot(5,10) print("Dot Product for 1-D array: ", sc_dot) vectr_x = 1 - 2j vectr_y = 4 + 8j vctr_dot = np.dot(vectr_x, vectr_y) print("Dot Product for multi-dimensional Array: ", vctr_dot)
Dot Product for 1-D array: 50 Dot Product for multi-dimensional Array: (20+0j)
6. NumPy Linear Equations
Now, solving linear algebraic equations for huge numeric values is not a time taking task any more!! Yes, with NumPy Array module, we can easily get the result for NumPy Linear Equations.
NumPy provides us with numpy.linalg.solve() function that takes array values as input and presents the output for the equation ax=b.
In the below example, we have passed two arrays as input to the solve() function, which in turn returns the linear algebraic output for the same in an array form.
import numpy as np x = np.array([[2, 4], [6, 8]]) y = np.array([2, 2]) print(("Linear equations:", np.linalg.solve(x, y)))
('Linear equations:', array([-1., 1.]))
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Python Programming, Stay tuned with us.
Till then, Happy Learning! 🙂