I would like to convert a NumPy array to a unit vector. More specifically, I am looking for an equivalent version of this function
def normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
Is there something like that in skearn
or numpy
?
This function works in a situation where v
is the 0 vector.
raise
an exception! - Hookedx/np.linalg.norm(x)
was not much slower (about 15-20%) thanx/np.sqrt((x**2).sum())
in numpy 1.15.1 on a CPU. - Bill