函数:
norm(x, ord = None, axis = None, keepdims = False)
ord表示求什么类型的范数
举例说明:
import numpy as np
x = [1,2,3,4]
x1 = np.linalg.norm(x=x, ord=1)
x2 = np.linalg.norm(x=x, ord=2)
x3 = np.linalg.norm(x=x, ord=np.inf)
print(x1)
print(x2)
print(x3)
运行结果:
axis=0表示对矩阵的每一列求范数,axis=1表示对矩阵的每一行求范数, keeptdims=True表示结果保留二维特性,keepdims=False表示结果不保留二维特性
import numpy as np
x = np.array([[0, 1, 2],
[3, 4, 5]])
x1 = np.linalg.norm(x=x, ord=1, axis=0, keepdims=True)
x2 = np.linalg.norm(x=x, ord=1, axis=1, keepdims=True)
x3 = np.linalg.norm(x=x, ord=1, axis=0, keepdims=False)
x4 = np.linalg.norm(x=x, ord=1, axis=1, keepdims=False)
print(x1)
print(x2)
print(x3)
print(x4)
运行结果: