import numpy as np
函数: np.linalg.norm(x, ord=None, axis=None, keepdims=False)
参数释义:x:向量或矩阵 ? ? ? ? ? ? ? ? ? ord:范数类型,默认二范数,ord1=1,求一范数,即元素绝对值和,ord=2,求二范数,ord=np.inf,求无穷范数,即max(|x_i|) ? ? ? ? ? ? ? ? ??axis:维度处理,axis=1表示按行向量处理,求多个行向量的范数;axis=0表示按列向量处理,求多个列向量的范数;axis=None表示矩阵范数。 ? ? ? ? ? ? ? ? ? keepdims:是否保留计算范数时指定的维度,True:保留,False:不保留
例: ?
import numpy as np
x2=np.arange(12).reshape(3,4)
print (x2)
print (np.linalg.norm(x2))
print (np.linalg.norm(x2,ord=2, axis=0, keepdims=False).shape, np.linalg.norm(x2,ord=2, axis=0, keepdims=False))
print (np.linalg.norm(x2,ord=2, axis=0, keepdims=True).shape, np.linalg.norm(x2,ord=2, axis=0, keepdims=True))
print (np.linalg.norm(x2,ord=2, axis=1, keepdims=False).shape, np.linalg.norm(x2,ord=2, axis=1, keepdims=False))
print (np.linalg.norm(x2,ord=2, axis=1, keepdims=True).shape, np.linalg.norm(x2,ord=2, axis=1, keepdims=True))
output:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
22.494443758403985
(4,) [ 8.94427191 10.34408043 11.83215957 13.37908816]
(1, 4) [[ 8.94427191 10.34408043 11.83215957 13.37908816]]
(3,) [ 3.74165739 11.22497216 19.13112647]
(3, 1) [[ 3.74165739]
[11.22497216]
[19.13112647]]
刚多范数类型参看这篇:https://blog.csdn.net/weixin_43977640/article/details/109909488
|