经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » NumPy » 查看文章
NumPy迭代数组的实现
来源:jb51  时间:2023/2/20 15:16:30  对本文有异议

迭代数组

NumPy中引入了 nditer 对象来提供一种对于数组元素的访问方式。

一、单数组迭代

1. 使用 nditer 访问数组的每个元素

  1. >>>a = np.arange(12).reshape(3, 4)
  2. >>>for x in np.nditer(a):
  3. ?? ??? ??? ?print(x, end=' ')
  4. 0 1 2 3 4 5 6 7 8 9 10 11?
  5.  
  6. # 以上实例不是使用标准 C 或者 Fortran 顺序,选择的顺序是和数组内存布局一致的,
  7. # 这样做是为了提升访问的效率,默认是行序优先(row-major order,或者说是 C-order)。
  8. # 这反映了默认情况下只需访问每个元素,而无需考虑其特定顺序。
  9. # 我们可以通过迭代上述数组的转置来看到这一点,
  10. # 并与以 C 顺序访问数组转置的 copy 方式做对比,如下实例:
  11. >>>for x in np.nditer(a.T):
  12. ?? ??? ??? ?print(x, end=' ')
  13. 0 1 2 3 4 5 6 7 8 9 10 11?
  14.  
  15. >>>for x in np.nditer(a.T.copy(order='C')):
  16. ?? ??? ??? ?print(x, end=' ')
  17. 0 4 8 1 5 9 2 6 10 3 7 11?

2. 控制数组元素的迭代顺序

使用参数 order 控制元素的访问顺序,参数的可选值有:

  • ‘C’:C order,即是行序优先;
  • ‘F’:Fortran order,即是列序优先;
  • ’K’:参考数组元素在内存中的顺序;
  • ‘A’:表示’F’顺序;
  1. >>>a = np.arange(12).reshape(3, 4)
  2. >>>for x in np.nditer(a, order='C'):
  3. ? ? ?? ?print(x, end=' ')
  4. 0 1 2 3 4 5 6 7 8 9 10 11?
  5.  
  6. >>>a = np.arange(12).reshape(3, 4)
  7. >>>for x in np.nditer(a, order='F'):
  8. ? ? ?? ?print(x, end=' ')
  9. 0 4 8 1 5 9 2 6 10 3 7 11?
  10.  
  11. >>>a = np.arange(12).reshape(3, 4)
  12. >>>for x in np.nditer(a, order='K'):
  13. ? ? ?? ?print(x, end=' ')
  14. 0 1 2 3 4 5 6 7 8 9 10 11?
  15.  
  16. >>>a = np.arange(12).reshape(3, 4)
  17. >>>for x in np.nditer(a, order='A'):
  18. ? ? ?? ?print(x, end=' ')
  19. 0 1 2 3 4 5 6 7 8 9 10 11?

3. 修改数组值

在使用 nditer 对象迭代数组时,默认情况下是只读状态。因此,如果需要修改数组,可以使用参数 op_flags = 'readwrite' or 'writeonly' 来标志为读写或只读模式。

此时,nditer 在迭代时将生成可写的缓冲区数组,可以在此进行修改。为了在修改后,可以将修改的数据回写到原始位置,需要在迭代结束后,抛出迭代结束信号,有两种方式:

  • 使用 with 上下文管理器;
  • 在迭代结束后,调用迭代器的close方法;
  1. >>>a = np.arange(12).reshape(3, 4)
  2. >>>print(a)
  3. >>>with np.nditer(a, op_flags=['readwrite']) as it:
  4. for x in it:
  5. x += 10
  6. >>>print(a)
  7. [[ 0 1 2 3]
  8. [ 4 5 6 7]
  9. [ 8 9 10 11]]
  10. [[10 11 12 13]
  11. [14 15 16 17]
  12. [18 19 20 21]]

4. 使用外部循环,跟踪索引或多索引

