|
1 | | -============================ |
| 1 | +======================== |
2 | 2 | 3.9 大型数组运算 |
3 | | -============================ |
| 3 | +======================== |
4 | 4 |
|
5 | 5 | ---------- |
6 | 6 | 问题 |
7 | 7 | ---------- |
8 | | -todo... |
| 8 | +你需要在大数据集(比如数组或网格)上面执行计算。 |
| 9 | + |
| 10 | +| |
9 | 11 |
|
10 | 12 | ---------- |
11 | 13 | 解决方案 |
12 | 14 | ---------- |
13 | | -todo... |
| 15 | +涉及到数组的重量级运算操作,可以使用NumPy库。 |
| 16 | +NumPy的一个主要特征是它会给Python提供一个数组对象,相比标准的Python列表而已更适合用来做数学运算。 |
| 17 | +下面是一个简单的小例子,向你展示标准列表对象和NumPy数组对象之间的差别: |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + >>> # Python lists |
| 22 | + >>> x = [1, 2, 3, 4] |
| 23 | + >>> y = [5, 6, 7, 8] |
| 24 | + >>> x * 2 |
| 25 | + [1, 2, 3, 4, 1, 2, 3, 4] |
| 26 | + >>> x + 10 |
| 27 | + Traceback (most recent call last): |
| 28 | + File "<stdin>", line 1, in <module> |
| 29 | + TypeError: can only concatenate list (not "int") to list |
| 30 | + >>> x + y |
| 31 | + [1, 2, 3, 4, 5, 6, 7, 8] |
| 32 | +
|
| 33 | + >>> # Numpy arrays |
| 34 | + >>> import numpy as np |
| 35 | + >>> ax = np.array([1, 2, 3, 4]) |
| 36 | + >>> ay = np.array([5, 6, 7, 8]) |
| 37 | + >>> ax * 2 |
| 38 | + array([2, 4, 6, 8]) |
| 39 | + >>> ax + 10 |
| 40 | + array([11, 12, 13, 14]) |
| 41 | + >>> ax + ay |
| 42 | + array([ 6, 8, 10, 12]) |
| 43 | + >>> ax * ay |
| 44 | + array([ 5, 12, 21, 32]) |
| 45 | + >>> |
| 46 | +
|
| 47 | +正如所见,两种方案中数组的基本数学运算结果并不相同。 |
| 48 | +特别的,numpy中的标量运算(比如ax * 2或ax + 10)会作用在每一个元素上。 |
| 49 | +另外,当两个操作数都是数组的时候执行元素对等位置计算,并最终生成一个新的数组。 |
| 50 | + |
| 51 | +对整个数组中所有元素同时执行数学运算可以使得作用在整个数组上的函数运算简单而又快速。 |
| 52 | +比如,如果你想计算多项式的值,可以这样做: |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + >>> def f(x): |
| 57 | + ... return 3*x**2 - 2*x + 7 |
| 58 | + ... |
| 59 | + >>> f(ax) |
| 60 | + array([ 8, 15, 28, 47]) |
| 61 | + >>> |
| 62 | +
|
| 63 | +NumPy还为数组操作提供了大量的通用函数,这些函数可以作为math模块中类似函数的替代。比如: |
| 64 | + |
| 65 | +.. code-block:: python |
| 66 | +
|
| 67 | + >>> np.sqrt(ax) |
| 68 | + array([ 1. , 1.41421356, 1.73205081, 2. ]) |
| 69 | + >>> np.cos(ax) |
| 70 | + array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362]) |
| 71 | + >>> |
| 72 | +
|
| 73 | +使用这些通用函数要比循环数组并使用math模块中的函数执行计算要快的多。 |
| 74 | +因此,只要有可能的话尽量选择numpy的数组方案。 |
| 75 | + |
| 76 | +底层实现中,NumPy数组使用了C或者Fortran语言的机制分配内存。 |
| 77 | +也就是说,它们是一个非常大的连续的并由同类型数据组成的内存区域。 |
| 78 | +所以,你可以构造一个比普通Python列表大的多的数组。比如,如果你想构造一个10,000*10,000的浮点数二维网格,很轻松: |
| 79 | + |
| 80 | +.. code-block:: python |
| 81 | +
|
| 82 | + >>> grid = np.zeros(shape=(10000,10000), dtype=float) |
| 83 | + >>> grid |
| 84 | + array([[ 0., 0., 0., ..., 0., 0., 0.], |
| 85 | + [ 0., 0., 0., ..., 0., 0., 0.], |
| 86 | + [ 0., 0., 0., ..., 0., 0., 0.], |
| 87 | + ..., |
| 88 | + [ 0., 0., 0., ..., 0., 0., 0.], |
| 89 | + [ 0., 0., 0., ..., 0., 0., 0.], |
| 90 | + [ 0., 0., 0., ..., 0., 0., 0.]]) |
| 91 | + >>> |
| 92 | +
|
| 93 | +所有的普通操作还是会同时作用在所有元素上: |
| 94 | + |
| 95 | +.. code-block:: python |
| 96 | +
|
| 97 | + >>> grid += 10 |
| 98 | + >>> grid |
| 99 | + array([[ 10., 10., 10., ..., 10., 10., 10.], |
| 100 | + [ 10., 10., 10., ..., 10., 10., 10.], |
| 101 | + [ 10., 10., 10., ..., 10., 10., 10.], |
| 102 | + ..., |
| 103 | + [ 10., 10., 10., ..., 10., 10., 10.], |
| 104 | + [ 10., 10., 10., ..., 10., 10., 10.], |
| 105 | + [ 10., 10., 10., ..., 10., 10., 10.]]) |
| 106 | + >>> np.sin(grid) |
| 107 | + array([[-0.54402111, -0.54402111, -0.54402111, ..., -0.54402111, |
| 108 | + -0.54402111, -0.54402111], |
| 109 | + [-0.54402111, -0.54402111, -0.54402111, ..., -0.54402111, |
| 110 | + -0.54402111, -0.54402111], |
| 111 | + [-0.54402111, -0.54402111, -0.54402111, ..., -0.54402111, |
| 112 | + -0.54402111, -0.54402111], |
| 113 | + ..., |
| 114 | + [-0.54402111, -0.54402111, -0.54402111, ..., -0.54402111, |
| 115 | + -0.54402111, -0.54402111], |
| 116 | + [-0.54402111, -0.54402111, -0.54402111, ..., -0.54402111, |
| 117 | + -0.54402111, -0.54402111], |
| 118 | + [-0.54402111, -0.54402111, -0.54402111, ..., -0.54402111, |
| 119 | + -0.54402111, -0.54402111]]) |
| 120 | + >>> |
| 121 | +
|
| 122 | +关于NumPy有一点需要特别的主意,那就是它扩展Python列表的索引功能 - 特别是对于多维数组。 |
| 123 | +为了说明清楚,先构造一个简单的二维数组并试着做些试验: |
| 124 | + |
| 125 | +.. code-block:: python |
| 126 | +
|
| 127 | + >>> a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) |
| 128 | + >>> a |
| 129 | + array([[ 1, 2, 3, 4], |
| 130 | + [ 5, 6, 7, 8], |
| 131 | + [ 9, 10, 11, 12]]) |
| 132 | +
|
| 133 | + >>> # Select row 1 |
| 134 | + >>> a[1] |
| 135 | + array([5, 6, 7, 8]) |
| 136 | +
|
| 137 | + >>> # Select column 1 |
| 138 | + >>> a[:,1] |
| 139 | + array([ 2, 6, 10]) |
| 140 | +
|
| 141 | + >>> # Select a subregion and change it |
| 142 | + >>> a[1:3, 1:3] |
| 143 | + array([[ 6, 7], |
| 144 | + [10, 11]]) |
| 145 | + >>> a[1:3, 1:3] += 10 |
| 146 | + >>> a |
| 147 | + array([[ 1, 2, 3, 4], |
| 148 | + [ 5, 16, 17, 8], |
| 149 | + [ 9, 20, 21, 12]]) |
| 150 | +
|
| 151 | + >>> # Broadcast a row vector across an operation on all rows |
| 152 | + >>> a + [100, 101, 102, 103] |
| 153 | + array([[101, 103, 105, 107], |
| 154 | + [105, 117, 119, 111], |
| 155 | + [109, 121, 123, 115]]) |
| 156 | + >>> a |
| 157 | + array([[ 1, 2, 3, 4], |
| 158 | + [ 5, 16, 17, 8], |
| 159 | + [ 9, 20, 21, 12]]) |
| 160 | +
|
| 161 | + >>> # Conditional assignment on an array |
| 162 | + >>> np.where(a < 10, a, 10) |
| 163 | + array([[ 1, 2, 3, 4], |
| 164 | + [ 5, 10, 10, 8], |
| 165 | + [ 9, 10, 10, 10]]) |
| 166 | + >>> |
| 167 | +
|
| 168 | +| |
14 | 169 |
|
15 | 170 | ---------- |
16 | 171 | 讨论 |
17 | 172 | ---------- |
18 | | -todo... |
| 173 | +NumPy是Python领域中很多科学与工程库的基础,同时也是被广泛使用的最大最复杂的模块。 |
| 174 | +即便如此,在刚开始的时候通过一些简单的例子和玩具程序也能帮我们完成一些有趣的事情。 |
| 175 | + |
| 176 | +通常我们导入NumPy模块的时候会使用语句import numpy as np。 |
| 177 | +这样的话你就不用再你的程序里面一遍遍的敲入numpy,只需要输入np就行了,节省了不少时间。 |
| 178 | + |
| 179 | +如果想获取更多的信息,你当然得去NumPy官网逛逛了,网址是: http://www.numpy.org |
| 180 | + |
0 commit comments