3,239 views
3 3 votes

How do I use the information provided in this link to apply in defining a L1-norm and L2-norm function in python?

100% Accept Rate Accepted 1 answers out of 1 questions

2 Answers

Best answer
1 1 vote

This article describes both of them in more details, but in short, we define $|| x ||_p$ as p-norm.

p-norm: Given $x$, a vector with $i$ components, a p-norm is defined as:

$|| x ||_p = \left(\sum_i |x_i|^p\right)^{1/p}$ 

The simplest norm conceptually is Euclidean distance. This is what we typically think of as distance between two points in space:

2-norm (Euclidean distance ro L2-norm)

$|| x ||_2 = \sqrt{\left(\sum_i x_i^2\right)} = \sqrt{x_1^2 + x_2^2 + \ldots + x_i^2}$

1-norm (Manhattan distance or L1-norm)

$|| x ||_1 = \sum_i |x_i| = |x_1| + |x_2| + \ldots + |x_i|$


Python implementation

If you want to calculate them in Python, numpy.linalg.norm provides functions for it. In addition, you can use Distance computations (scipy.spatial.distance) library. For example, for the Euclidean distance between two vectors, $a$ and $b$, you can use the following code:

from scipy.spatial import distance
distance.euclidean(a, b)

and if you want to calculate the Manhattan distance, you can use the following code:

from scipy.spatial import distance
distance.cityblock(a, b)

How did I know this time it is called cityblock? I read the documentation and looked up for Manhattan distance.

selected by
1 1 vote
For the assignment, I misread the notebook. I thought it was saying 11_norm (eleven_norm) and 12_norm (twelve_norm), when it was really saying l1_norm (L1_norm) and l2_norm (L2_norm). I should have looked at the characters in more detail, my mistake.

Related questions

1 1 vote
2 answers 2 answers
1.6k
1.6k views
kalyanak.p asked Sep 27, 2018
1,597 views
Please see example from the following link.The question is leaning towards a programming code solution in Python as the link above shows. Involving sklearn and any other ...
4 4 votes
1 1 answer
20.0k
20.0k views
1 1 vote
1 answers 1 answer
6.5k
6.5k views
1 1 vote
1 1 answer
750
750 views
datascience asked Sep 28, 2018
750 views
Is there any big difference between ingle quotation (' ') and double ("") quotation in Python?
0 0 votes
1 1 answer
709
709 views
tofighi asked May 20, 2019
709 views
Python3 and Python2 seems so similar, and the only changes which are obvious are some changes in functions such as print that now needs parentheses. Are there other impro...