线性代数(numpy.linalg)API

1. 线性代数(numpy.linalg)

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)来控制线程数或指定处理器体系结构。

1.1. 范数和其他

1.1.1. 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种不同的矩阵范数其中一个,或无穷多个矢量范数其中一个(如下所述)。

参数

名称类型说明
xarray_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.
输入向量。如果axisNonex必须为1-D或2-D,除非ordNone。如果axisord均为None,则将返回x.ravel的2范数。
ord{non-zero int, inf, -inf, ‘fro’,’nuc’}, optional
{non-zero int, 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 axisis 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. If axisis Nonethen either a vector norm (when xis 1-D) or a matrix norm (when x is 2-D) is returned. The default is None.New in version 1.8.0.
如果axis是整数,则它指定x的轴,沿着该轴计算矢量范数。如果axis是2元组,则它指定保存2D矩阵的轴,并计算这些矩阵的矩阵范数。如果axisNone,则返回向量范数(当x为1-D时)或矩阵范数(当x为2-D时)。默认为None。1.8.0版中的新功能。
keepdimsbool, optional
bool, 可选
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 original x.New in version 1.10.0.
如果将其设置为True,则仅在范数中保留1维。 With this option the result will broadcast correctly against the original x。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维的

返回值

名称类型说明
nfloator ndarrayNorm 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:

下列范数可以被计算:

ordnorm for matrices
矩阵范数
norm for vectors
向量范数
NoneFrobenius norm
Frobenius范数
2-norm
'fro'Frobenius norm
Frobenius范数
'nuc'nuclear norm
核范数
infmax(sum(abs(x), axis=1))max(abs(x))
-infmin(sum(abs(x), axis=1))min(abs(x))
0sum(x != 0)
1max(sum(abs(x), axis=0))as below
-1min(sum(abs(x), axis=0))as below
22-norm (largest sing. value)as below
-2smallest singular valueas below
othersum(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} AF=[i,jabs(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 axisargument 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 axisargument 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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值