numpy nditer用法

深度学习中, 我们经常会遇到nditer用法.NumPy 迭代器对象 numpy. nditer 提供了一种灵活访问一个或者多个数组元素的方式。迭代器最基本的任务的可以完成对数组元素的访问。

示例代码

  • flags=['multi_index']表示对aar1进行多重索引
  • op_flags=['readwrite']表示不仅可以进行读,还可以进行写, 也就是创建迭代器的时候就已经规定了权限
  • pprint(it.multi_index)表示输出元素的索引, 可以看到结果都是index
  • it.iternext()表示进入下一次diedai,如果不加这一条语句的化, 输出结果会一直为(0,0)

以上实例不是使用标准 C 或者 Fortran 顺序,选择的顺序是和数组内存布局一致的,这样做是为了提升访问的效率,默认是行序优先(row-major order,或者说是 C-order)。

这反映了默认情况下只需访问每个元素,而无需考虑其特定顺序。我们可以通过迭代上述数组的转置来看到这一点,并与以 C 顺序访问数组转置的 copy 方式做对比,如下实例:

从上述例子可以看出,aar1 和 aaar1.T 的遍历顺序是一样的,也就是他们在内存中的存储顺序也是一样的,但是 aar1.T.copy(order = 'C') 的遍历结果是不同的,那是因为它和前两种的存储方式是不一样的,默认是按行访问。

API 参考

class numpy.nditer[source]

Efficient multi-dimensional iterator object to iterate over arrays. To get started using this object, see the introductory guide to array iteration.

Parameters:
op : ndarray or sequence of array_like

The array(s) to iterate over.

flags : sequence of str, optional

Flags to control the behavior of the iterator.

  • “buffered” enables buffering when required.
  • “c_index” causes a C-order index to be tracked.
  • “f_index” causes a Fortran-order index to be tracked.
  • “multi_index” causes a multi-index, or a tuple of indices with one per iteration dimension, to be tracked.
  • “common_dtype” causes all the operands to be converted to a common data type, with copying or buffering as necessary.
  • “copy_if_overlap” causes the iterator to determine if read operands have overlap with write operands, and make temporary copies as necessary to avoid overlap. False positives (needless copying) are possible in some cases.
  • “delay_bufalloc” delays allocation of the buffers until a reset() call is made. Allows “allocate” operands to be initialized before their values are copied into the buffers.
  • “external_loop” causes the values given to be one-dimensional arrays with multiple values instead of zero-dimensional arrays.
  • “grow_inner” allows the value array sizes to be made larger than the buffer size when both “buffered” and “external_loop” is used.
  • “ranged” allows the iterator to be restricted to a sub-range of the iterindex values.
  • “refs_ok” enables iteration of reference types, such as object arrays.
  • “reduce_ok” enables iteration of “readwrite” operands which are broadcasted, also known as reduction operands.
  • “zerosize_ok” allows itersize to be zero.
op_flags : list of list of str, optional

This is a list of flags for each operand. At minimum, one of “readonly”, “readwrite”, or “writeonly” must be specified.

  • “readonly” indicates the operand will only be read from.
  • “readwrite” indicates the operand will be read from and written to.
  • “writeonly” indicates the operand will only be written to.
  • “no_broadcast” prevents the operand from being broadcasted.
  • “contig” forces the operand data to be contiguous.
  • “aligned” forces the operand data to be aligned.
  • “nbo” forces the operand data to be in native byte order.
  • “copy” allows a temporary read-only copy if required.
  • “updateifcopy” allows a temporary read-write copy if required.
  • “allocate” causes the array to be allocated if it is None in the op parameter.
  • “no_subtype” prevents an “allocate” operand from using a subtype.
  • “arraymask” indicates that this operand is the mask to use for selecting elements when writing to operands with the ‘writemasked’ flag set. The iterator does not enforce this, but when writing from a buffer back to the array, it only copies those elements indicated by this mask.
  • ‘writemasked’ indicates that only elements where the chosen ‘arraymask’ operand is True will be written to.
  • “overlap_assume_elementwise” can be used to mark operands that are accessed only in the iterator order, to allow less conservative copying when “copy_if_overlap” is present.
op_dtypes : dtype or tuple of dtype(s), optional

The required data type(s) of the operands. If copying or buffering is enabled, the data will be converted to/from their original types.

order : {‘C’, ‘F’, ‘A’, ‘K’}, optional

Controls the iteration order. ‘C’ means C order, ‘F’ means Fortran order, ‘A’ means ‘F’ order if all the arrays are Fortran contiguous, ‘C’ order otherwise, and ‘K’ means as close to the order the array elements appear in memory as possible. This also affects the element memory order of “allocate” operands, as they are allocated to be compatible with iteration order. Default is ‘K’.

casting : {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional

Controls what kind of data casting may occur when making a copy or buffering. Setting this to ‘unsafe’ is not recommended, as it can adversely affect accumulations.

  • ‘no’ means the data types should not be cast at all.
  • ‘equiv’ means only byte-order changes are allowed.
  • ‘safe’ means only casts which can preserve values are allowed.
  • ‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.
  • ‘unsafe’ means any data conversions may be done.
op_axes : list of list of ints, optional

If provided, is a list of ints or None for each operands. The list of axes for an operand is a mapping from the dimensions of the iterator to the dimensions of the operand. A value of -1 can be placed for entries, causing that dimension to be treated as “newaxis”.

itershape : tuple of ints, optional

The desired shape of the iterator. This allows “allocate” operands with a dimension mapped by op_axes not corresponding to a dimension of a different operand to get a value not equal to 1 for that dimension.

buffersize : int, optional

When buffering is enabled, controls the size of the temporary buffers. Set to 0 for the default value.

Notes

nditer supersedes flatiter. The iterator implementation behind nditer is also exposed by the NumPy C API.

The Python exposure supplies two iteration interfaces, one which follows the Python iterator protocol, and another which mirrors the C-style do-while pattern. The native Python approach is better in most cases, but if you need the iterator’s coordinates or index, use the C-style pattern.

标签:

About: kiah