numpy cheatsheet

numpy cheatsheet

1 创建矩阵

1.1 创建向量

1
2
3
>>> import numpy as np
>>> np.array([1,2])
array([1, 2])

1.2 创建矩阵

1
2
3
>>> np.array([[1,2],[3,4]])
array([[1, 2],
[3, 4]])

1.3 创建0元素矩阵

1
2
3
4
>>> np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

1.4 创建随机值填充矩阵

1
2
3
4
5
6
>>> np.random.randint(3,6,(5,5))
array([[3, 4, 5, 3, 3],
[3, 5, 3, 4, 4],
[5, 5, 3, 3, 3],
[5, 4, 4, 3, 3],
[5, 3, 3, 3, 3]])

1.5 创建单位矩阵

1
2
3
4
5
>>> np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

2 矩阵属性

2.1 矩阵大小

1
2
3
4
5
6
7
8
>>> import numpy as np
>>> arr = np.array([[1,2],[3,4]])
>>> arr
array([[1, 2],
[3, 4]])

>>> arr.shape
(2, 2)

2.2 矩阵行列式

1
2
>>> np.linalg.det(arr)
-2.0000000000000004

2.3 矩阵的秩

1
2
3
4
5
6
>>> np.linalg.matrix_rank(arr)
2

>>> not_full_mar = np.array([[1,2],[2,4]])
>>> np.linalg.matrix_rank(not_full_mar)
1

2.4 矩阵元素的数据类型

1
2
3
4
5
6
7
>>> arr.dtype
dtype('int32')

# 改变矩阵元素的数据类型
>>> arr.astype(float)
array([[1., 2.],
[3., 4.]])

3 访问矩阵

3.1 访问第0行

1
2
3
4
5
6
7
8
9
10
11
>>> import numpy as np
>>> arr = np.random.randint(0,10,(5,5))
>>> arr
array([[6, 1, 8, 7, 1],
[4, 8, 8, 1, 7],
[2, 8, 9, 4, 1],
[3, 9, 3, 3, 5],
[0, 1, 8, 6, 8]])

>>> arr[0,:]
array([6, 1, 8, 7, 1])

3.2 访问第0列

1
2
>>> arr[:,0]
array([6, 4, 2, 3, 0])

3.3 访问第0-2行,第1到3行

1
2
3
4
>>> arr[0:3, 1:4]
array([[1, 8, 7],
[8, 8, 1],
[8, 9, 4]])

3.4 访问第3行第3列的元素

1
2
>>> arr[3,3]
3

4 elementwise操作

4.1 逐元素加法

1
2
3
4
5
>>> import numpy as np
>>> arr = np.array([[1,2],[3,4]])
>>> arr + 3
array([[4, 5],
[6, 7]])

4.2 逐元素乘法

1
2
3
>>> arr * 2
array([[2, 4],
[6, 8]])

4.3 逐元素对数 底数默认为e

1
2
3
>>> np.log(arr)
array([[0. , 0.69314718],
[1.09861229, 1.38629436]])

4.4 逐元素平方

1
2
3
>>> np.square(arr)
array([[ 1, 4],
[ 9, 16]], dtype=int32)

4.5 矩阵求和

1
2
>>> np.sum(arr)
10

4.6 矩阵每列求和

1
2
>>> np.sum(arr, axis=0)
array([4, 6])

4.7 矩阵每行求和

1
2
>>> np.sum(arr, axis=1)
array([3, 7])

4.8 矩阵与矩阵逐元素相乘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
[3, 4]])
>>> b = np.array([[5],[6]])
>>> b
array([[5],
[6]])
>>> np.multiply(a,b)
array([[ 5, 10],
[18, 24]])

>>> b = np.array([[5,6]])
>>> b
array([[5, 6]])
>>> np.multiply(a,b)
array([[ 5, 12],
[15, 24]])

>>> b = np.array([[5,7],[6,8]])
>>> np.multiply(a,b)
array([[ 5, 14],
[18, 32]])

4.9 带if/else的mapping

1
2
3
4
5
>>> import numpy as np
>>> arr1 = np.array([[1,2],[3,4]])
>>> np.where(arr1 > 2.5, arr1 * 2, arr1 * 3)
array([[3, 6],
[6, 8]])

4.10 使用mask的mapping

1
2
3
4
5
6
>>> import numpy as np
>>> arr1 = np.array([[1,2],[3,4]])
>>> arr[arr > 1] = 4
>>> arr1
array([[1, 4],
[4, 4]])

5 矩阵逆/转置

1
2
>>> import numpy as np
>>> arr = np.array([[1,2],[3,4]])

5.1 转置

1
2
3
>>> arr.T
array([[1, 3],
[2, 4]])

5.2 逆矩阵

1
2
3
>>> np.linalg.inv(arr)
array([[-2. , 1. ],
[ 1.5, -0.5]])

6 调整矩阵大小

6.1 reshape 调整前后元素个数不能改变

1
2
3
4
5
6
>>> import numpy as np
>>> arr = np.array([[1,2],[3,4]])


>>> np.reshape(arr, (1,4))
array([[1, 2, 3, 4]])

6.2 resize 调整前后元素个数可以改变

1
2
3
4
5
>>> np.resize(arr, (1,4))
array([[1, 2, 3, 4]])

>>> np.resize(arr,(1,2))
array([[1, 2]])

6.3 向量转为矩阵 reshape中(-1,1)中的-1代表该维度不变

1
2
3
4
5
6
7
8
9
>>> vec = np.array([1,3])
>>> vec.shape
(2,)
>>> vec.reshape((-1,1))
array([[1],
[3]])
>>> vec = vec.reshape((-1,1))
>>> vec.shape
(2, 1)

7 矩阵乘法

1
2
3
4
5
6
7
>>> import numpy as np
>>> arr = np.array([[1,2],[3,4]])

>>> arr2 = np.array([[2,3],[4,5]])
>>> np.matmul(arr, arr2)
array([[10, 13],
[22, 29]])

8 拼接矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> import numpy as np
>>> arr1 = np.array([[1,2],[3,4]])

>>> arr2 = np.array([[2,3],[4,5]])
>>> np.vstack((arr1, arr2))
array([[1, 2],
[3, 4],
[2, 3],
[4, 5]])
>>> np.concatenate((arr1, arr2), axis=0)

array([[1, 2],
[3, 4],
[2, 3],
[4, 5]])
>>> np.concatenate((arr1, arr2), axis=1)

array([[1, 2, 2, 3],
[3, 4, 4, 5]])

9 遍历矩阵

1
2
3
4
5
6
7
8
9
10
import numpy as np
>>> arr1 = np.array([[1,2],[3,4]])
>>> for row in arr1:
print(row)
[1 2]
[3 4]
>>> for column in arr1.T:
print(column)
[1 3]
[2 4]