np.Linear algebra学习
转自:https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html
1.分解
//其中我觉得可以的就是svd奇异值分解吧,虽然并不知道数学原理
np.linalg.svd(a, full_matrices=1, compute_uv=1)
a是要分解的(M,N)array;
full_matrices : bool, optional
If True (default), u and v have the shapes (M, M) and (N, N), respectively. Otherwise, the shapes are (M, K) and (K, N), respectively, where K = min(M, N).
当full_matrices是True时(默认):
>>> d=np.mat("4 11 14;8 7 -2")
>>> d
matrix([[ 4, 11, 14],
[ 8, 7, -2]])
>>> U,sigma,V=np.linalg.svd(d)
>>> U
matrix([[-0.9486833 , -0.31622777],
[-0.31622777, 0.9486833 ]])
>>> V
matrix([[-0.33333333, -0.66666667, -0.66666667],
[ 0.66666667, 0.33333333, -0.66666667],
[-0.66666667, 0.66666667, -0.33333333]])
>>> sigma
array([18.97366596, 9.48683298])
>>> U.shape,sigma.shape,V.shape
((2, 2), (2,), (3, 3))
>>> S=np.zeros((2,3))
>>> S[:2,:2]=np.diag(sigma)
>>> S
array([[18.97366596, 0. , 0. ],
[ 0. , 9.48683298, 0. ]])
>>> U*S*V
matrix([[ 4., 11., 14.],
[ 8., 7., -2.]])
当full_matrices是False时:
>>> U,sigma,V=np.linalg.svd(d,full_matrices=0)
>>> U
matrix([[-0.9486833 , -0.31622777],
[-0.31622777, 0.9486833 ]])
>>> sigma
array([18.97366596, 9.48683298])
>>> V
matrix([[-0.33333333, -0.66666667, -0.66666667],
[ 0.66666667, 0.33333333, -0.66666667]])
>>> S=np.diag(sigma)#####
>>> S
array([[18.97366596, 0. ],
[ 0. , 9.48683298]])
>>> U*S*V
matrix([[ 4., 11., 14.],
[ 8., 7., -2.]])
2.矩阵特征值
np.linalg.eig(a) Compute the eigenvalues and right eigenvectors of a square array.
>>> w,v=LA.eig(np.diag((1,2,3)))
>>> w
array([1., 2., 3.])
>>> v
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> np.diag((1,2,3))
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
np.linalg.eigvals(g):Compute the eigenvalues of a general matrix.
>>> w2=LA.eigvals(np.diag((1,2,3)))
>>> w2
array([1., 2., 3.])
3.范数和其他数字
3.1 np.linalg.norm(x, ord=None, axis=None, keepdims=False):Matrix or vector norm.
Using the axis argument to compute vector norms:axis用来计算矩阵中的向量范数。
>>> a=np.array([3,4])
>>> a
array([3, 4])
>>> LA.norm(a)
5.0
>>> LA.norm(a,ord=1)
7.0
>>> a=np.array([3,-4])
>>> LA.norm(a,ord=1)
7.0
>>> LA.norm(a,ord=np.inf)
4.0
>>> LA.norm(a,ord=-np.inf)
3.0
3.2 np.linalg.cond(x, p=None):Compute the condition number of a matrix.
>>> a=np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]])
>>> a
array([[ 1, 0, -1],
[ 0, 1, 0],
[ 1, 0, 1]])
>>> LA.cond(a)
1.4142135623730951
>>> LA.cond(a,2)
1.4142135623730951
>>> LA.cond(a,1)
2.0
//其中:
p : {None, 1, -1, 2, -2, inf, -inf, ‘fro’}, optional
Order of the norm:
p norm for matrices None 2-norm, computed directly using the SVD‘fro’ Frobenius norm inf max(sum(abs(x), axis=1)) -inf min(sum(abs(x), axis=1)) 1 max(sum(abs(x), axis=0)) -1 min(sum(abs(x), axis=0)) 2 2-norm (largest sing. value) -2 smallest singular value inf means the numpy.inf object, and the Frobenius norm is the root-of-sum-of-squares norm.
使用的范数,默认是L2范数。
3.3 np.linalg.det(a):Compute the determinant of an array.
>>> a = np.array([[1, 2], [3, 4]])
>>> LA.det(a)
-2.0000000000000004
3.4 np.linalg.matrix_rank(M, tol=None):Return matrix rank of array using SVD method
>>> LA.matrix_rank(np.eye(4))
4
>>> I=np.eye(4)
>>> I[-1,-1]=0
>>> I
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 0.]])
>>> LA.matrix_rank(I)
3
3.5 trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)
>>> np.trace(np.eye(4))
4.0
矩阵对角线上的和。
4.解方程和逆矩阵

