NumPy: how to quickly normalize many vectors?

前端 未结 6 974
梦毁少年i
梦毁少年i 2021-01-31 04:47

How can a list of vectors be elegantly normalized, in NumPy?

Here is an example that does not work:

from numpy import *

vectors = array([arange         


        
6条回答
  •  被撕碎了的回忆
    2021-01-31 05:32

    Alright: NumPy's array shape broadcast adds dimensions to the left of the array shape, not to its right. NumPy can however be instructed to add a dimension to the right of the norms array:

    print vectors.T / norms[:, newaxis]
    

    does work!

提交回复
热议问题