First time here? Checkout the FAQ!
x
+1 vote
995 views
asked in Machine Learning by (1.4k points)  
edited by

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 Python libraries.

Relative to the link, could there be a manual solution for L1 as well?

Could x include more dimensions for manual solutions? For example:

([[4,6,9,3], [8,1,2,5]])

  

2 Answers

+2 votes
answered by (1.4k points)  
edited by
 
Best answer

Found out the answer, thanks to a few reference sources.

Import the libraries:

import numpy as np
from sklearn.preprocessing import Normalizer as NRM

Manual L1 normalization:

If the data contained negative numbers, abs will find the correct value.

 

data = abs(np.array([[5, 8, 12, 15], [7,6,1,2], [4,1,0,3]]))
l1 = np.linalg.norm(data, ord=1, axis=1)
x_norm2 = data / l1[:,None]
x_norm2

"Undo" L1 normalization (turn normalization to raw numbers from data set):

Using the same variables as above

results = x_norm2*l1[:,None]
results

To verify the results, use the sklearn library:

normalizer_x = NRM(norm = "l1")
x_norm = normalizer_x.transform(data)
print(x_norm)

Results:

Here it is on repl.it.

References:

Stackoverflow

SciPy

commented by (116k points)  
Thank you so much for sharing!
commented by (360 points)  
+1
That would be great if you can share your code and results in https://repl.it
0 votes
answered by (1.1k points)  
I think if we have the normalization factor, it will be easy, right? Do you have any specific example?
...