Scipy教程 - python数值计算库

版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主允许不得转载。 https://blog.csdn.net/pipisorry/article/details/43277755

http://blog.csdn.net/pipisorry/article/details/43277755

Linear Algebra(scipy.linalg) scipy线性代数库简介

When SciPy is built using the optimized ATLAS LAPACK and BLAS libraries, it has very fast linear algebra capabilities.
If you dig deep enough, all of the raw lapack and blas libraries are available for your use for even more speed.

All of these linear algebra routines expect an object that can be converted into a 2-dimensional array. The output of these routines is also a two-dimensional array.

numpy.linalg和scipy.linalg的比较取舍

当然,在numpy中也有线性代数库Linear algebra (numpy.linalg),但是推荐用scipy.linalg代替numpy.linalg

原因是:scipy.linalg contains all the functions in numpy.linalg. plus some other more advanced ones not contained in numpy.linalg;

Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for numpy this is optional. Therefore, the scipy version might be faster depending on how numpy was installed.

numpy.matrix和2D numpy.ndarray的比较取舍

matrix使用起来更方便快捷,但是并不推荐,原因是:the use of the numpy.matrix class is discouraged, since it adds nothing that cannot be accomplished with 2D numpy.ndarray objects, and may lead to a confusion of which class is being used.

scipy.linalg operations can be applied equally to numpy.matrix or to 2D numpy.ndarray objects.

Note:

1. 在你计算一些统计量时就会了解到这个,比如计算协方差cov函数,输入参数可以是矩阵也可以是二维数组,但是返回值是二维数组,这样如果你使用矩阵,就要来回转换类型,相当不方便!(lz亲历)

2. 矩阵运算在二维数组中都有相应替换,如矩阵点乘用二维数组替换为A.dot(b.T) #matrix multiplication

引入import

from scipy import linalg

Note: 不能使用下面的代码调用linalg.eig()

import scipy as sp

sp.linalg.eig(M)

原因见[Scipy教程 - python数值计算库]

皮皮blog



numpy中线性代数库的使用

numpy中有的库函数可能在scipy中没有对应的,或者是lz没找到

计算矩阵的秩

 >>> import numpy as np 
 >>> I = np.eye(3)#先创建一个单位阵 
 >>> I 
 array([[ 1., 0., 0.],  [ 0., 1., 0.],  [ 0., 0., 1.]]) 
 >>> np.linalg.matrix_rank(I)#秩 
3
 >>> I[1, 1] = 0#将该元素置为0 
 >>> I 
array([[ 1., 0., 0.],  [ 0., 0., 0.], [ 0., 0., 1.]])
 >>> np.linalg.matrix_rank(I)#此时秩变成2 

 2

其它

np.linalg.cond(a,p=None)    #矩阵a的条件数

d1=np.linalg.companion(a)    #伴随矩阵
d2=np.linalg.triu()/tril()   #作用同MATLAB中的同名函数
fliplr()/flipud()/rot90()    #功能类似MATLAB同名函数。
xx=np.roll(x,2)   #roll()是循环移位函数。此调用表示向右循环移动2位。

矩阵的数学函数

numpy的子包linalg中专门处理矩阵的数学函数。np.linalg.logm(A)计算矩阵A的对数,这个处理和MATLAB是类似的,使用一个m后缀表示是矩阵的运算。在这个空间内可以使用的有cosm() /sinm() /signm() /sqrtm()等。其中常规exp()对应有三种矩阵形式:expm()使用Pade近似算法、expm2()使用特征值分析算法、expm3()使用泰勒级数算法。

皮皮blog



scipy中线性代数库linalg:

Linalg Basic routines基本例行

求逆

linalg.inv (A)

Solving linear system解线性方程组

np.linalg.solve(A,b)

检查计算结果正确性:A.dot(np.linalg.solve(A,b))-b #check

Note:

1. 上面这个方程的解等价于linalg.inv(A).dot(b),只是直接这样解比较慢。

2. 从1可知这里系数矩阵A必须是方阵,否则这个方程是个超定方程,要使用下面的命令linalg.lstsq计算最小二乘解。

A不是方阵会出错:numpy.linalg.linalg.LinAlgError: Last 2 dimensions of the array must be square

Finding Determinant行列式计算

linalg.det(A)

Computing norms范式计算

linalg.norm(A)

向量范式

矩阵范式

求解线性最小二乘问题和伪逆Solving linear least-squares problems and pseudo-inverses

linalg.lstsq

例:c,resid,rank,sigma = linalg.lstsq(A,zi)

其中c就是方程的解

也可以使用下面这个命令来解

linalg.pinv or linalg.pinv2(uses a different method based on singular value decomposition)

Generalized inverse广义逆

linalg.pinv or linalg.pinv2

摩尔·彭罗斯广义逆矩阵(Moore-Penrose pseudoinverse)可以使用linalg模块中的pinv函数进行求解。计算广义逆矩阵需要用到奇异值分解。inv函数只接受方阵作为输入矩阵,而pinv函数则没有这个限制。

皮皮blog



Decompositions分解

特征值和特征向量Eigenvalues and eigenvectors

linalg.eig(A)    返回(特征值,特征向量)的元组,注意eig(A)返回的特征向量矩阵的列i对应于第i个特征值,而不是行

linalg.eigvals()    只返回特征值

奇异值分解Singular value decomposition 

linalg.svd()

注意返回值为U*S*Vh,其中S只是对角矩阵对角线元素组成的数组而不是对角矩阵,当然可以通过np.diag(S)创建,再通过np.allclose(a, (u.dot(np.diag(e)).dot(v)))测试分解后是否可以重建。

但是也可以直接通过np.allclose(a, np.dot(U, np.dot(S, Vh)))来测试。


LU decomposition    lu()

Cholesky decomposition    cholesky()

QR decomposition    qr()

Schur decomposition    schur()

插值分解Interpolative Decomposition
皮皮blog



矩阵方法Matrix Functions

...

特殊矩阵Special matrices

...

[scipy-ref-0.14.0-p71]

皮皮blog



稀疏线性代数Sparse linear algebra

{scipy.sparse.linalg}

Abstract linear operators

Matrix Operations

Matrix norms

Solving linear problems

Matrix factorizations

Exceptions

[scipy-ref-0.14.0-p861]

皮皮blog

from: http://blog.csdn.net/pipisorry/article/details/43277755

ref: Linear Algebra (scipy.linalg)*

http://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html


猜你喜欢

转载自blog.csdn.net/pipisorry/article/details/43277755