In this blog post, we’ll discuss how to normalize a matrix using the popular Python library NumPy. But first things first: 👇

## What is Normalization?

In mathematics, ** normalizing** refers to making something standardized or regular. Normalization of a matrix is a process of scaling the matrix so that the elements of the matrix have a common scale without changing the rank or other fundamental matrix properties.

## Why Normalization?

Normalization is often used in machine learning and data analysis to pre-process data and make it more amenable to analysis. It can help to make the data more interpretable and easier to compare and can also help to prevent certain types of algorithms from being influenced by the scale of the data.

💡 **Matrix normalization** converts the original matrix into a normalized matrix such that the normalized matrix contains scaled values of the original matrix according to the desired range. Normalization of a vector or matrix is a common operation performed in a variety of scientific, mathematical, and programming applications.

For example, normalizing a matrix is a common operation in data pre-processing, which refers to the process of scaling the values in the matrix so that they have ** unit norm**.

A vector with unit norm has a Euclidean length of 1.

This is often useful when working with machine learning algorithms, as it can help to scale the input features so that they are on the same scale and have similar ranges.

## How to Perform Normalization of a 1D Array?

For Normalizing a 1D NumPy array in Python, take the minimum and maximum values of the array, then subtract each value with the minimum value and divide it by the difference between the minimum and maximum value.

Suppose we have an `array = [1, 2, 3, 4, 5, 6, 7, 8, 9]`

and wish to normalize it in the range `[0, 1]`

. Here’s the step-by-step approach to normalize it:

### Step 1: Import the NumPy library

import numpy as np

### Step 2: Define the original array

array = np.arange(1,10)

### Step 3: Perform normalization

arrmax, arrmin = array.max(), array.min() array = (array - arrmin) / (arrmax - arrmin)

### Step 4: Print normalized array

print("Array After Normalization") print(array)

**Code:**

import numpy as np #numpy array original array = np.arange(1, 10) print("Array before normalization") print(array) #apply normalization arrmax, arrmin = array.max(), array.min() array = (array - arrmin) / (arrmax - arrmin) print("Array after normalization") print(array)

**Output:**

Array before normalization

`[1 2 3 4 5 6 7 8 9]`

Array after normalization

`[0. 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1. ] `

## How to Perform Normalization of 2D Numpy Matrix?

For normalization of a NumPy matrix in Python, we use the Euclidean norm.

For the formula for simple normalization, we divide the original matrix with the norm of that matrix.

To calculate the norm of a matrix we can use the `np.linalg.norm()`

function which is an inbuilt function in NumPy that calculates the norm of a matrix.

## Frobenius Norm of Matrix

To calculate the Frobenius norm of the matrix, we multiply the matrix with its transpose and obtain the eigenvalues of this resultant matrix. The norm is calculated by the square root of the largest eigenvalue.

Suppose we have a matrix `[[1, 2, 3], [4, 5, 6], [7, 8, 9]]`

and wish to normalize it. Here’s the step-by-step approach of doing so:

### Step 1: Import the library

import numpy as np

### Step 2: Define the original NumPy matrix

array = np.arange(1,10) matrix = np.reshape(3,3)

### Step 3: Perform normalization

norm = np.linalg.norm(matrix) matrix = matrix / norm

### Step 4: Print normalized matrix

print(matrix)

**Code:**

import numpy as np #numpy array original array = np.arange(1,10) matrix = array.reshape(3,3) print("Matrix before normalization") print(matrix) #apply normalization norm = np.linalg.norm(matrix) Matrix = matrix/norm print("Matrix after normalization") print(matrix)

**Output:**

```
Matrix before normalization
[[1 2 3]
[4 5 6]
[7 8 9]]
Matrix after normalization
[[0.05923489 0.11846978 0.17770466]
[0.23693955 0.29617444 0.35540933]
0.41464421 0.4738791 0.53311399]]
```

Thanks for reading through the whole tutorial! ♥️