Matrix Manipulation and Visualization using Numpy and Matplotlib
I.Introduction
Data availability is very great nowadays and We need to process data in certain form including array, table and Matrix. Sometimes we also need Statistical methods to process such data format to get useful information. In another side, We often need to visualise data in form of charts such as Bar chart, Pie chart and Line Chart.
This article talks about Matrix and Visualising it using Numpy and Matplotlib. Matrix is a scalar structure of elements with rows and columns. Matrix A with size from m rows and n columns (m x n) is [1]:
If m=n, the Matrix is called squares matrix. Matrix can be written shortly with :
For example, Matrix B with size 3 x 4 as follows :
II Matrix Arithmetics Operation
II.1 Matrix Summation
Two matrices or more can be summed if the size of each of them is same [1]. For example A = ( 2 x 3) and B = (2 x 3) can be summed of.
II.2 Matrix Multiplication
Two matrices can be multiply if the number of columns of first matrix is same with the number of second matrix [1]. For example matrix M is 2 x 4 and matrix N is 3 x 2, so such matrices can be multiplied with the formula [1] :
For example :
III Numpy for matrix manipulation
Numpy is a Python Library for scientific computing including matrix manipulation. To install numpy, we can use pip on Terminal as follows:
pip install numpy
After succesfully install Numpy, we have to load numpy in our Python code using :
import numpy
We can use alias like np, so the syntax will be :
import numpy as np
Assume the Matrix 1 as follows :
The following code is creating matrix1 :
import numpy as np
matrix1=np.array([[1,5],[5,6]])
matrix1
The output as follows :
III.1 Matrix addition using Numpy
We also able to use Numpy for Matrix addition. For example, there are 2 matrices for addition in figure 4 and we use Numpy for the addition.
The code as follows :
matrix3=np.array([[2,8,19],[7,9,20]])
matrix4=np.array([[5,9,11],[4,10,4]])
c=matrix3+matrix4
c
The output as follows :
III.2 Matrix multiplication using Numpy
Numpy is able to use in Matrix multiplication, for example we use Numpy for Matrix multiplication of figure 6. the code as follows :
K=np.array([[2,1],[1,4]])
M=np.array([[1,3,5],[1,10,5]])
X=np.dot(K,M)
X
While the output as follows :
Not only creating matrix, matrix addition and matrix multiplication, Numpy is also able for other Matrix manipulation such as calculating invers of matrix, determining transpose of matrix and determining determinant of matrix.
IV. Visualizing Matrix
Matplotlib is a library in Python for visualizing. To install it, We use :
pip install matplotlib
Here, we try to visualizing our matrices using dot in . For example we have matrix addition here :
Each mattrix we want to visualize with the code as follows :
import matplotlib.pyplot as plt
matrix_a=np.array([[3,4],[6,7]])
matrix_b=np.array([[12,10],[8,9]])
matrix_c=matrix_a+matrix_b
matrix_c
The output as follows :
Then, We visualize each of matrix with dot using Matplotlib and the code as follows :
plt.plot(matrix_a,"ro")
plt.plot(matrix_b,"go")
plt.plot(matrix_c,"bo")
plt.show()
The Visualization in Figure 13 shows red dots are the element of Matrix a, the green ones are the element of Matrix b and the blue ones are the element of Matrix c which the result summation of Matrix a and Matrix b.
V. Conclusion
Matrix is one of data shape which has rows and columns. The manipulation including summation and multiplication can be used with Numpy, a Python library for Scientific computing and the visualization of elements of matrix can be done by using Matplotlib, a Python Library for data visualization.
VI.Reference :
[1] Rinaldi Munir, Matematika Diskrit , Penerbit Informatika, 2012