Normalize a NumPy array to a unit vector in Python

In this article, we will learn how to normalize a NumPy array to a unit vector.

Table Of Contents

What is Normalization?

Normalization is a data transformation process which involves re-scaling of the data values to fall between standard scale, usually 0 to 1. The normalization increases the accuracy of the model.

Example of normalization

arr = [1000, 70, 50, 600, 80]
nomalized_array = [1, 0.07, 0.05, 0.6, 0.08]

What is a Unit vector?

There are multiple ways to normalize a NumPy array to a unit vector. Lets discuss all the methods one by one with proper approach and a working code examples.

Method 1: Using unit_vector() method from transformations library

The transformations module has a unit_vector() method, and it takes data as an input and returns a normalized ndarray which is a unit vector.

Syntax of unit_vector() function

transformations.unit_vector(data)
  • Parameters:
  • data : any
    data to be normalized, in this case numpy array.
  • Returns:
  • A normalized ndarray.

Approach :

  1. Import numpy library and create numpy array.
  2. Import transformations library, it can be installed using pip.
  3. Pass the numpy array to the unit_vector() method.
  4. The method will return a normalized array, whose magnitiude is 1.

Source Code :

import numpy as np
import transformations

# creating a numpy array
arr = np.array([1,2,3])

# normalizing the array to unit vector
print(transformations.unit_vector(arr))

Output :

[0.26726124 0.53452248 0.80178373]

If you get the error,

ModuleNotFoundError: No module named 'transformations'

Then you need to install the transformations module using command,

pip3 install transformations

Method 2: Using normalize() method from vg module.

The vg module has a normalize() method. It takes data as an input and returns a normalized vector, which is a unit vector.

Syntax of normalize() function

vg.normalize(data)
  • Parameters:
  • data : any
    data to be normalized, in this case numpy array.
  • Returns:
  • A normalized array.

Approach :

  1. Import numpy library and create numpy array.
  2. Import vg library, it can be installed using pip.
  3. Pass the numpy array to the normalize() method.
  4. The method will return a normalized array, whose magnitiude is 1.

Source Code :

import numpy as np
import vg

# creating a numpy array
arr = np.array([1,2,3])

# normalizing the array to unit vector
unit_vector = vg.normalize(arr)

print(unit_vector)

Output :

[0.26726124 0.53452248 0.80178373]

If you get the error,

ModuleNotFoundError: No module named 'vg'

Then you need to install the vg module using command,

pip3 install vg

Method 3: Using linalg.norm() method from numpy module

The numpy module has a norm() method. It takes data as an input and returns a norm of the data. To normalize an array into unit vector, divide the elements present in the data with this norm.

Syntax of linalg.norm() function

np.linalg.norm(data)
  • Parameters:
  • data : any
    data to be normalized, in this case numpy array.
  • Returns:
  • float or ndarray
    norm of vector.

Approach :

  1. Import numpy library and create numpy array.
  2. Pass the numpy array to the norm() method.
  3. The method will return a norm of the given vector.
  4. divide the entire array with this norm, the resultant array will be normalized unit vector

Source Code :

import numpy as np

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

# normalizing the array to unit vector
unitVector = data / np.linalg.norm(data)

print(unitVector)

Output :

[0.26726124 0.53452248 0.80178373]

Method 4: Calculating norm using dot

Instead of using np.linalg.norm we can calculate norm by using dot() and sqrt():

norm = np.sqrt(data.dot(data))

Here, dot() method is used to dot product the given data with itself and then calculate the square root of the resultant using sqrt(). To normalize the array into unit vector, divide the elements present in the data with this norm.

Syntax of dot() function

np.dot(a,b)
  • Parameters:
  • a,b : arrays
  • Returns:
  • dot product of a and b.

Approach :

  1. Import numpy library and create numpy array.
  2. Calculate the dot product of the array with itself using dot().
  3. calculate the square root of resultant using sqrt().
  4. divide the entire array with this norm, the resultant array will be normalized unit vector

Source Code :

import numpy as np

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

# normalizing the array to unit vector
unitVector = data/np.sqrt(data.dot(data))

print(unitVector)

Output :

[0.26726124 0.53452248 0.80178373]

Method 5: Using normalize() method from sklearn library

The scikit learn module have normalize() method. It takes data as an input and returns a normalized ndarray which is a unit vector.

Syntax of normalize() function

> sklearn.preprocessing.normalize(data)
  • Parameters:
  • data : any
    data to be normalized, in this case ndarray.
  • Returns:
  • A normalized ndarray.

Approach :

  1. Import numpy library and create numpy array.
  2. Import sklearn library, it can be installed using pip.
  3. Pass the numpy array to the normalize() method.
  4. The method will return a nomalized array, whose magnitiude is 1.

Source Code :

import numpy as np
from sklearn.preprocessing import normalize

# creating a numpy array
data = np.array([[1,2,3]])

# normalizing the array to unit vector
normalizedArray = normalize(data)

print(normalizedArray)

Output :

[[0.26726124 0.53452248 0.80178373]]

Summary

Great! you made it, We have discussed All possible methods to normalize a numpy array into unit vector. Happy learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top