QBoard » Artificial Intelligence & ML » AI and ML - Python » How to normalize an array in NumPy to a unit vector?

How to normalize an array in NumPy 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 function

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

     

    Is there something like that in sklearn or numpy?

    This function works in a situation where v is the 0 vector.

    This post was edited by Sai Anirudh at December 11, 2020 1:44 PM IST
      December 10, 2020 4:57 PM IST
    0
  • The numpy module in Python has the norm() function that can return the array’s vector norm. Then we divide the array with this norm vector to get the normalized vector. For example, in the code below, we will create a random array and find its normalized form using this method.
    import numpy as np
    
    v = np.random.rand(10)
    normalized_v = v/np.linalg.norm(v)
    print(normalized_v)​
      August 1, 2021 11:12 PM IST
    0
  • If you're using scikit-learn you can use sklearn.preprocessing.normalize
      August 3, 2021 10:20 PM IST
    0
  • 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
      August 6, 2021 4:47 PM IST
    0
  • If you have multidimensional data and want each axis normalized to its max or its sum:
    def normalize(_d, to_sum=True, copy=True):
        # d is a (n x dimension) np array
        d = _d if not copy else np.copy(_d)
        d -= np.min(d, axis=0)
        d /= (np.sum(d, axis=0) if to_sum else np.ptp(d, axis=0))
        return d

    Uses numpys peak to peak function.

    a = np.random.random((5, 3))
    
    b = normalize(a, copy=False)
    b.sum(axis=0) # array([1., 1., 1.]), the rows sum to 1
    
    c = normalize(a, to_sum=False, copy=False)
    c.max(axis=0) # array([1., 1., 1.]), the max of each row is 1

     

      December 14, 2020 7:07 PM IST
    0