0


Python-np.expand_dims()

1. np.expand_dims

用于扩展数组的维度

执行程序后注意观察中括号[ ]的位置和数量

np.expand_dims(a, axis=0)表示在axis=0维度处扩展维度,加一层中括号[ ];

np.expand_dims(a, axis=1)表示在axis=1维度处扩展维度,加一层中括号[ ];

np.expand_dims(a, axis=2)表示在axis=2维度处扩展维度,加一层中括号[ ];

np.expand_dims(a, axis=-1)表示在axis=-1(最后)维度处扩展维度,加一层中括号[ ];

  1. (py3.6) E:\PYTHON>ipython
  2. Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)]
  3. Type 'copyright', 'credits' or 'license' for more information
  4. IPython 7.16.3 -- An enhanced Interactive Python. Type '?' for help.
  5. In [1]: import numpy as np
  6. In [2]: a = np.array([[1, 2, 3],[4, 5, 6]])
  7. In [3]: a
  8. Out[3]:
  9. array([[1, 2, 3],
  10. [4, 5, 6]])
  11. In [4]: a.shape
  12. Out[4]: (2, 3)

1.1 axis=0

  1. In [5]: b = np.expand_dims(a, axis=0)
  2. In [6]: b
  3. Out[6]:
  4. array([[[1, 2, 3],
  5. [4, 5, 6]]])
  6. In [7]: b.shape
  7. Out[7]: (1, 2, 3)

1.2 axis=1

  1. In [8]: c = np.expand_dims(a, axis=1)
  2. In [9]: c
  3. Out[9]:
  4. array([[[1, 2, 3]],
  5. [[4, 5, 6]]])
  6. In [10]: c.shape
  7. Out[10]: (2, 1, 3)

1.3 axis=2

  1. In [11]: d = np.expand_dims(a, axis=2)
  2. In [12]: d
  3. Out[12]:
  4. array([[[1],
  5. [2],
  6. [3]],
  7. [[4],
  8. [5],
  9. [6]]])
  10. In [13]: d.shape
  11. Out[13]: (2, 3, 1)

1.4 axis=-1

  1. In [14]: e = np.expand_dims(a, axis=-1)
  2. In [15]: e
  3. Out[15]:
  4. array([[[1],
  5. [2],
  6. [3]],
  7. [[4],
  8. [5],
  9. [6]]])
  10. In [16]: e.shape
  11. Out[16]: (2, 3, 1)

1.5 axis=3

  1. In [17]: f = np.expand_dims(a, axis=3)
  2. ---------------------------------------------------------------------------
  3. AxisError Traceback (most recent call last)
  4. <ipython-input-16-d7316647942f> in <module>
  5. ----> 1 f = np.expand_dims(a, axis=3)
  6. <__array_function__ internals> in expand_dims(*args, **kwargs)
  7. D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\lib\site-packages\numpy\lib\shape_base.py in expand_dims(a, axis)
  8. 595
  9. 596 out_ndim = len(axis) + a.ndim
  10. --> 597 axis = normalize_axis_tuple(axis, out_ndim)
  11. 598
  12. 599 shape_it = iter(a.shape)
  13. D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\lib\site-packages\numpy\core\numeric.py in normalize_axis_tuple(axis, ndim, argname, allow_duplicate)
  14. 1325 pass
  15. 1326 # Going via an iterator directly is slower than via list comprehension.
  16. -> 1327 axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
  17. 1328 if not allow_duplicate and len(set(axis)) != len(axis):
  18. 1329 if argname:
  19. D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\lib\site-packages\numpy\core\numeric.py in <listcomp>(.0)
  20. 1325 pass
  21. 1326 # Going via an iterator directly is slower than via list comprehension.
  22. -> 1327 axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
  23. 1328 if not allow_duplicate and len(set(axis)) != len(axis):
  24. 1329 if argname:
  25. AxisError: axis 3 is out of bounds for array of dimension 3

实例:

