In this blog we will learn some important and most useful topics of linear algebra matrix using "Sumpy" python library with the help of examples:
In this we will apply some important topics like join, shap(), transpose of matrix and other.
Before start if first we need to import library of it.
We are now ready to use the Sympy library features .
Now we create a matrix using matrix() function, here we tells with python sumpy library in python using:
To do this, we simply type sp.Matrix()
Here we will create a matrix that contains two rows: the first one will contain elements 0 and 1 and the second will contain elements 5 and 6.
Example:
X = sp.Matrix( [ 0,1],[5,6] ] )
X # Show
Output:
[1 3
2 4]
Matrix Features
.shape attribute
Use to check the size of a matrix.
Example 1:
print(X.shape)
Output:
(2, 2) #first number represent number of rows and second represent number of columns
Example 2:
B = sp.Matrix( [ [1,7,3,5], [2,4,6,10] ] )
print(B.shape)
Output:
(2, 4)
Multiplication of matrix
Here we can learn it by using some examples which is given below :
Let X is a example of matrix which is given above
3*X
Output:
[3 9
6 12]
Addition of matrix:
Let X is a example of matrix which is given above
3+X
Output:
[4 6
5 7]
As per above we can apply all simple mathematical operation in python matrix.
Transpose of matrix:
Let suppose matrix is given as:
X = sp.Matrix([[1,2],[3,4]])
X
[1 2
3 4]
Now we transpose it by using
X.T
Output:
[1 3
2 4]
Find Selected element as per row and column
Use this code of line to find element as per your selected row and column.
X = [1 3
2 4]
print( X[1,1] ) #find first row and first column index element
Output:
4
or
print( X[1,0] ) #use to find first row and zero index element
Output:
2
or
X[1,:] #find element of first row and all columns indexes
Gaussian Elimination
Suppose two equations which is given as
𝑥+𝑦=10
2𝑥−𝑦=14
We can create the matrix representation of this system and then run the rref() (stands for reduced-row echelon form).
A = sp.Matrix( [ [1, 1, 10], [2, -1, 14] ])
A
Output:
[ 1 1 10
2 -1 14 ]
A.rref()
Output:
([1 0 8
0 1 2], ( 0, 1 ))
Selecting row and column of matrix:
X = sp.Matrix( [ [1, 2, 3], [4, 5, 6] ])
X
Output:
[ 1 2 3
4 5 6 ]
X.row(1)
Output:
[ 1 2 3 ]
Thanks here we completed some basic parts of linear algebra using sumpy, if you want help in advanced topics like swap numbers of matrix, fill value by any value and how to change matrix format by fill any otelementsmets as per your choice.
Other python important blogs which is also useful:
If you need any other help related to Data Science then please contact at codersarts website by clicking here
If you have any query then please ask through comments sections, which is given below
Kommentare