NumPy is a python package that performs complex mathematical calculations easily after creating the array. The Numpy Matrix multiplication is one of them. In this tutorial you will know how to perform matrix multiplication of two numpy arrays with steps.

## Steps to perform Numpy Matrix Multiplication

Performing matrix multiplication using NumPy in Python is straightforward. Here are the steps:

### Step 1: Install NumPy (if it is not installed)

You can install NumPy using pip command. Run the following command if you haven’t installed it in your system.

```
pip install numpy #For python 2.xx
pip3 install numpy #For python 3.xx
```

### Step 2: Import NumPy

After installation import the NumPy library in your Python script or Jupyter Notebook.

`import numpy as np`

### Step 3: Create Sample Matrices

Define the matrices you want to multiply. Ensure that the number of columns in the first matrix matches the number of rows in the second matrix otherwise the error will shown.

```
# Example matrices
matrix_a = np.array([[10, 20, 30],
[40, 50, 60]])
matrix_b = np.array([[70, 80],
[90, 100],
[110, 120]])
```

### Step 4: Perform Matrix Multiplication

Use the `np.dot()`

function or the `@`

operator for matrix multiplication.

```
# Using np.dot()
result_dot = np.dot(matrix_a, matrix_b)
# Using @ operator (available in Python 3.5 and later)
result_at = matrix_a @ matrix_b
```

Both `np.dot()`

and `@`

perform matrix multiplication. The resulting matrix will have dimensions equal to the outer dimensions of the input matrices. In the above example `result_dot`

and `result_at`

will be 2×2 matrices.

Alternatively, you can use `np.matmul()`

for matrix multiplication:

` result_matmul = np.matmul(matrix_a, matrix_b)`

All these methods will give you the same result.

### Step 5: Display the Result

Now let’s print the result of the matrix multiplication.

```
print("Result using np.dot():\n", result_dot)
print("\nResult using @ operator:\n", result_at)
print("\nResult using np.matmul():\n", result_matmul)
```

**Output**

```
Result using np.dot():
[[ 5800 6400]
[13900 15400]]
Result using @ operator:
[[ 5800 6400]
[13900 15400]]
Result using np.matmul():
[[ 5800 6400]
[13900 15400]]
```

Thats all the steps you have to follow for the matrix multiplication.

## Other Methods to do Numpy Matrix Multiplication

You can also use the below method to perform matrix multiplication on two numpy arrays.

`numpy.matmul()`

function

This function performs matrix multiplication, similar to`np.dot()`

.

` result_matmul = np.matmul(matrix_a, matrix_b)`

**Output**

```
[[ 5800 6400]
[13900 15400]]
```

`numpy.vdot()`

function

This computes the dot product of two vectors.

` result_vdot = np.vdot(matrix_a, matrix_b)`

**Output**

` 21700`

`numpy.inner()`

function

This computes the inner product of vectors.

` result_inner = np.inner(matrix_a, matrix_b)`

`numpy.outer()`

function:

This computes the outer product of vectors.

` result_outer = np.outer(matrix_a, matrix_b)`

`numpy.einsum()`

function:

This function provides a powerful way to express a wide range of tensor operations using a compact string notation.

` result_einsum = np.einsum('ij,jk->ik', matrix_a, matrix_b)`

The string `'ij,jk->ik'`

specifies the Einstein summation convention for matrix multiplication.

**Output**

```
[[ 5800 6400]
[13900 15400]]
```

**Element-wise multiplication (**`*`

operator):

NumPy also allows you to perform element-wise multiplication using the`*`

operator.

` result_elementwise = matrix_a * matrix_b`

Note: This is not matrix multiplication; instead, it multiplies corresponding elements of the matrices.

## Conclusion

In machine learning you will mostly use matrix multiplication to design predictive models. The above are the steps you should follow for performing numpy matrix multiplication. Here you have also learned the other methos to multiply two numpy matrices.

## Leave a Reply