# NumPy Matrix Multiplication

By | September 28, 2021 In python, we have a very powerful 3rd party library NumPy which stands for Numerical Python. In standard python we do not have support for standard Array data structure like what we have in Java and C++, so without a proper array, we cannot form a Matrix on which we can perform direct arithmetic operations. so in this article, we will discuss NumPy Matrix Multiplication.

Tough in Standard Python we have a list data structure that acts like an array and we also have an inbuilt array module but both are not sufficient for the mathematic matrix concept. As we know that matrix and arrays are the most powerful and important data structures and a key to build data science models, and we just cannot ignore those in python, that’s why python has introduced a library which gives full support for the arrays similar to other high-level programming languages.

NumPy is an open-source Python package, which is mostly used for data science because of its built-in support for many mathematical tools. It also works along with SciPy and Mat-plot lib libraries which are used to write powerful algorithms for data science models.

### Numpy dot() Matrix Multiplication:

As NumPy is famous for the support of Mathematic tools, so to perform matrix multiplication we do not need to write an algorithm NumPy provides users with an inbuilt dot() method which can multiply two matrices. Example:

```import numpy as np

Matrix_A = np.array( [[1,2,3],
[4,5,7],
[8,9,10]])

Matrix_B = np.array([[11,12,13],
[14,15,16],
[17,18,19]])

print("Matrix_A * Matrix_B is :")
print(Matrix_A.dot(Matrix_B))

print("\nMatrix_B * Matrix_A is: ")
print(Matrix_B.dot(Matrix_A))```

Output:

```Matrix_A * Matrix_B is :
[[ 90  96 102]
[233 249 265]
[384 411 438]]

Matrix_B * Matrix_A is:
[[163 199 247]
[202 247 307]
[241 295 367]]

```

#### Matrix Multiplication using For Loop

```import numpy as np

Matrix_A = np.array( [[1,2,3],
[4,5,7],
[8,9,10]])

Matrix_B = np.array([[11,12,13],
[14,15,16],
[17,18,19]])

out_1 = [[0 for c_1 in range(len(Matrix_A)) ] for r_1 in range(len(Matrix_B))]
out_2 = [[0 for c_2 in range(len(Matrix_A)) ] for r_2 in range(len(Matrix_B))]

print("Matrix_A * Matrix_B is :")
for i in range(len(Matrix_A)):
for j in range(len(Matrix_B)):
for k in range(len(Matrix_B)):
out_1[i][j] += Matrix_A[i][k]*Matrix_B[k][j]

print(np.array(out_1))

print("\nMatrix_B * Matrix_A is: ")
for i in range(len(Matrix_B)):
for j in range(len(Matrix_A)):
for k in range(len(Matrix_A)):
out_2[i][j] += Matrix_B[i][k]*Matrix_A[k][j]

print(np.array(out_2))```

Output:

```Matrix_A * Matrix_B is :
[[ 90  96 102]
[233 249 265]
[384 411 438]]

Matrix_B * Matrix_A is:
[[163 199 247]
[202 247 307]
[241 295 367]]

```