# Matrix Operations with Python NumPy-I

Matrix Operations : Creation of Matrix

The 2-D array in NumPy is called as Matrix. The following line of code is used to create the Matrix.

>>> import numpy as np   #load the Library

>>> matrix = np.array( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print(matrix)

[[ 4  5  6]

[ 7  8  9]

[10 11 12]]

>>>

Matrix Operations : Describing a Matrix

To describe a matrix we use matrix.shape

>>> matrix = np.array( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print( matrix )

[[ 4  5  6]

[ 7  8  9]

[10 11 12]]

>>> print( matrix.shape )

(3, 3)

Matrix Operations : Matrix Reshaping

We can change the shape of matrix without changing the element of the Matrix by using reshape ().

>>> matrix = np.array( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print ( matrix.reshape ( 1, 9 ) )

[[ 4  5  6  7  8  9 10 11 12]]

>>>

Accessing the Elements of the Matrix with Python

Using following line of codes, we can access particular element, row or column of the matrix.

>>> import numpy as np

>>> matrix = np.array( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print ( “2nd element of 1st row of the matrix = “, matrix [0] [1] )

2nd element of 1st row of the matrix =  5

>>> print ( ” 3d element of 2nd row of the matrix = “, matrix [1] [2] )

3d element of 2nd row of the matrix =  9

>>> print ( ” last element of the last row of the matrix = “, matrix [-1] [-1] )

last element of the last row of the matrix =  12

>>> print ( “Second row of the matrix = “, matrix [1] )

Second row of the matrix =  [7 8 9]

>>> print ( “First row of the matrix = “, matrix [0] )

First row of the matrix =  [4 5 6]

>>> print ( “Last row of the matrix = “, matrix [-1] )

Last row of the matrix =  [10 11 12]

>>> print ( “Second column of the matrix = “, matrix [:, 1] )

Second column of the matrix =  [ 5  8 11]

>>> print ( “First column of the matrix = “, matrix [:, 0] )

First column of the matrix =  [ 4  7 10]

>>> print ( “Last column of the matrix = “, matrix [:, -1] )

Last column of the matrix =  [ 6  9 12]

Plus, operator (+) is used to add the elements of two matrices.

>>> import numpy as np

>>> X = np.array ( [ [ 8, 10 ], [ -5, 9 ] ] ) #X is a Matrix of size 2 by 2

>>> Y = np.array ( [ [ 2, 6 ], [ 7, 9 ] ] )   #Y is a Matrix of size 2 by 2

>>> Z = X + Y

>>> print (” Addition of Two Matrix : \n “, Z)

[[10 16]

[ 2 18]]

>>>

Multiplication of Matrices

Multiplication operator (*) is used to multiply the elements of two matrices.

>>> import numpy as np

>>> X = np.array ( [ [ 8, 10 ], [ -5, 9 ] ] ) #X is a Matrix of size 2 by 2

>>> Y = np.array ( [ [ 2, 6 ], [ 7, 9 ] ] )   #Y is a Matrix of size 2 by 2

>>> Z = X * Y

>>> print (” Multiplication of Two Matrix : \n “, Z)

Multiplication of Two Matrix :

[[ 16  60]

[-35  81]]

Subtraction of Matrices

Minus operator (-) is used to substract the elements of two matrices.

>>> import numpy as np

>>> X = np.array ( [ [ 8, 10 ], [ -5, 9 ] ] ) #X is a Matrix of size 2 by 2

>>> Y = np.array ( [ [ 2, 6 ], [ 7, 9 ] ] )   #Y is a Matrix of size 2 by 2

>>> Z = X – Y

>>> print ( ” Substraction of Two Matrix : \n “,  Z)

Substraction of Two Matrix :

[[  6   4]

[-12   0]]

>>>

Matrix Dot Products Calculation

The dot product of two matrix can perform with the following line of code.

>>> import numpy as np

>>> matrix1 = np.array( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> matrix2 = np.array( [ [ 1, 2, 1 ], [ 2, 1, 3 ], [ 1, 1, 2 ] ] )

>>> print ( ” The dot product of two matrix :\n”, np.dot ( matrix1 , matrix2 ) )

The dot product of two matrix :

[[20 19 31]

[32 31 49]

[44 43 67]]

>>>

Transpose of a Matrix

It is nothing but the interchange the rows and columns of a Matrix

>>> import numpy as np

>>> matrix = np.array ( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print ( ” Matrix is : \n “, matrix)

Matrix is :

[[ 4  5  6]

[ 7  8  9]

[10 11 12]]

>>> print ( ” Transpose Matrix is : \n “, matrix.T )

Transpose Matrix is :

[[ 4  7 10]

[ 5  8 11]

[ 6  9 12]]

>>>

Accessing the Diagonal of a Matrix

Sometime we are only interested in diagonal element of the matrix, to access it we need to write following line of code.

>>> import numpy as np

>>> matrix = np.array ( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print ( ” Matrix is : \n “, matrix)

Matrix is :

[[ 4  5  6]

[ 7  8  9]

[10 11 12]]

>>> print ( ” Diagonal of the matrix : \n “, matrix.diagonal ( ) )

Diagonal of the matrix :

[ 4  8 12]

>>>

Inverse of Matrix

The inverse of the matrix can perform with following line of code

>>> import numpy as np

>>> matrix = np.array ( [ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] )

>>> print ( ” Matrix is : \n “, matrix)

Matrix is :

[[ 4  5  6]

[ 7  8  9]

[10 11 12]]

>>> print ( ” Inverse of the matrix : \n “, np.linalg.inv (matrix) )

Inverse of the matrix :

[[-9.38249922e+14  1.87649984e+15 -9.38249922e+14]

[ 1.87649984e+15 -3.75299969e+15  1.87649984e+15]

[-9.38249922e+14  1.87649984e+15 -9.38249922e+14]]

>>>