1 NumPy version
1 | import numpy as np |
NumPy( Numerical Python) 是 Python 数值计算最重要的基础库,核心是 N 维数组对象 ndarray ( N-dimensional array )。
2 Create ndarray
1 | import numpy as np |
4 Special ndarray
1 | import numpy as np |
零矩阵
1
a1 = np.zeros((3,4)) # 零矩阵
矩阵
1
a2 = np.ones((3,4)) # 1 矩阵
空矩阵
1
a3 = np.empty((3,4)) # 未初始化的空矩阵
对角矩阵
1
2a7 = np.identity(5) # 5x5 的对角矩阵
a8 = np.mat(np.identity(5))线段
1
2
3a5 = np.arange(12)
a6 = np.linspace(1, 10, 5) # 5 个元素的线段
print(a6 < 5) # 返回布尔类型的矩阵对角矩阵
1
2a7 = np.identity(5) # 5x5 的对角矩阵
a8 = np.mat(np.identity(5))随机矩阵
1
2
3a = np.random.random((2, 4)) # 0-1的随机数
print(np.sum(a), np.min(a), np.max(a))
np.sum(a, axis = 0) # 列操作,axis = 1行操作,the default, axis = None,will sum all of the elements of the input array
4 Array operations
4.1 Preparation
1 | import numpy as np |
Result: 1
2
3
4[[10 20]
[30 40]]
[[0 1]
[2 3]]
4.2 Mathematical operation
Plus & minus
1
2c1 = a + b
c2 = a - bMultiply & divide
1
2c3 = a * b # 对应元素相乘
c4 = b / adot
1
2c5 = np.dot(a, b) # 矩阵相乘,点乘
c6 = a.dot(b) # 和上式相同,a 值不改变
4.3 Conditional selection
where
1
2
3
4
5import numpy as np
a = np.array([[10, 20], [30, 40]])
row, col = np.where(a == 20)
print(row, col, sep = '\t')Reuslt:
1
[0] [1]
5 Array basic methods
导入模块
1
import numpy as np
统计特征
上述函数皆默认1
2
3
4
5
6# find elements
A = np.arange(2, 14).reshape(3, 4)
index = np.argmax(A) # 最大值索引or argmin
mean = np.mean(A) # 均值
median = np.median(A) # 中位数
print(index, mean, median, sep = '\n')axis = None, the index is into the flattened array
,若添加参数:axis = 1
则返回每一行的相关操作,axis = 0
则返回每一列的相关操作,具体参照:help(np.mean)
。累加和差分
1
2
3
4
5# A:3x4 的数组
cumsum = np.cumsum(A)
# 累加,axis默认为None,输出1x12数组
diff = np.diff(A)
# 差分,默认axis=-1,即行操作与axis=1效果相同,返回3X3的数组,axis=0,返回2x4数组查找数据
查找非零元素的索引,返回两个array,第一个为行索引,第二个为列索引,输出:1
non = np.nonzero(A)
1
2(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),
array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))矩阵转置
1
2
3
4
5# 矩阵转置, transpose array
transpose1 = np.transpose(A)
transpose2 = A.T
print(transpose1, transpose2, sep = '\n')
print(transpose1.dot(A)) # ${A * A^T}$数据裁剪
clip
Given an interval [5, 9], values outside this interval are clipped to this interval edges, namely 5 and 9. Result:1
2
3a = np.arange(2, 14).reshape(3,4)
clip = np.clip(a, 5, 9)
print(a, clip, sep = '\n')1
2
3array([[5, 5, 5, 5],
[6, 7, 8, 9],
[9, 9, 9, 9]])迭代输出
Result:1
2
3
4
5
6
7
8
9
10
11
12a = np.arange(2, 14).reshape(3,4)
# 行迭代输出
for row in a:
print(row)
# 列迭代输出
for column in a.T:
print(column.T)
# 元素迭代, flat返回迭代器,flatten()返回array
for item in a.flat: # or a.flatten()
print(item)1
2
3
4# Row
array([[5, 5, 5, 5],
[6, 7, 8, 9],
[9, 9, 9, 9]])
6 Array joint
6.1 vstack and hstack
1 | a = np.array([1, 1, 1]) |
6.2 concatenate
Join a sequence of arrays along a existing axis, default axis is o, if axis = None, arrays will be flattened before use.
1 | import numpy as np |
7 Array split
1 | import numpy as np |
- np.split(ary, indices_or sections, axis = 0)
Array to be divided into multiple sub-arrays along the given 'axis ', if such split is not possible, then an error will be rasied.
8 Array copy
- Copy and deep copy
1 | import numpy as np |