4.1 np.linalg.solve(a,b):Solve a linear matrix equation, or system of linear scalar equations.
Solve the system of equations 3 * x0 + x1 = 9 and x0 + 2 * x1 = 8:
>>> a = np.array([[3,1], [1,2]])
>>> b = np.array([9,8])
>>> x = np.linalg.solve(a, b)
>>> x
array([ 2., 3.])
check:
>>> np.allclose(np.dot(a, x), b)
True
4.2 np.linalg.lstsq(a, b, rcond=-1):Return the least-squares solution to a linear matrix equation
最小二乘求解。
np.Linear algebra学习的更多相关文章
- PYTHON替代MATLAB在线性代数学习中的应用(使用Python辅助MIT 18.06 Linear Algebra学习)
前言 MATLAB一向是理工科学生的必备神器,但随着中美贸易冲突的一再升级,禁售与禁用的阴云也持续笼罩在高等学院的头顶.也许我们都应当考虑更多的途径,来辅助我们的学习和研究工作. 虽然PYTHON和众 ...
- Python Linear algebra
Linear algebra 1.模块文档 NAME numpy.linalg DESCRIPTION Core Linear Algebra Tools ---------------------- ...
- 读Linear Algebra -- Gilbert Strang
转眼间我的学士学位修读生涯已经快要到期了,重读线性代数,一是为了重新理解Algebra的的重要概念以祭奠大一刷过的计算题,二是为了将来的学术工作先打下一点点(薄弱的)基础.数学毫无疑问是指导着的科研方 ...
- 【线性代数】Linear Algebra Big Picture
Abstract: 通过学习MIT 18.06课程,总结出的线性代数的知识点相互依赖关系,后续博客将会按照相应的依赖关系进行介绍.(2017-08-18 16:28:36) Keywords: Lin ...
- Linear Algebra From Data
Linear Algebra Learning From Data 1.1 Multiplication Ax Using Columns of A 有关于矩阵乘法的理解深入 矩阵乘法理解为左侧有是一 ...
- Linear Algebra lecture1 note
Professor: Gilbert Strang Text: Introduction to Linear Algebra http://web.mit.edu/18.06 Lecture 1 ...
- 算法库:基础线性代数子程序库(Basic Linear Algebra Subprograms,BLAS)介绍
调试DeepFlow光流算法,由于作者给出的算法是基于Linux系统的,所以要在Windows上运行,不得不做大量的修改工作.移植到Windows平台,除了一些头文件找不到外,还有一些函数也找不到.这 ...
- 线性代数导论 | Linear Algebra 课程
搞统计的线性代数和概率论必须精通,最好要能锻炼出直觉,再学机器学习才会事半功倍. 线性代数只推荐Prof. Gilbert Strang的MIT课程,有视频,有教材,有习题,有考试,一套学下来基本就入 ...
- Here’s just a fraction of what you can do with linear algebra
Here’s just a fraction of what you can do with linear algebra The next time someone wonders what the ...
随机推荐
- GDI+绘制五星红旗
五星红旗是由红色背景,加5个黄色五角星组成.绘制一个五星红旗的思路,就是先定义一个五角星的自定义控件,然后通过设置五角星的大小.位置.旋转角度等属性,组合成一个五星红旗. 五角星自定义控件代码: pu ...
- 【CF553E】Kyoya and Train 最短路+cdq分治+FFT
[CF553E]Kyoya and Train 题意:有一张$n$个点到$m$条边的有向图,经过第i条边要花$c_i$元钱,经过第i条边有$p_{i,k}$的概率要耗时k分钟.你想从1走到n,但是如果 ...
- 词云绘制wordcloud
wordcloud是优秀的第三方词云展示库,该库以空格为分割线,按照单词出现的频率自动设置字号与颜色实例如下 import wordcloud#词云库 import jieba#分词库 a=open( ...
- Eclipse中代码格式化配置
一.配置formatter 从Eclipse主菜单选择“窗口→首选项”,进入“代码格式化程序”设置页.如下图所示: 确认选择的是格式化配置是Eclipse [built-in]. 注意:编写好代码后需 ...
- 实际体验 .NET Standard 2.0 的魅力
在我们的 .net core 大迁移工程中,有些项目完成了迁移,有些还未迁移,这就带来了一个烦恼——我们自己开发的公用类库如何在 .net core 与 .net framework 项目中共享?如果 ...
- .NET Core 中 IOptions 有什么用
我只发现IOptions的一个用处——方便了在.NET Core应用程序中使用强类型配置. 如果没有IOptions,使用强类型配置需要自己解决下面2个问题: 1)将配置文件(比如appsetting ...
- centos 阿里云 安装VNC Viewer
https://help.aliyun.com/knowledge_detail/41530.html 这个东西非常的不安全,极其容易造成密码账号丢失.非常容易导致各类远程攻击,切记...
- 依赖: nginx-common (= 1.14.0-0ubuntu1) 但是它将不会被安装
.apt --fix-broken install .sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件. .sudo apt-get ...
- day7:set和深浅copy
1,判断字符串是不是空格isspace函数 s1 = ' ' s2 = ' ssss' print(s1.isspace()) print(s2.isspace()) 运行结果: True False ...
- php之函数
scope(空间) unpack (解压) Traversable (穿越) performance(性能) experiment (检验) properties (属性) trailing (尾随) ...