NumPy Vector

Get the vector norm

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, which is not float.

Calculate the inner product of vectors

import numpy as np

v = np.array([1, 2])
w = np.array([3, 4])

i = np.dot(v, w)

print(i)  # 11
print(type(i))  # <class 'numpy.int64'>

Calculate the outer product of vectors

import numpy

a = numpy.array([1, 2])
b = numpy.array([3, 4])

c = numpy.outer(a, b)

print(c)
# [[3 4]
#  [6 8]]

print(type(c))  # <class 'numpy.ndarray'>

Calculate the angle between two vectors

import numpy as np
import numpy.linalg as LA

a = np.array([1, 2])
b = np.array([-5, 4])

inner = np.inner(a, b)
norms = LA.norm(a) * LA.norm(b)

cos = inner / norms
rad = np.arccos(np.clip(cos, -1.0, 1.0))
deg = np.rad2deg(rad)

print(rad)  # 1.35970299357215
print(deg)  # 77.9052429229879

NumPy Vector

NumPy Tutorial