How to normalize a NumPy array to a unit vector? How to normalize a NumPy array to a unit vector? python python

# How to normalize a NumPy array to a unit vector?

If you're using scikit-learn you can use `sklearn.preprocessing.normalize`:

``import numpy as npfrom sklearn.preprocessing import normalizex = np.random.rand(1000)*10norm1 = x / np.linalg.norm(x)norm2 = normalize(x[:,np.newaxis], axis=0).ravel()print np.all(norm1 == norm2)# True``

I would agree that it were nice if such a function was part of the included batteries. But it isn't, as far as I know. Here is a version for arbitrary axes, and giving optimal performance.

``import numpy as npdef normalized(a, axis=-1, order=2):    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))    l2[l2==0] = 1    return a / np.expand_dims(l2, axis)A = np.random.randn(3,3,3)print(normalized(A,0))print(normalized(A,1))print(normalized(A,2))print(normalized(np.arange(3)[:,None]))print(normalized(np.arange(3)))``

This might also work for you

``import numpy as npnormalized_v = v / np.sqrt(np.sum(v**2))``

but fails when `v` has length 0.

In that case, introducing a small constant to prevent the zero division solves this.