Categories: NumpyPython

Matrix operations of Numpy arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

Matrix addition and subtraction of Numpy arrays of same size are element-wise, i.e. elements in the resulting matrix are the results of the corresponding elements of the inputting matrix at the same position, so the resulting matrix has the same shape as the inputting matrices. On the contrary, matrix multiplication of two Numpy arrays is not element-wise. Matrix multiplication of Numpy arrays in Python performs in the same manner as in calculus, i.e. the first matrix should have the same column numbers as the second matrix’s row numbers. Each element in the resulting matrix is the sum of product of each element of the corresponding row of first matrix and each corresponding element of the column of second matrix. The shape of the resulting matrix thus has the number of rows of the first matrix and number of columns of the second matrix. So the matrix multiplication of Numpy arrays is not element-wise, and the sequence of two matrices usually can not be interchanged. Matrix multiplication of Numpy array is implemented using function dot().

#import Numpy module
import numpy as np
#Create two matrices for addition and subtraction
M3 = np.arange(10, 18).reshape(2, 4)
M3
#output
array([[10, 11, 12, 13],
       [14, 15, 16, 17]])
M4 = np.arange(1, 9).reshape(2, 4)
M4
#output
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
#addition of two matrices of same size
M3 + M4
#result
array([[11, 13, 15, 17],
       [19, 21, 23, 25]])
#subtraction of two matrices of same size
M3 - M4
#result
array([[9, 9, 9, 9],
       [9, 9, 9, 9]])
#Matrix multiplication
#create first matrix of shape (2,4)
M1 = np.arange(10, 18).reshape(2, 4)
M1
#output
array([[10, 11, 12, 13],
       [14, 15, 16, 17]])
#create second matrix of shape (4,3)
M2 = np.arange(1, 13).reshape(4, 3)
M2
#output
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
#matrix multiplication of M1 * M2
np.dot(M1, M2)
#result
array([[268, 314, 360],
       [356, 418, 480]])
#we can write the same operation in alternative way
M1.dot(M2)
#result
array([[268, 314, 360],
       [356, 418, 480]])
#If we change the sequence to M2 * M1, it does not work
np.dot(M2, M1)
#output
Traceback (most recent call last):
  Cell In[5], line 1
    np.dot(M2, M1)
ValueError: shapes (4,3) and (2,4) not aligned: 3 (dim 1) != 2 (dim 0)

We do not talk about more complicate case when the addition or subtraction are carried out between two matrices of different sizes here. You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago