KXLau

V1

2022/04/13阅读：16主题：橙心

工具-numpy

numpy是使用Python进行数据科学的基础库。numpy以一个强大的N维数组对象为中心，它还包含有用的线性代数，傅里叶变换和随机数函数。

一维ndarray

``import numpy as np``

``a = np.array([1, 5, 3, 19, 13, 7, 3])a[3]``

``````19
``````
``a[2:5]``

``````array([ 3, 19, 13])
``````
``a[2:-1]``

``````array([ 3, 19, 13,  7])
``````
``a[:2]``

``````array([1, 5])
``````
``a[2::2]``

``````array([ 3, 13,  3])
``````
``a[::-1]``

``````array([ 3,  7, 13, 19,  3,  5,  1])
``````

``a[3] = 999a``

``````array([  1,   5,   3, 999,  13,   7,   3])
``````

``a[2:5] = [997, 998, 999]a``

``````array([  1,   5, 997, 998, 999,   7,   3])
``````

与常规数组的区别

``a[2:5] = -1a``

``````array([ 1,  5, -1, -1, -1,  7,  3])
``````

``try:    a[2:5] = [1, 2, 3, 4, 5, 6]except ValueError as e:    print(e)``

``````cannot copy sequence with size 6 to array axis with dimension 3
``````

``try:    del a[2:5] except ValueError as e:    print(e)``

``````cannot delete array elements
``````

``a_slice = a[2:6]a_slice[1] = 1000a   # 原始ndarray也被修改！``

``````array([   1,    5,   -1, 1000,   -1,    7,    3])
``````
``a[3] = 2000a_slice  # 修改切片也会修改原始ndarray！``

``````array([  -1, 2000,   -1,    7])
``````

``another_slice = a[2:6].copy()another_slice[1] = 3000a     # 原始ndarray不变``

``````array([   1,    5,   -1, 2000,   -1,    7,    3])
``````
``a[3] = 4000another_slice    # 修改原始ndarray不会影响切片的副本``

``````array([  -1, 3000,   -1,    7])
``````

多维ndarray

``b = np.arange(48).reshape(4, 12)b``

``````array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]])
``````
``b[1, 2]   # 第1行 第2列``

``````14
``````
``b[1, :]  # 第1行的所有列元素``

``````array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
``````
``b[:, 1]   # 第1列的所有行元素``

``````array([ 1, 13, 25, 37])
``````

``b[1, :]``

``````array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
``````
``b[1:2, :]``

``````array([[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
``````

花式索引

``b[(0, 2), 2:5]``

``````array([[ 2,  3,  4],
[26, 27, 28]])
``````
``b[:, (-1, 2, -1)]``

``````array([[11,  2, 11],
[23, 14, 23],
[35, 26, 35],
[47, 38, 47]])
``````

``b[(-1, 2, -1, 2), (5, 9, 1, 9)]   # 返回由b[-1, 5] b[2, 9] b[-1, 1] b[2, 9]组成的一维数组``

``````array([41, 33, 37, 33])
``````

更高维数组

``c = b.reshape(4, 2, 6)c``

``````array([[[ 0,  1,  2,  3,  4,  5],
[ 6,  7,  8,  9, 10, 11]],

[[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]],

[[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]],

[[36, 37, 38, 39, 40, 41],
[42, 43, 44, 45, 46, 47]]])
``````
``c[2, 1, 4]``

``````34
``````
``c[2, :, 3]``

``````array([27, 33])
``````

``c[2, 1]``

``````array([30, 31, 32, 33, 34, 35])
``````

省略号

``c[2, ...]   # 相当于c[2, :, :]``

``````array([[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
``````
``c[2, 1, ...]   # 相当于c[2, 1, :]``

``````array([30, 31, 32, 33, 34, 35])
``````
``c[2, ..., 3]    # 相当于c[2, :, 3]``

``````array([27, 33])
``````
``c[..., 3]   # 相当于c[:, :, 3]``

``````array([[ 3,  9],
[15, 21],
[27, 33],
[39, 45]])
``````

布尔索引

``d = np.arange(48).reshape(4, 12)d``

``````array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]])
``````
``rows_on = np.array([True, False, True, False])b[rows_on, :]   # 相当于b[(0, 2), :]``

``````array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
[24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]])
``````
``cols_on = np.array([False, True, False] * 4)b[:, cols_on]   # 相当于b[:, (1, 4, 7, 10)]``

``````array([[ 1,  4,  7, 10],
[13, 16, 19, 22],
[25, 28, 31, 34],
[37, 40, 43, 46]])
``````

np.ix_

``d[np.ix_(rows_on, cols_on)]``

``````array([[ 1,  4,  7, 10],
[25, 28, 31, 34]])
``````
``np.ix_(rows_on, cols_on)``

``````(array([[0],
[2]], dtype=int64), array([[ 1,  4,  7, 10]], dtype=int64))
``````

``b[b % 3 == 1]``

``````array([ 1,  4,  7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46])
``````

V1