素材:注意程序中用的是相对路径

  1. import tensorflow as tf
  2. tf.compat.v1.disable_eager_execution()
  3. import numpy as np
  4. from tensorflow.keras.preprocessing import image
  5. from skimage import io
  6. image_path="../images/lena.jpg"
  7. # 加载图像
  8. img = io.imread(image_path)
  9. # 显示图像
  10. io.imshow(img)
  11. io.show()
  12. # 图像预处理
  13. x = image.img_to_array(img)
  14. y = np.expand_dims(x, axis = 0)
  15. print("x:", x)
  16. print("y:", y)
  17. print(x.shape)
  18. print(y.shape)

结果:

  1. D:\RuanJianAnZhunangWeiZhi\anaconda\anaconda3\envs\py3.6\python.exe E:\Python_files\Tensorflow_dl\TensorFlowProgramming\TensorFlowProgramming\ch6CNN\pool_op_demo2.py
  2. 2023-04-04 08:24:33.571665: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
  3. 2023-04-04 08:24:33.571878: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
  4. x: [[[221. 139. 125.]
  5. [221. 139. 125.]
  6. [222. 139. 125.]
  7. ...
  8. [223. 136. 108.]
  9. [216. 131. 102.]
  10. [211. 126. 95.]]
  11. [[221. 139. 125.]
  12. [221. 139. 125.]
  13. [222. 139. 125.]
  14. ...
  15. [225. 139. 112.]
  16. [217. 132. 103.]
  17. [211. 126. 97.]]
  18. [[223. 140. 124.]
  19. [223. 140. 124.]
  20. [223. 140. 124.]
  21. ...
  22. [225. 141. 117.]
  23. [214. 130. 104.]
  24. [206. 122. 96.]]
  25. ...
  26. [[ 81. 26. 58.]
  27. [ 83. 28. 60.]
  28. [ 84. 27. 59.]
  29. ...
  30. [161. 72. 90.]
  31. [163. 75. 91.]
  32. [163. 75. 91.]]
  33. [[ 80. 25. 56.]
  34. [ 82. 27. 58.]
  35. [ 83. 26. 58.]
  36. ...
  37. [164. 76. 92.]
  38. [167. 76. 93.]
  39. [167. 76. 93.]]
  40. [[ 80. 25. 56.]
  41. [ 81. 26. 57.]
  42. [ 82. 25. 57.]
  43. ...
  44. [166. 78. 94.]
  45. [169. 78. 95.]
  46. [169. 78. 95.]]]
  47. y: [[[[221. 139. 125.]
  48. [221. 139. 125.]
  49. [222. 139. 125.]
  50. ...
  51. [223. 136. 108.]
  52. [216. 131. 102.]
  53. [211. 126. 95.]]
  54. [[221. 139. 125.]
  55. [221. 139. 125.]
  56. [222. 139. 125.]
  57. ...
  58. [225. 139. 112.]
  59. [217. 132. 103.]
  60. [211. 126. 97.]]
  61. [[223. 140. 124.]
  62. [223. 140. 124.]
  63. [223. 140. 124.]
  64. ...
  65. [225. 141. 117.]
  66. [214. 130. 104.]
  67. [206. 122. 96.]]
  68. ...
  69. [[ 81. 26. 58.]
  70. [ 83. 28. 60.]
  71. [ 84. 27. 59.]
  72. ...
  73. [161. 72. 90.]
  74. [163. 75. 91.]
  75. [163. 75. 91.]]
  76. [[ 80. 25. 56.]
  77. [ 82. 27. 58.]
  78. [ 83. 26. 58.]
  79. ...
  80. [164. 76. 92.]
  81. [167. 76. 93.]
  82. [167. 76. 93.]]
  83. [[ 80. 25. 56.]
  84. [ 81. 26. 57.]
  85. [ 82. 25. 57.]
  86. ...
  87. [166. 78. 94.]
  88. [169. 78. 95.]
  89. [169. 78. 95.]]]]
  90. (512, 512, 3)
  91. (1, 512, 512, 3)
  92. Process finished with exit code 0

参考文章:

https://blog.csdn.net/qq_37924224/article/details/119816771

https://blog.csdn.net/hong615771420/article/details/83448878


本文转载自: https://blog.csdn.net/aaaccc444/article/details/129940622
版权归原作者 天寒心亦热 所有, 如有侵权,请联系我们删除。

“Python-np.expand_dims()”的评论:

还没有评论