add-semi-colons
add-semi-colons

Reputation: 18810

Python normalization without any packages

Trying to calculate L1 Norm without using any packages in python

Lets say i have vector: l = [2.34, 3.32, 6.32, 2.5, 3,3, 5.32]

And I want to find L1 of this vector without any packages:

I have calculated the

mean = sum(l) / float(len(l)
variance = sum(pow(x-mean, 3) for x in l) / len(l)
normalized = [(x-mean)/std for x in l]

How do I get the L1-Norm

Upvotes: 1

Views: 262

Answers (2)

andrew_reece
andrew_reece

Reputation: 21264

You can get the L1 norm like this:

sum(map(abs, l))
# 25.8

To check (using Numpy):

import numpy as np
np.linalg.norm(l, 1)
# 25.800000000000001

Upvotes: 2

Romain Reboulleau
Romain Reboulleau

Reputation: 306

Using the built-in mathmodule, the $L^1$ norm would be:

L1 = sum([math.fabs(x) for x in l])

Upvotes: 0

Related Questions