
NumPy norm: How to calculate the norm of a vector in Python
This is an example to calculate a vector norm using Python NumPy.
import numpy as np
from numpy import linalg as LA
v = np.array([3, 4])
n = LA.norm(v)
print(n) # 5.0
print(type(n)) # <class 'numpy.float64'>
linalg.norm returns the norm of NumPy array (vector) in numpy.float64. numpy.float64
is not float
.
Another example.
import numpy as np
from numpy import linalg as LA
v = np.array([-1, 2.3, 1 / 5])
n = LA.norm(v)
print(n) # 2.515949125081825
print(type(n)) # <class 'numpy.float64'>
Even if a vector has a fraction or float, norm
calculates properly.
Comments
Powered by Markdown