numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)[source]
The following norms can be calculated:
ord norm for matrices norm for vectors
None Frobenius norm 2-norm
‘fro’ Frobenius norm –
‘nuc’ nuclear norm –
inf max(sum(abs(x), axis=1)) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
0 – sum(x != 0)
1 max(sum(abs(x), axis=0)) as below
-1 min(sum(abs(x), axis=0)) as below
2 2-norm (largest sing. value) as below
-2 smallest singular value as below
other – sum(abs(x)**ord)**(1./ord)
使用np.linalg.norm很实用,比如用2-norm可以很容易求得欧式距离等
# -*- coding: utf-8 -*-
"""
@author: 蔚蓝的天空Tom
Talk is cheap, show me the code
Aim:用numpy.linalg.norm方法求矩阵的三种范数
Note numpy.linalog.norm ---> return Norm of the matrix or vector(s)
"""
import numpy as np
from numpy import linalg as LA
def norm_matrix():
'''求向量的三种范数
'''
A = np.array([[3, -4],
[-6, 5]])
#9.2736184954957039, 对象是矩阵,求矩阵元素平方和开方
ret = np.linalg.norm(A, ord=None)
#[ 5. 7.81024968],对象是逐行,求每行元素平方和开方
ret = np.linalg.norm(A, ord=None, axis=1)
#[ 6.70820393 6.40312424], 对象是逐列,求每列元素平方和开方
ret = np.linalg.norm(A, ord=None, axis=0)
#11.0,对象是矩阵,求矩阵中逐行元素绝对值和的最大值
ret = np.linalg.norm(A, ord=np.Inf)
#[ 4. 6.], 对象是逐行,求每行元素绝对值的最大值
ret = np.linalg.norm(A, ord=np.Inf, axis=1)
#[ 6. 5.], 对象是逐列,求每列元素绝对值的最大者
ret = np.linalg.norm(A, ord=np.Inf, axis=0)
#7.0 对象是矩阵,求矩阵中逐行元素绝对值和的最小值
ret = np.linalg.norm(A, ord=-np.Inf)
#[ 3. 5.], 对象是逐行,求行的元素绝对值的最小值
ret = np.linalg.norm(A, ord=-np.Inf, axis=1)
#[ 3. 4.],对象是逐列,求列的元素绝对值的最小值
ret = np.linalg.norm(A, ord=-np.Inf, axis=0)
#raise ValueError("Invalid norm order for matrices.")
#ret = np.linalg.norm(A, ord=0)
A = np.array([[3, -4, 1],
[-6, 5, 0]])
#[ 3. 2.], 对象是逐行,求每行的非零元素个数
ret = np.linalg.norm(A, ord=0, axis=1)
#[ 2. 2. 1.],对象是逐列,求每列的非零元素个数
ret = np.linalg.norm(A, ord=0, axis=0)
A = np.array([[100, -4],
[-6, 5]])
#106.0, 对象是矩阵,求列的元素绝对值和的最大值
ret = np.linalg.norm(A, ord=1)
#[ 104. 11.], 对象是逐行,求行的元素绝对值和
ret = np.linalg.norm(A, ord=1, axis=1)
#[ 106. 9.],对象是逐列,求列的元素绝对值和
ret = np.linalg.norm(A, ord=1, axis=0)
A = np.array([[-4, 3, 1],
[-6, 5, -1]])
#2.0, 对象是矩阵,求列的元素绝对值和的最小者
ret = np.linalg.norm(A, ord=-1) #1.71428571429, min(sum(abs(x), axis=0))
#[ 0.63157895 0.73170732], 对象是逐行
ret = np.linalg.norm(A, ord=-1, axis=1)
#[ 2.4 1.875 0.5 ],对象是逐列
ret = np.linalg.norm(A, ord=-1, axis=0)
A = np.array([[-4, 3, 4],
[-6, 4, 3]])
#10.0015300174, 对象是矩阵,求所有元素平方和开方,所有元素平方和102,开方=10.0015300174
ret = np.linalg.norm(A, ord=2)
#[ 6.40312424 7.81024968],对象是每行,求行的元素平方和开方
ret = np.linalg.norm(A, ord=2, axis=1)
#[ 7.21110255 5. 5.],对象是每列,求列的元素平方和开方
ret = np.linalg.norm(A, ord=2, axis=0)
A = np.array([[-4, 3, 4],
[-6, 4, 3]])
#1.40335216944,对象是矩阵,smallest singular value
ret = np.linalg.norm(A, ord=-2)
#[ 2.05798302 2.22834406],对象是逐行
ret = np.linalg.norm(A, ord=-2, axis=1)
#[ 3.32820118 2.4 2.4],对象是逐列
ret = np.linalg.norm(A, ord=-2, axis=0)
if __name__=='__main__':
norm_matrix()
# -*- coding: utf-8 -*-
"""
@author: 蔚蓝的天空Tom
Talk is cheap, show me the code
Aim:用numpy.linalg.norm方法求向量的三种范数
Note numpy.linalog.norm ---> return Norm of the matrix or vector(s)
"""
import numpy as np
from numpy import linalg as LA
def norm_vector():
'''
求向量的三种范数
'''
A = np.array([3, -4])
#5.0,平方和开方
ret = np.linalg.norm(A, ord=None)
#4.0, max(abs(x)),元素绝对值的最大者
ret = np.linalg.norm(A, ord=np.Inf)
#3.0, min(abs(x)),元素绝对值的最小
ret = np.linalg.norm(A, ord=-np.Inf)
#2.0, sum(x!=0),非0元素的个
ret = np.linalg.norm(A, ord=0)
#7.0, max(sum(abs(x), axis=0)),元素绝对值的和的最大值
ret = np.linalg.norm(A, ord=1)
#1.71428571429, min(sum(abs(x), axis=0))
ret = np.linalg.norm(A, ord=-1)
#5.0, 平方和开方
ret = np.linalg.norm(A, ord=2)
#2.4, smallest singular value
ret = np.linalg.norm(A, ord=-2)
if __name__=='__main__':
norm_vector()
def norm_em():
'''用2-norm求欧氏距离'''
p1 = np.array([1, 1])
p2 = np.array([4, 5])
em = np.sqrt(sum(np.multiply(p1-p2, p1-p2))) #5.0 一般方法求欧氏距离, 欧氏距离就是两点连线距离
em = np.linalg.norm(p1-p2, ord=2) #5.0 用2-norm求欧氏距离
参考文献:
[1]https://blog.csdn.net/hqh131360239/article/details/79061535
[2]https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html
(end)