+3 votes
2.4k views
asked in Python by (240 points)  

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

  

2 Answers

+1 vote
answered by (115k points)  
selected by
 
Best answer

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.

+1 vote
answered by (240 points)  
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.
...