向量范数与矩阵范数定义和python向量矩阵运算示例

向量范数与矩阵范数定义和python向量矩阵运算示例

1.范数(norm)的意义

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

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

2.范数满足的三个特性

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

3.向量的范数

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

||x||1=in|xi|||x||1=∑in|xi|

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

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

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

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

4.矩阵的范数

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

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

||A||1=maxji=1m|aij|||A||1=maxj∑i=1m|aij|

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

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

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

||A||=maxji=1n|aij|||A||∞=maxj∑i=1n|aij|

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

||A||F=(i=1mj=1n|aij|2)12||A||F=(∑i=1m∑j=1n|aij|2)12

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

我的例子:

#!/usr/bin/env python
# coding:utf-8
import numpy as np
import numpy.linalg as LA

def compute_norm():
    mat = np.matrix([[1,2],[3,4]])
    print "mat:\n", mat
    inv_mat = np.linalg.inv(mat)
    print "inv_mat:\n",inv_mat

def vector_norm():
    a = np.arange(9) - 4
    print a
    print LA.norm(a, np.inf)  # 无穷范数
    print LA.norm(a, -np.inf)  # 负无穷范数
    print LA.norm(a, 1)  # 1范数
    print LA.norm(a, 2)  # 2范数

def matrix_norm():
    a = np.arange(9) - 4
    print a
    b = a.reshape(3, 3)
    print "b:\n",b
    b_t = np.transpose(b)
    print "b_t:\n",b_t
    b_new = np.dot(b_t, b)  # b_new矩阵为b^t * b
    print "b_new:\n",b_new

    x_eigvals = np.linalg.eigvals(b_new)  # 求b_new矩阵的特征值
    print "x_eigvals:\n",x_eigvals

    print LA.norm(b, 1)  # 列范数
    print LA.norm(b, 2)  # 谱范数,为x里最大值开平方
    print LA.norm(b, np.inf)  # 无穷范数,行范数
    print LA.norm(b, "fro")  # F范数
print "数组转换为矩阵及矩阵求逆:\n"
compute_norm()
print "向量范数相关运算:\n"
vector_norm()
print "矩阵范数相关运算:\n"
matrix_norm()

结果如下:

数组转换为矩阵及矩阵求逆:

mat:
[[1 2]
 [3 4]]
inv_mat:
[[-2.   1. ]
 [ 1.5 -0.5]]
向量范数相关运算:

[-4 -3 -2 -1  0  1  2  3  4]
4.0
0.0
20.0
7.74596669241
矩阵范数相关运算:

[-4 -3 -2 -1  0  1  2  3  4]
b:
[[-4 -3 -2]
 [-1  0  1]
 [ 2  3  4]]
b_t:
[[-4 -1  2]
 [-3  0  3]
 [-2  1  4]]
b_new:
[[21 18 15]
 [18 18 18]
 [15 18 21]]
x_eigvals:
[  5.40000000e+01   6.00000000e+00   5.99792822e-16]
7.0
7.34846922835
9.0
7.74596669241

Process finished with exit code 0

numpy中mat和python的list转换例子

>>> import numpy
>>> a=[[1,2],[3,4],[5,6]]
>>> a
[[1, 2], [3, 4], [5, 6]]
>>> b=numpy.mat(a)
>>> b
matrix([[1, 2],
        [3, 4],
        [5, 6]])
>>> c=b.tolist()
>>> c
[[1, 2], [3, 4], [5, 6]]

参考:https://blog.csdn.net/bitcarmanlee/article/details/51945271

猜你喜欢

转载自blog.csdn.net/helloxiaozhe/article/details/80522541