Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

l2
norm
distance
machine learning

L2 norm in Python

Educative Answers Team

The L2L_2 norm loss function, also known as the least squares error (LSE), is used to minimize the sum of the square of differences between the target value, YiY_i, and the estimated value, f(xi)f(x_i)

The mathematical representation of L2L_2-norm is:

widget

As an error function, L2L_2-norm is less robust to outliers than the L1L_1-norm. An outlier causes the error value to increase to a much larger number because the difference in the actual and predicted value gets squared.

However, L2L_2-norm always provides one stable solution (unlike L1L_1-norm).

The L1L_1-norm loss function is known as the least absolute error (LAE) and ​is used to minimize the sum of absolute differences between the target value, YiY_i, and the estimated values, f(xi)f(x_i).

Code

The code to implement the L2L_2-norm is given below:

import numpy as np
actual_value = np.array([1, 2, 3])
predicted_value = np.array([1.1, 2.1, 5 ])

# take square of differences and sum them
l2 = np.sum(np.power((actual_value-predicted_value),2))
print(l2)

RELATED TAGS

l2
norm
distance
machine learning
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring