How to get the unit vector from a numpy array

This question already has answers here: How to normalize a NumPy array to a unit vector? (13 answers) Closed 3 years ago.

Lets say I have a vector v, and I want the unit vector, i.e. v has length 1.0 Is there a direct way to get that from numpy?

I want something like:

import numpy as np
v_hat = v.norm()

Rather than,

length = np.linalg.norm(v)
v_hat = v / length

Leave a Comment