numpy.linalg.norm
The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient low level implementations of standard linear algebra algorithms. Those libraries may be provided by NumPy itself using C versions of a subset of their reference implementations but, when possible, highly optimized libraries that take advantage of specialized processor functionality are preferred. Examples of such libraries are OpenBLAS, MKL ™, and ATLAS. Because those libraries are multithreaded and processor dependent, environmental variables and external packages such as threadpoolctl may be needed to control the number of threads or specify the processor architecture.
NumPy线性代数函数依靠BLAS和LAPACK提供标准线性代数算法的低阶实现。 NumPy本身可以使用其参考实现的子集的C版本来提供这些库,但是,在可能的情况下,最好是利用针对处理器优化过的库。 例如OpenBLAS,MKL(TM)和ATLAS。 因为这些库是多线程的,并且依赖于处理器,所以可能需要环境变量和外部程序包(例如threadpoolctl)来控制线程数或指定处理器体系结构。
numpy.linalg.norm
numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)
Matrix or vector norm.
矩阵或向量的范数。
This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.
根据参数ord,此函数能够返回8种不同的矩阵范数其中一个,或无穷多个矢量范数其中一个(如下所述)。
参数
名称 类型 说明 x
array_like
数组(矩阵、向量)Input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.
输入向量。如果axis
为None
,x
必须为1-D或2-D,除非ord
为None
。如果axis
和ord
均为None
,则将返回x.ravel
的2范数。ord
{non-zero int
,inf
,-inf
, ‘fro
’,’nuc
’}, optional
{non-zeroint
,inf
,-inf
, ‘fro
’,’nuc
’}, 可选Order of the norm (see table under Notes). inf means numpy’s inf object. The default is None.
范数的类型(见Notes下的表)。inf
表示numpy的inf
类型。默认为None
。axis
{ None
,int
, 2-tuple of ints}, optional.
{None
,int
, 2-tuple of ints}, 可选If axis
is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. Ifaxis
isNone
then either a vector norm (whenx
is 1-D) or a matrix norm (when x is 2-D) is returned. The default isNone
.New in version 1.8.0.
如果axis
是整数,则它指定x
的轴,沿着该轴计算矢量范数。如果axis
是2元组,则它指定保存2D矩阵的轴,并计算这些矩阵的矩阵范数。如果axis
为None
,则返回向量范数(当x
为1-D时)或矩阵范数(当x为2-D时)。默认为None
。1.8.0版中的新功能。keepdims
bool
, optionalbool
, 可选If this is set to True
, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the originalx
.New in version 1.10.0.
如果将其设置为True
,则仅在范数中保留1维。 With this option the result will broadcast correctly against the originalx
。1.10.0版中的新功能。
PS:对于axis
变量,当axis=0
时,表示对x
中除第一个下标外其他下标相同的量作为一组求范数,即按第一维分组进行求范数;当axis=1
时,表示对x
中除第二个下标外其他下标相同的量作为一组求范数,即按第二维分组进行求范数;依次类推。
以下面矩阵为例:
[ [ a 00 a 01 a 02 a 03 ] [ a 10 a 11 a 12 a 13 ] [ a 20 a 21 a 22 a 23 ] ] \left[ \begin{matrix} [a_{00}\ a_{01}\ a_{02}\ a_{03}] \\ [a_{10}\ a_{11}\ a_{12}\ a_{13}] \\ [a_{20}\ a_{21}\ a_{22}\ a_{23}] \end{matrix} \right] ⎣⎡[a00 a01 a02 a03][a10 a11 a12 a13][a20 a21 a22 a23]⎦⎤
axis = 0: [ a 00 2 + a 10 2 + a 20 2 a 01 2 + a 11 2 + a 21 2 a 02 2 + a 12 2 + a 22 2 a 03 2 + a 13 2 + a 23 2 ] [\sqrt{a_{00}^2+a_{10}^2+a_{20}^2}\ \sqrt{a_{01}^2+a_{11}^2+a_{21}^2}\ \sqrt{a_{02}^2+a_{12}^2+a_{22}^2}\ \sqrt{a_{03}^2+a_{13}^2+a_{23}^2}] [a002+a102+a202 a012+a112+a212 a022+a122+a222 a032+a132+a232]
axis = 1: [ a 00 2 + a 01 2 + a 02 2 + a 03 2 a 10 2 + a 11 2 + a 12 2 + a 13 2 a 20 2 + a 21 2 + a 22 2 + a 23 2 ] [\sqrt{a_{00}^2+a_{01}^2+a_{02}^2+a_{03}^2}\ \sqrt{a_{10}^2+a_{11}^2+a_{12}^2+a_{13}^2} \ \sqrt{a_{20}^2+a_{21}^2+a_{22}^2+a_{23}^2} ] [a002+a012+a022+a032 a102+a112+a122+a132 a202+a212+a222+a232]
axis>1:错误,因为矩阵仅为2维的
返回值
名称 类型 说明 n
float
orndarray
Norm of the matrix or vector(s).
矩阵或向量的范数。
说明
For values of ord <= 0, the result is, strictly speaking, not a mathematical ‘norm’, but it may still be useful for various numerical purposes.
当ord<=0
时,严格来说,函数计算结果不是数学意义上的‘范数’,但也许对其他数值运算仍有用。
The following norms can be calculated:
下列范数可以被计算:
ord
norm for matrices
矩阵范数norm for vectors
向量范数None
Frobenius norm
Frobenius范数2-norm 'fro'
Frobenius norm
Frobenius范数– '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)
The Frobenius norm is given by [^1] :
Frobenius在[^1]中给出:
∣ ∣ A ∣ ∣ F = [ ∑ i , j a b s ( a i , j ) 2 ] 1 / 2 ||A||_F = [\sum_{i,j} abs(a_{i,j})^2]^{1/2} ∣∣A∣∣F=[i,j∑abs(ai,j)2]1/2
The nuclear norm is the sum of the singular values.
核范数是奇异值的总和。
参考
[^1] G. H. Golub and C. F. Van Loan, Matrix Computations, Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15
示例
>>> from numpy import linalg as LA >>> a = np.arange(9) - 4 >>> a array([-4, -3, -2, ..., 2, 3, 4]) >>> b = a.reshape((3, 3)) >>> b array([[-4, -3, -2], [-1, 0, 1], [ 2, 3, 4]])
>>> LA.norm(a) 7.745966692414834 >>> LA.norm(b) 7.745966692414834 >>> LA.norm(b, 'fro') 7.745966692414834 >>>LA.norm(a, np.inf) 4.0 >>> LA.norm(b, np.inf) 9.0 >>> LA.norm(a, -np.inf) 0.0 >>> LA.norm(b, -np.inf) 2.0
>>> LA.norm(a, 1) 20.0 >>> LA.norm(b, 1) 7.0 >>> LA.norm(a, -1) -4.6566128774142013e-010 >>> LA.norm(b, -1) 6.0 >>> LA.norm(a, 2) 7.745966692414834 >>> LA.norm(b, 2) 7.3484692283495345
>>> LA.norm(a, -2) 0.0 >>> LA.norm(b, -2) 1.8570331885190563e-016 # may vary >>> LA.norm(a, 3) 5.8480354764257312 # may vary >>> LA.norm(a, -3) 0.0
Using the
axis
argument to compute vector norms:
使用axis
参数计算向量范数:
>>> c = np.array([[ 1, 2, 3], ... [-1, 1, 4]]) >>> LA.norm(c, axis=0) array([ 1.41421356, 2.23606798, 5. ]) >>> LA.norm(c, axis=1) array([ 3.74165739, 4.24264069]) >>> LA.norm(c, ord=1, axis=1) array([ 6., 6.])
Using the
axis
argument to compute matrix norms:
使用axis
参数计算矩阵范数:
>>> m = np.arange(8).reshape(2,2,2) >>> LA.norm(m, axis=(1,2)) array([ 3.74165739, 11.22497216]) >>> LA.norm(m[0, :, :]), LA.norm(m[1, :, :]) (3.7416573867739413, 11.224972160321824)