以上操作在迭代过程中,都是逐元素进行的,这虽然简单,但是效率不高。可以使用参数 flags 让 nditer 迭代时提供更大的块。并可以通过强制设定 C 和 F 顺序,得到不同的块大小。

  1. # 默认情况下保持本机的内存顺序,迭代器提供单一的一维数组
  2. # 'external_loop' 给出的值是具有多个值的一维数组,而不是零维数组
  3. >>>a = np.arange(12).reshape(3, 4)
  4. >>>print(a)
  5. >>>for x in np.nditer(a, flags=['external_loop']):
  6. ? ? ?? ?print(x, end=' ')
  7. [[ 0 ?1 ?2 ?3]
  8. ?[ 4 ?5 ?6 ?7]
  9. ?[ 8 ?9 10 11]]
  10. [ 0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 11],?
  11.  
  12. # 设定 'F' 顺序
  13. >>>a = np.arange(12).reshape(3, 4)
  14. >>>print(a)
  15. >>>for x in np.nditer(a, flags=['external_loop'], order='F'):
  16. ? ? ?? ?print(x, end=' ')
  17. [[ 0 ?1 ?2 ?3]
  18. ?[ 4 ?5 ?6 ?7]
  19. ?[ 8 ?9 10 11]]
  20. [0 4 8], [1 5 9], [ 2 ?6 10], [ 3 ?7 11],?
  21.  
  22. # 'c_index' 可以通过 it.index 跟踪 'C‘ 顺序的索引
  23. >>>a = np.arange(12).reshape(3, 4)
  24. >>>print(a)
  25. >>>it = np.nditer(a, flags=['c_index'])
  26. >>>for x in it:
  27. ? ??? ? ?? ?print("{}: ({})".format(x, it.index))
  28. [[ 0 ?1 ?2 ?3]
  29. ?[ 4 ?5 ?6 ?7]
  30. ?[ 8 ?9 10 11]]
  31. 0: (0)
  32. 1: (1)
  33. 2: (2)
  34. 3: (3)
  35. 4: (4)
  36. 5: (5)
  37. 6: (6)
  38. 7: (7)
  39. 8: (8)
  40. 9: (9)
  41. 10: (10)
  42. 11: (11)
  43.  
  44. # 'f_index' 可以通过 it.index 跟踪 'F‘ 顺序的索引
  45. >>>a = np.arange(12).reshape(3, 4)
  46. >>>print(a)
  47. >>>it = np.nditer(a, flags=['c_index'])
  48. >>>for x in it:
  49. ? ??? ? ?? ?print("{}: ({})".format(x, it.index))
  50. [[ 0 ?1 ?2 ?3]
  51. ?[ 4 ?5 ?6 ?7]
  52. ?[ 8 ?9 10 11]]
  53. 0: (0)
  54. 1: (3)
  55. 2: (6)
  56. 3: (9)
  57. 4: (1)
  58. 5: (4)
  59. 6: (7)
  60. 7: (10)
  61. 8: (2)
  62. 9: (5)
  63. 10: (8)
  64. 11: (11)
  65.  
  66. # 'multi_index' 可以通过 it.multi_index 跟踪数组索引
  67. >>>a = np.arange(12).reshape(3, 4)
  68. >>>print(a)
  69. >>>it = np.nditer(a, flags=['multi_index'])
  70. >>>for x in it:
  71. ? ? ?? ?print("{}: {}".format(x, it.multi_index))
  72. [[ 0 ?1 ?2 ?3]
  73. ?[ 4 ?5 ?6 ?7]
  74. ?[ 8 ?9 10 11]]
  75. 0: (0, 0)
  76. 1: (0, 1)
  77. 2: (0, 2)
  78. 3: (0, 3)
  79. 4: (1, 0)
  80. 5: (1, 1)
  81. 6: (1, 2)
  82. 7: (1, 3)
  83. 8: (2, 0)
  84. 9: (2, 1)
  85. 10: (2, 2)
  86. 11: (2, 3)

external_loop 与 multi_index、c_index、c_index不可同时使用,否则将引发错误 ValueError: Iterator flag EXTERNAL_LOOP cannot be used if an index or multi-index is being tracked

5. 以特定数据类型迭代

当需要以其它的数据类型来迭代数组时,有两种方法:

  • 临时副本:迭代时,会使用新的数据类型创建数组的副本,然后在副本中完成迭代。但是,这种方法会消耗大量的内存空间。
  • 缓冲模式: 使用缓冲来支持灵活输入,内存开销最小。
  1. # 临时副本
  2. >>>a = np.arange(12).reshape(3, 4)
  3. >>>print(a.dtype)
  4. >>>it = np.nditer(a, op_flags=['readonly', 'copy'],op_dtypes=[np.float64])
  5. >>>for x in it:
  6. ? ? ?? ?print("{}".format(x), end=', ')
  7. int32
  8. 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,
  9.  
  10. # 缓冲模式
  11.  
  12. >>>a = np.arange(12).reshape(3, 4)
  13. >>>print(a.dtype)
  14. >>>it = np.nditer(a, flags=['buffered'],op_dtypes=[np.float64])
  15. >>>for x in it:
  16. ? ? ?? ?print("{}".format(x), end=', ')
  17. int32
  18. 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,?

注意
默认情况下,转化会执行“安全”机制,如果不符合 NumPy 的转换规则,会引发异常:TypeError: Iterator operand 0 dtype could not be cast from dtype('float64') to dtype('float32') according to the rule 'safe'

二、广播数组迭代

如果不同形状的数组是可广播的,那么 dtype 可以迭代多个数组。

  1. >>> a = np.arange(3)
  2. >>> b = np.arange(6).reshape(2,3)
  3. >>> for x, y in np.nditer([a,b]):
  4. print("%d:%d" % (x,y), end=' ')
  5. 0:0 1:1 2:2 0:3 1:4 2:5

到此这篇关于NumPy迭代数组的实现的文章就介绍到这了,更多相关NumPy迭代数组内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号