numpy的求模模块 linalg在范数运算中的应用

refer:    http://blog.csdn.net/bitcarmanlee/article/details/51945271

1.范数(norm)的意义

要更好的理解范数,就要从函数、几何与矩阵的角度去理解。
我们都知道,函数与几何图形往往是有对应的关系,这个很好想象,特别是在三维以下的空间内,函数是几何图像的数学概括,而几何图像是函数的高度形象化,比如一个函数对应几何空间上若干点组成的图形。
但当函数与几何超出三维空间时,就难以获得较好的想象,于是就有了映射的概念,映射表达的就是一个集合通过某种关系转为另外一个集合。通常数学书是先说映射,然后再讨论函数,这是因为函数是映射的一个特例。
为了更好的在数学上表达这种映射关系,(这里特指线性关系)于是就引进了矩阵。这里的矩阵就是表征上述空间映射的线性关系。而通过向量来表示上述映射中所说的这个集合,而我们通常所说的基,就是这个集合的最一般关系。于是,我们可以这样理解,一个集合(向量),通过一种映射关系(矩阵),得到另外一个几何(另外一个向量)。
那么向量的范数,就是表示这个原有集合的大小。
而矩阵的范数,就是表示这个变化过程的大小的一个度量。

总结起来一句话,范数(norm),是具有“长度”概念的函数。

2.范数满足的三个特性

1.非负性: ||x||0 ,且 ||x||=0 当且仅当 x=0 时成立 。
2.齐次性: ||kx||=|k|||x||
3.三角不等式: ||x+y||||x||+||y||

3.向量的范数

1-范数,计算方式为向量所有元素的绝对值之和。

||x||1=in|xi|

2-范数,计算方式跟欧式距离的方式一致。
||x||2=(i=1n|xi|2)2

-范数,所有向量元素中的最大值。
||x||=maxi|xi|

-范数,所有向量元素中的最小值。
||x||=mini|xi|

p -范数,所有向量元素绝对值的p次方和的1/p次幂。
||x||p=(i=1n|xi|p)1p

4.矩阵的范数

首先假设矩阵的大小为 mn ,即m行n列。

1-范数,又名列和范数。顾名思义,即矩阵列向量中绝对值之和的最大值。

||A||1=maxji=1m|aij|

2-范数,又名谱范数,计算方法为 ATA 矩阵的最大特征值的开平方。
||A||2=λ1

其中 λ1 ATA 的最大特征值。

-范数,又名行和范数。顾名思义,即矩阵行向量中绝对值之和的最大值。

||A||=maxji=1n|aij|

F-范数,Frobenius范数,计算方式为矩阵元素的绝对值的平方和再开方。

||A||F=i=1mj=1n|aij|212

5.在python里计算范数

numpy包里的linalg模块,是专门处理基本线性代数问题的模块。借助该模块中的norm()函数可以轻松计算向量与矩阵的范数。

先看看norm()方法的原型:

def 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.

    Parameters
    ----------
    x : array_like
        Input array.  If `axis` is None, `x` must be 1-D or 2-D.
    ord : {non-zero int, inf, -inf, 'fro', 'nuc'}, optional
        Order of the norm (see table under ``Notes``). inf means numpy's
        `inf` object.
    axis : {int, 2-tuple of ints, None}, optional
        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.  If `axis` is None then either a vector norm (when `x`
        is 1-D) or a matrix norm (when `x` is 2-D) is returned.
    keepdims : bool, optional
        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`.

        .. versionadded:: 1.10.0

    Returns
    -------
    n : float or ndarray
        Norm of the matrix or vector(s).

    Notes
    -----
    For values of ``ord <= 0``, the result is, strictly speaking, not a
    mathematical 'norm', but it may still be useful for various numerical
    purposes.

    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)
    =====  ============================  ==========================

    The Frobenius norm is given by [1]_:

        :math:`||A||_F = [\\sum_{i,j} abs(a_{i,j})^2]^{1/2}`

    The nuclear norm is the sum of the singular values.

    References
    ----------
    .. [1] G. H. Golub and C. F. Van Loan, *Matrix Computations*,
           Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15
 


再看看更为详细的计算说明:

Examples
    --------
    >>> from numpy import linalg as LA
    >>> a = np.arange(9) - 4
    >>> a
    array([-4, -3, -2, -1,  0,  1,  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)
    nan
    >>> LA.norm(b, -2)
    1.8570331885190563e-016
    >>> LA.norm(a, 3)
    5.8480354764257312
    >>> LA.norm(a, -3)
    nan

    Using the `axis` argument to compute vector norms:

    >>> 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:

    >>> 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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于你的问题,我可以帮你解答。 首先,我们可以使用Numpy模块来创建两个数组或矩阵。下面是创建两个数组的示例代码: ```python import numpy as np # 创建两个一维数组 array1 = np.array([1, 2, 3, 4, 5]) array2 = np.array([6, 7, 8, 9, 10]) print("Array 1:", array1) print("Array 2:", array2) ``` 接下来,我们可以进行基本的运算操作,包括加、减、乘、除、幂和点积。以下是对这两个数组进行基本运算的示例代码: ```python # 加 addition = array1 + array2 print("Addition:", addition) # 减 subtraction = array1 - array2 print("Subtraction:", subtraction) # 乘 multiplication = array1 * array2 print("Multiplication:", multiplication) # 除 division = array1 / array2 print("Division:", division) # 幂 power = np.power(array1, array2) print("Power:", power) # 点积 dot_product = np.dot(array1, array2) print("Dot Product:", dot_product) ``` 最后,让我们以一个矩阵为例,完成一些线性代数运算。下面是一个示例代码,其我们计算了逆矩阵、行列式、特征值、特征向量和奇异值分解: ```python # 创建一个二维矩阵 matrix = np.array([[1, 2], [3, 4]]) # 逆矩阵 inverse = np.linalg.inv(matrix) print("Inverse Matrix:", inverse) # 行列式 determinant = np.linalg.det(matrix) print("Determinant:", determinant) # 特征值和特征向量 eigenvalues, eigenvectors = np.linalg.eig(matrix) print("Eigenvalues:", eigenvalues) print("Eigenvectors:", eigenvectors) # 奇异值分解 singular_values = np.linalg.svd(matrix) print("Singular Values:", singular_values) # 范数 norm = np.linalg.norm(matrix) print("Matrix Norm:", norm) ``` 希望这些示例代码能够帮助你理解如何使用Numpy进行随机数组的创建、基本运算和线性代数运算。如果还有其他问题,请随时提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值