[英]Calculating norm of columns as vectors in a matrix
I am looking for the best way of calculating the norm of columns as vectors in a matrix. 我正在寻找计算列的范数作为矩阵中的向量的最佳方法。 My code right now is like this but I am sure it can be made better(with maybe numpy?):
我现在的代码是这样的,但我相信它可以做得更好(可能是numpy?):
import numpy as np
def norm(a):
ret=np.zeros(a.shape[1])
for i in range(a.shape[1]):
ret[i]=np.linalg.norm(a[:,i])
return ret
a=np.array([[1,3],[2,4]])
print norm(a)
Which returns: 哪个回报:
[ 2.23606798 5. ]
Thanks. 谢谢。
您可以使用ufuncs计算规范:
np.sqrt(np.sum(a*a, axis=0))
使用numpy的直接解决方案:
x = np.linalg.norm(a, axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.