Numpy
1. 数组和数的运算
array1 = np.arange(1,10)
array1 # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array1 + 10 # array([11, 12, 13, 14, 15, 16, 17, 18, 19])
array1 - 10 # array([-9, -8, -7, -6, -5, -4, -3, -2, -1])
array1 * 10 # array([10, 20, 30, 40, 50, 60, 70, 80, 90])
array1 ** 10
array1 > 3 # array([False, False, False, True, True, True, True, True, True])
2. 数组和数组的运算
array2 = np.array([10,10,10,20,20,20,30,30,30])
array2 # array([10, 10, 10, 20, 20, 20, 30, 30, 30])
array2.shape # (9,)
array1 + array2 # array([11, 12, 13, 24, 25, 26, 37, 38, 39])
array1 * array2 # array([ 10, 20, 30, 80, 100, 120, 210, 240, 270])
array1 > array2 # array([False, False, False, False, False, False, False, False, False])
array1 ** array2
array3 = np.arange(1,10).reshape(3,3)
array3
'''
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
'''
array3.shape # (3, 3)
array4 = array2.reshape(3,3)
array4
'''
array([[10, 10, 10],
[20, 20, 20],
[30, 30, 30]])
'''
array4 / array3
array4 // array3
2.1 广播机制
如果参与运算的两个数组形状(shape)不一样,numpy会使用广播机制使形状一样 广播机制条件(二选一):
- 两个数组形状虽然不一样,但是后缘维度(shape属性从后往前看对应的部分)相同
- 两个数组形状不一样且后缘维度不相同,但是其中有一方在某个轴上的长度(size)为1
2.2 矩阵乘法
array9
'''
array([[1, 2],
[3, 4],
[5, 6]])
'''
array10
'''
array([[2, 8, 6, 5],
[7, 2, 2, 3]])
'''
array9 @ array10
np.matmul(array9,array10)
'''
array([[16, 12, 10, 11],
[34, 32, 26, 27],
[52, 52, 42, 43]])
'''
3.Numpy操作数组的函数
x = np.array([1,2,-3,0])
y = np.array([4,0,0,0])
z = x / y
z # array([0.25, inf, -inf, nan])
3.1 isinf
# 检测是否有无穷大值
np.isinf(z) # array([False, True, True, False])
# nan:not a number
array11 = np.array([10,np.nan,20,30])
array11 # array([10., nan, 20., 30.])
3.2 isnan:判断空值
# 判断空值
np.isnan(array11) # array([False, True, False, False])
# 排除空值
array11[~np.isnan(array11)] # array([10., 20., 30.])
3.3 矩阵点乘:dot
𝐴⋅𝐵=|𝐴||𝐵|𝐶𝑂𝑆𝜃
𝐶𝑂𝑆𝜃=𝐴⋅𝐵|𝐴||𝐵|
u = np.array([3,4])
v = np.array([5,1])
# 计算夹角余弦
# 模:np.linalg.norm(u)
np.dot(u,v) / np.linalg.norm(u) / np.linalg.norm(v)
# 计算角度
# 弧长:np.arccos
# 角度:np.rad2deg
np.rad2deg(np.arccos(np.dot(u,v) / np.linalg.norm(u) / np.linalg.norm(v)))
# 说明:夹角越小说明两个向量相似度越高
3.4 扁平化:flatten、ravel
array9
'''
array([[1, 2],
[3, 4],
[5, 6]])
'''
# 扁平化
array9.flatten() # array([1, 2, 3, 4, 5, 6])
array9.reshape(-1) # array([1, 2, 3, 4, 5, 6])
# 扁平化(性能更好)
array9.ravel() # array([1, 2, 3, 4, 5, 6])
3.5 in1d
array10 = np.array([22,44,55])
array11 = np.array([[66,44,22],[33,44,77]])
np.in1d(array10,array11)
'''
array([ True, True, False])
'''
3.6 交集,并集
𝐴∩𝐵元素的个数 / 𝐴∪𝐵元素的个数
- 交集:np.intersect1d
- 并集:np.union1d
user_a = np.array(['A','B','E','F','G','X','Y'])
user_b = np.array(['E','H','J','K','P','Q','X'])
user_c = np.array(['C','B','E','F','G','X','Y'])
np.intersect1d(user_a,user_b).size / np.union1d(user_a,user_b).size
np.intersect1d(user_a,user_c).size / np.union1d(user_a,user_c).size
np.intersect1d(user_b,user_c).size / np.union1d(user_b,user_c).size
3.7 拷贝
用copy拷贝后再修改,不会改变原数组
3.8 堆叠、拆分
- hstack、vstack、stack、concatenate、vsplit、hsplit
1)堆叠
array16 = np.array([[1,1,1],[2,2,2],[3,3,3]])
array17 = np.array([[4,4,4],[5,5,5],[6,6,6]])
# 1) hstack
np.hstack((array16,array17))
'''
array([[1, 1, 1, 4, 4, 4],
[2, 2, 2, 5, 5, 5],
[3, 3, 3, 6, 6, 6]])
'''
# 2) vstack
np.vstack((array16,array17))
'''
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
'''
# 3) stack
array15 = np.stack((array16,array17))
array15
# axis=0
'''
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]])
'''
np.stack((array16,array17), axis=1)
'''
array([[[1, 1, 1],
[4, 4, 4]],
[[2, 2, 2],
[5, 5, 5]],
[[3, 3, 3],
[6, 6, 6]]])
'''
2)拆分
array18 = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6]])
array18
'''
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
'''
# 垂直方向拆分(行)
np.vsplit(array18,3)
'''
[array([[1, 1, 1],
[2, 2, 2]]),
array([[3, 3, 3],
[4, 4, 4]]),
array([[5, 5, 5],
[6, 6, 6]])]
'''
# 水平方向拆分(列)
np.hsplit(array18,3)
'''
[array([[1],
[2],
[3],
[4],
[5],
[6]]),
array([[1],
[2],
[3],
[4],
[5],
[6]]),
array([[1],
[2],
[3],
[4],
[5],
[6]])]
'''
3)拼接
# 拼接(默认沿着0轴拼接)
np.concatenate((array16,array17))
'''
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
'''
np.concatenate((array16,array17),axis=1)
'''
array([[1, 1, 1, 4, 4, 4],
[2, 2, 2, 5, 5, 5],
[3, 3, 3, 6, 6, 6]])
'''
3.9 增加:append、insert
array16
'''
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
'''
np.append(array16,100) # array([ 1, 1, 1, 2, 2, 2, 3, 3, 3, 100])
np.append(array16,[1,2,3]) # array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3])
np.insert(array16,0,100) # array([100, 1, 1, 1, 2, 2, 2, 3, 3, 3])
np.insert(array16,1,[5,6,7])
# array([1, 5, 6, 7, 1, 1, 2, 2, 2, 3, 3, 3])
3.10 argwhere:查找非0元素索引
array16
'''
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
'''
np.argwhere(array16)
'''
array([[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2]],
'''
3.11 extract、select、where
array21 = np.arange(1,10)
array21
# array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 根据条件抽取数据(相当于布尔索引)
np.extract(array21 > 5,array21) # array([6, 7, 8, 9])
# np.select(condlist, choicelist, default=0)
# 根据条件处理数据得到新的数组数据
np.select([array21 < 3,array21>5],[array21+10,array21**2])
# array([11, 12, 0, 0, 0, 36, 49, 64, 81])
# 给出一个条件将满足不满足条件的数据粉被处理得到新的数组对象
np.where(array21 > 5, array21 + 10, array21 ** 2)
# array([ 1, 4, 9, 16, 25, 16, 17, 18, 19])
3.12 filp:翻转
np.flip(array21,axis=0)
# array([9, 8, 7, 6, 5, 4, 3, 2, 1])
array19 = np.stack((array16,array17), axis=1)
array19
'''
array([[[1, 1, 1],
[4, 4, 4]],
[[2, 2, 2],
[5, 5, 5]],
[[3, 3, 3],
[6, 6, 6]]])
'''
np.flip(array19,axis=1)
'''
array([[[4, 4, 4],
[1, 1, 1]],
[[5, 5, 5],
[2, 2, 2]],
[[6, 6, 6],
[3, 3, 3]]])
'''
np.flip(array19,axis=0)
'''
array([[[3, 3, 3],
[6, 6, 6]],
[[2, 2, 2],
[5, 5, 5]],
[[1, 1, 1],
[4, 4, 4]]])
'''
3.13 roll
- 沿着指定轴对数组移位
array21 # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.roll(array21,2,axis=0)
# array([8, 9, 1, 2, 3, 4, 5, 6, 7])
array19
'''
array([[[1, 1, 1],
[4, 4, 4]],
[[2, 2, 2],
[5, 5, 5]],
[[3, 3, 3],
[6, 6, 6]]])
'''
np.roll(array19,2,axis=0)
'''
array([[[2, 2, 2],
[5, 5, 5]],
[[3, 3, 3],
[6, 6, 6]],
[[1, 1, 1],
[4, 4, 4]]])
'''
np.roll(array19,1,axis=1)
'''
array([[[4, 4, 4],
[1, 1, 1]],
[[5, 5, 5],
[2, 2, 2]],
[[6, 6, 6],
[3, 3, 3]]])
'''
3.14 repeat、tile:重复
array21 # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 重复创建新的数组对象
np.repeat(array21,3)
# array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9])
np.tile(array21,3)
# array([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9])
3.15 resize:重新调整数组大小
array18
'''
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
'''
np.resize(array18,(3,4))
'''
array([[1, 1, 1, 2],
[2, 2, 3, 3],
[3, 4, 4, 4]])
'''
np.resize(array18,(8,4))
'''
array([[1, 1, 1, 2],
[2, 2, 3, 3],
[3, 4, 4, 4],
[5, 5, 5, 6],
[6, 6, 1, 1],
[1, 2, 2, 2],
[3, 3, 3, 4],
[4, 4, 5, 5]])
'''
3.16 put、place
- 将满足条件的元素/指定位置的元素替换成指定值
array22 = np.copy(array21)
array22 # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.put(array22, [0,4,6], [100,200])
array22 # array([100, 2, 3, 4, 200, 6, 100, 8, 9])
np.place(array22, array22 < 100, [33,66,77])
array22 # array([100, 33, 66, 77, 200, 33, 100, 66, 77])
[3, 4, 4, 4],
[5, 5, 5, 6],
[6, 6, 1, 1],
[1, 2, 2, 2],
[3, 3, 3, 4],
[4, 4, 5, 5]])
‘’’
### 3.16 put、place
+ 将满足条件的元素/指定位置的元素替换成指定值
```python
array22 = np.copy(array21)
array22 # array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.put(array22, [0,4,6], [100,200])
array22 # array([100, 2, 3, 4, 200, 6, 100, 8, 9])
np.place(array22, array22 < 100, [33,66,77])
array22 # array([100, 33, 66, 77, 200, 33, 100, 66, 77])