Multiple, add, and divide element-wise and more with matrices.
Matrix Arithmetic in Numpy

Matrix arithmetics

In [2]:
import numpy as np
In [7]:
# create first 2D numpy array
lst = [[1, 2, 3], [4, 5, 6]]
array = np.array(lst_2, float)
array
Out[7]:
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
In [10]:
# create second 2D numpy array
lst_2 = [[7, 8, 9], [10, 11, 12]]
array_2 = np.array(lst_2)
array_2
Out[10]:
array([[ 7,  8,  9],
       [10, 11, 12]])
In [11]:
# element-wise addition
array + array_2
Out[11]:
array([[  8.,  10.,  12.],
       [ 14.,  16.,  18.]])
In [12]:
# element-wise subtractaction
array_2 - array
Out[12]:
array([[ 6.,  6.,  6.],
       [ 6.,  6.,  6.]])
In [13]:
# element-wise multiplication
array * array_2
Out[13]:
array([[  7.,  16.,  27.],
       [ 40.,  55.,  72.]])
Tags: numpy