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