Explain how L2 Normalization can be implemented using scikit-learn library in Python?

The process of converting a range of values into standardized range of values is known as normalization. These values could be between -1 to +1 or 0 to 1. Data can be normalized with the help of subtraction and division as well.

Let us understand how L2 normalization works. It is also known as ‘Least Squares’. This normalization modifies the data in such a way that the sum of the squares of the data remains as 1 in every row.

Let us see how L2 normalization can be implemented using Scikit learn in Python −


import numpy as np
from sklearn import preprocessing
input_data = np.array(
   [[34.78, 31.9, -65.5],[-16.5, 2.45, -83.5],[0.5, -87.98, 45.62],[5.9, 2.38, -55.82]]
normalized_data_l2 = preprocessing.normalize(input_data, norm='l2')
print("\nL2 normalized data is \n", normalized_data_l2)


L2 normalized data is
[[ 0.43081298 0.39513899 -0.81133554]
[-0.19377596 0.02877279 -0.98062378]
[ 0.00504512 -0.88774018 0.4603172 ]
[ 0.10501701 0.04236279 -0.99356772]]


  • The required packages are imported.

  • The input data is generated using the Numpy library.

  • The ‘normalize’ function present in the class ‘preprocessing‘ is used to normalize the data such that the sum of squares of values in every row would be 1.

  • The type of normalization is specified as ‘l2’.

  • This way, any data in the array gets normalized and the sum of squares of every row would be 1 only.

  • This normalized data is displayed on the console.

Updated on: 11-Dec-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started