NumPy normalize 0 and 1 in Python

In this Python tutorial, I will explain how NumPy normalize 0 and 1 in Python using examples with 1D and 2D arrays and other range of numbers. I will also explain how Python NumPy normalize angle.

To normalize an array in Python NumPy, between 0 and 1 using either a custom function or the np.linalg.norm() function. The custom function scales data linearly based on the minimum and maximum values, while np.linalg.norm() normalizes data based on the array’s mean and vector norm.

NumPy normalize 0 and 1 in Python

To normalize a NumPy array in Python we can use the following methods:

  1. Custom Function
  2. np.linalg.norm() Function

Let’s see them one by one using some examples:

Method 1: NumPy normalize between 0 and 1 a Python array using a custom function

A custom NumPy normalize function can be written using basic arithmetic operations. The formula to normalize an array X is:

normalized_X = (Xi−min(X))/(max(X)−min(X))
XiThe element of the array that we want to normalize.
min(X)The minimum value within the Python array.
max(X)The maximum value within the Python array.

Example: Let’s take a 1D array and try to normalize it between 0 to 1 using a custom function through Python.

import numpy as np

def normalize_custom(X):
    X_min = X.min()
    X_max = X.max()
    return (X - X_min) / (X_max - X_min)

data = np.array([10, 20, 30])
normalized_data = normalize_custom(data)
print("Normalized Data:", normalized_data)

Output: This function calculates the minimum and maximum values of the array and then applies the normalization formula to each element.

Normalized Data: [0.  0.5 1. ]
NumPy normalize 0 and 1 in Python

This way we can use a custom function for NumPy normalize 0 and 1 in Python.

Method 2: Normalize NumPy array using np.linalg.norm() Function

In Python, Normalize means the normal value of the array has a vector magnitude and we have to convert the array to the desired range. To do this task we are going to use numpy.linalg.norm() method.

Syntax: Here is the Syntax of numpy.linalg.norm() method.

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)

Example: Let’s take an array in Python and try to normalize them between 0 to 1 through Python.

import numpy as np

array = np.array([12, 22, 28])

norm_value = np.linalg.norm(array)
normalized_array_with_norm = array / norm_value
print(normalized_array_with_norm)

Output: This will calculate the Euclidean length of the vector through Python.

[0.31934778 0.58547092 0.74514481]
np normalize in Python

This way we can use the np.linalg.norm() function for NumPy normalize 0 and 1 in Python.

Python normalize between 0 and 1 of a NumPy 2D array

To normalize a 2D array between 0 and 1 in Python using NumPy, we can use the code given in the example:

import numpy as np

array_2d = np.array([[1, 2], [3, 4]])

min_val = np.min(array_2d)
max_val = np.max(array_2d)
normalized_array_2d = (array_2d - min_val) / (max_val - min_val)
print('After Normalizing:', normalized_array_2d)

Output:

After Normalizing: [[0.         0.33333333]
 [0.66666667 1.        ]]
normalize np array between 0 and 1 in Python

NumPy normalize 0 and 1 in Python 2D array.

Python NumPy vector normalize in a range

To normalize a vector within a specific range in Python using NumPy, you can follow a two-step process:

  1. Normalize the vector to a 0 to 1 range.
  2. Scale and shift this normalized vector to your desired range.

For example, if you want to normalize a vector to a range of [a, b], the formula would be:

normalized_vector = (max(vector)−min(vector))/(vector−min(vector))
scaled_vector=normalized_vector×(b−a)+a
import numpy as np
def normalize_vector(vector, range_min, range_max):
    normalized_vector = (vector - np.min(vector)) / (np.max(vector) - np.min(vector))
    scaled_vector = normalized_vector * (range_max - range_min) + range_min
    return scaled_vector

vector = np.array([10, 20, 30])
normalized_vector = normalize_vector(vector, 0, 5)
print('After normalizing:', normalized_vector)

Output:

After normalizing: [0.  2.5 5. ]
numpy normalize vector in Python

This way we can use np.normalize a vector in Python between 0 to 5.

Python normalize angle in NumPy

To find out the angle of floating and complex values in Python, we are going to use numpy.angle() function and this method will also convert the NumPy array values to normalize.

import numpy as np

new_arr = np.array([[6.0,19.0j],
                [15+1j,12.0]])
new_result= np.angle(new_arr)
print(new_result)

Output:

[[0.         1.57079633]
 [0.06656816 0.        ]]
numpy normalize angle in Python

This way we can use the np.angle() function in NumPy normalize 0 and 1 in Python.

Conclusion

NumPy normalize 0 and 1 in Python array is a straightforward process, whether we choose a custom function for simplicity or the np.linalg.norm() function for a more statistical approach. Both methods provide efficient ways to preprocess data, ensuring compatibility and improved performance in various machine learning and data analysis applications.

Also, np.angle helps to find the angle of floating and complex values in Python.

You may also like to read: