# 【干货】用于机器学习的线性代数速查表

NumPy，Python的数值计算库，它提供了许多线性代数函数。对机器学习从业人员用处很大。

1. 数组

2. 矢量

3. 矩阵

4. 矩阵的类型

5. 矩阵运算

6. 矩阵分解

7. 统计

### 1.数组

```from numpyimport array
A= array([[1,2,3],[1,2,3],[1,2,3]])```

```from numpyimport zeros
A= zeros([3,5])```

`from numpyimport ones`
`A= ones([5,5])`

### 2.矢量

`c= a+ b`

`c= a- b`

`c= a* b`

`c= a/ b`

`c= a.dot(b)`

`c= a* 2.2`

```from numpy.linalgimport norm
l2= norm(v)```

### 3.矩阵

`C= A+ B`

`C= A- B`

`C= A* B`

`C= A/ B`

`C= A.dot(B)`

`C= A.dot(b)`

`C= A.dot(2.2)`

### 4.矩阵的类型

```# lower
from numpyimport tril
lower= tril(M)
# upper
from numpyimport triu
upper= triu(M)```

```from numpyimport diag
d= diag(M)```

from numpy import identity I = identity(3)

### 5.矩阵运算

`B= A.T`

```from numpy.linalgimport inv
B= inv(A)```

```from numpyimport trace
B= trace(A)```

```from numpy.linalgimport det
B= det(A)```

```from numpy.linalgimport matrix_rank
r= matrix_rank(A)```

### 6.矩阵分解

LU分解

```from scipy.linalgimport lu
P, L, U= lu(A)```

QR分解

```from numpy.linalgimport qr
Q, R= qr(A,'complete')```

```from numpy.linalgimport eig
values, vectors= eig(A)```

```from scipy.linalgimport svd
U, s, V= svd(A)```

### 7.统计

```from numpyimport mean
result= mean(v)```

```from numpyimport var
result= var(v, ddof=1)```

```from numpyimport std
result= std(v, ddof=1)```

```from numpyimport cov
sigma= cov(v1, v2)```

from numpy.linalg import lstsq b = lstsq(X, y)

### 附：

NumPy API

• 线性代数：https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html
• 统计：https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.statistics.html

• https://minireference.com/static/tutorials/linear_algebra_in_4_pages.pdf
• https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet
• https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_SciPy_Cheat_Sheet_Linear_Algebra.pdf

1576 篇文章83 人订阅

0 条评论

## 相关文章

### word2vec原理(二) 基于Hierarchical Softmax的模型

word2vec原理(二) 基于Hierarchical Softmax的模型

1952

1682

2875

6767

3967

1402

1622

2105

3587

3775