pythonnumpyscikit-learnstatisticsnormalization

How to normalize a numpy array to a unit vector


I would like to convert a NumPy array to a unit vector. More specifically, I am looking for an equivalent version of this normalisation function:

def normalize(v):
    norm = np.linalg.norm(v)
    if norm == 0: 
       return v
    return v / norm

This function handles the situation where vector v has the norm value of 0.

Is there any similar functions provided in sklearn or numpy?


Solution

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

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