Numpy 简明教程
NumPy - Iterating Over Array
NumPy 包含一个迭代器对象 numpy.nditer 。这是一个高效的多维迭代器对象,可以使用它来遍历数组。使用 Python 的标准迭代器接口访问数组中的每个元素。
NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.
让我们使用 arange() 函数创建一个 3X4 数组,并使用 nditer 遍历它。
Let us create a 3X4 array using arange() function and iterate over it using nditer.
Example 1
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Modified array is:'
for x in np.nditer(a):
print x,
此程序的输出如下 -
The output of this program is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55
Example 2
迭代的顺序与数组的内存布局相匹配,无需考虑特定的排序。可以通过对上述数组的转置进行迭代来查看这一点。
The order of iteration is chosen to match the memory layout of an array, without considering a particular ordering. This can be seen by iterating over the transpose of the above array.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Transpose of the original array is:'
b = a.T
print b
print '\n'
print 'Modified array is:'
for x in np.nditer(b):
print x,
以上程序的输出如下:
The output of the above program is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55
Iteration Order
如果使用 F 样式顺序存储相同的元素,则迭代器将选择更有效的方式在数组上进行迭代。
If the same elements are stored using F-style order, the iterator chooses the more efficient way of iterating over an array.
Example 1
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Transpose of the original array is:'
b = a.T
print b
print '\n'
print 'Sorted in C-style order:'
c = b.copy(order = 'C')
print c
for x in np.nditer(c):
print x,
print '\n'
print 'Sorted in F-style order:'
c = b.copy(order = 'F')
print c
for x in np.nditer(c):
print x,
其输出如下所示 −
Its output would be as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Sorted in C-style order:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0 20 40 5 25 45 10 30 50 15 35 55
Sorted in F-style order:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0 5 10 15 20 25 30 35 40 45 50 55
Example 2
可以通过明确地提及 nditer 对象来强制其使用特定的顺序。
It is possible to force nditer object to use a specific order by explicitly mentioning it.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Sorted in C-style order:'
for x in np.nditer(a, order = 'C'):
print x,
print '\n'
print 'Sorted in F-style order:'
for x in np.nditer(a, order = 'F'):
print x,
其输出将为 −
Its output would be −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Sorted in C-style order:
0 5 10 15 20 25 30 35 40 45 50 55
Sorted in F-style order:
0 20 40 5 25 45 10 30 50 15 35 55
Modifying Array Values
nditer 对象有另一个称为 op_flags 的可选参数。其默认值是只读的,但可以设置为读写或只写模式。这将允许使用此迭代器修改数组元素。
The nditer object has another optional parameter called op_flags. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.
Example
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
for x in np.nditer(a, op_flags = ['readwrite']):
x[...] = 2*x
print 'Modified array is:'
print a
它的输出如下:
Its output is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
[[ 0 10 20 30]
[ 40 50 60 70]
[ 80 90 100 110]]
External Loop
nditer 类构造函数具有 ‘flags’ 参数,它可以采用以下值:
The nditer class constructor has a ‘flags’ parameter, which can take the following values −
Sr.No. |
Parameter & Description |
1 |
c_index C_order index can be tracked |
2 |
f_index Fortran_order index is tracked |
3 |
multi-index Type of indexes with one per iteration can be tracked |
4 |
external_loop Causes values given to be one-dimensional arrays with multiple values instead of zero-dimensional array |
Example
在下面的示例中,一维数组对应于每列由迭代器遍历。
In the following example, one-dimensional arrays corresponding to each column is traversed by the iterator.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'
print 'Modified array is:'
for x in np.nditer(a, flags = ['external_loop'], order = 'F'):
print x,
输出如下 −
The output is as follows −
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
[ 0 20 40] [ 5 25 45] [10 30 50] [15 35 55]
Broadcasting Iteration
如果两个数组是 broadcastable ,则一个合并的 nditer 对象能够同时在它们上进行迭代。假设数组 a 有 3X4 维,并且有另一个数组 b 有 1X4 维,则使用以下类型的迭代器(数组 b 被广播到 a 的大小)。
If two arrays are broadcastable, a combined nditer object is able to iterate upon them concurrently. Assuming that an array a has dimension 3X4, and there is another array b of dimension 1X4, the iterator of following type is used (array b is broadcast to size of a).
Example
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'First array is:'
print a
print '\n'
print 'Second array is:'
b = np.array([1, 2, 3, 4], dtype = int)
print b
print '\n'
print 'Modified array is:'
for x,y in np.nditer([a,b]):
print "%d:%d" % (x,y),
其输出如下所示 −
Its output would be as follows −
First array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Second array is:
[1 2 3 4]
Modified array is:
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4