numpy的求模模块 linalg在范数运算中的应用
来源:互联网 发布:免费交朋友软件 编辑:程序博客网 时间:2024/06/06 02:53
refer: http://blog.csdn.net/bitcarmanlee/article/details/51945271
1.范数(norm)的意义
要更好的理解范数,就要从函数、几何与矩阵的角度去理解。
我们都知道,函数与几何图形往往是有对应的关系,这个很好想象,特别是在三维以下的空间内,函数是几何图像的数学概括,而几何图像是函数的高度形象化,比如一个函数对应几何空间上若干点组成的图形。
但当函数与几何超出三维空间时,就难以获得较好的想象,于是就有了映射的概念,映射表达的就是一个集合通过某种关系转为另外一个集合。通常数学书是先说映射,然后再讨论函数,这是因为函数是映射的一个特例。
为了更好的在数学上表达这种映射关系,(这里特指线性关系)于是就引进了矩阵。这里的矩阵就是表征上述空间映射的线性关系。而通过向量来表示上述映射中所说的这个集合,而我们通常所说的基,就是这个集合的最一般关系。于是,我们可以这样理解,一个集合(向量),通过一种映射关系(矩阵),得到另外一个几何(另外一个向量)。
那么向量的范数,就是表示这个原有集合的大小。
而矩阵的范数,就是表示这个变化过程的大小的一个度量。
总结起来一句话,范数(norm),是具有“长度”概念的函数。
2.范数满足的三个特性
1.非负性:
2.齐次性:
3.三角不等式:
3.向量的范数
1-范数,计算方式为向量所有元素的绝对值之和。
2-范数,计算方式跟欧式距离的方式一致。
4.矩阵的范数
首先假设矩阵的大小为
1-范数,又名列和范数。顾名思义,即矩阵列向量中绝对值之和的最大值。
2-范数,又名谱范数,计算方法为
其中
F-范数,Frobenius范数,计算方式为矩阵元素的绝对值的平方和再开方。
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) """
看到上面这个表以后,同学们应该特别清楚了吧。
- numpy的求模模块 linalg在范数运算中的应用
- numpy的linalg模块
- numpy矩阵相关和线性代数linalg模块
- numpy linalg
- Numpy入门学习之(二)linalg库----向量范数、矩阵范数、行列式、矩阵逆、幂
- numpy.linalg学习
- 范数与距离的关系以及在机器学习中的应用
- 范数与距离的关系以及在机器学习中的应用
- 范数与距离的关系以及在机器学习中的应用
- 范数与距离的关系以及在机器学习中的应用
- 求模(mod)运算的应用
- 4.2 线性代数linalg模块
- pyspark.linalg模块学习
- 用于线性代数的Ruby模块linalg 0.32发布
- numpy中的random模块
- numpy中的随机数模块
- Python的Numpy库中的 nonzero函数,及应用在 hardlim硬限幅函数中
- python3 中numpy模块的一些应用(一)
- 性能测试
- 个人信用,你了解多少?
- Redis的数据类型
- maven学习笔记(一) maven简介以及安装
- sewise视频直播
- numpy的求模模块 linalg在范数运算中的应用
- 学习记录---颤抖吧,hadoop!(三)----搭建伪分布式
- android NDK 之cmake的使用
- Feedforward Deep Networks(要点)
- Flask上下文机制的理解
- Sprinboot基础优酷篇 (三)热部署
- hdu 3183 A Magic Lamp(RMQ)
- 将aar发布到Jcenter仓库遇到的问题及解决方案
- JavaScript 专题之如何判断两个对象相等