np.stack(a,axis=x), x=0,1,2

import numpy as np

a = [
    np.array([[1, 2, 3], [4, 5, 6]]),           # A0
    np.array([[10, 20, 30], [40, 50, 60]]),     # A1
    np.array([[100, 200, 300], [400, 500, 600]]),  # A2
    np.array([[7, 8, 9], [10, 11, 12]]),        # A3
    np.array([[13, 14, 15], [16, 17, 18]]),     # A4
    np.array([[21, 22, 23], [24, 25, 26]])      # A5
]  # a 是一个 list,长度为 6,每个元素 shape = (2, 3)


print("====> np.stack(a, axis=0)")
out0 = np.stack(a, axis=0)
print(out0.shape)  # (6, 2, 3)
print(out0)

print("\n====> np.stack(a, axis=1)")
out1 = np.stack(a, axis=1)
print(out1.shape)  # (2, 6, 3)
print(out1)

print("\n====> np.stack(a, axis=2)")
out2 = np.stack(a, axis=2)
print(out2.shape)  # (2, 3, 6)
print(out2)

====> np.stack(a, axis=0)
(6, 2, 3)
[[[  1   2   3]
  [  4   5   6]]

 [[ 10  20  30]
  [ 40  50  60]]

 [[100 200 300]
  [400 500 600]]

 [[  7   8   9]
  [ 10  11  12]]

 [[ 13  14  15]
  [ 16  17  18]]

 [[ 21  22  23]
  [ 24  25  26]]]

====> np.stack(a, axis=1)
(2, 6, 3)
[[[  1   2   3]
  [ 10  20  30]
  [100 200 300]
  [  7   8   9]
  [ 13  14  15]
  [ 21  22  23]]

 [[  4   5   6]
  [ 40  50  60]
  [400 500 600]
  [ 10  11  12]
  [ 16  17  18]
  [ 24  25  26]]]

====> np.stack(a, axis=2)
(2, 3, 6)
[[[  1  10 100   7  13  21]
  [  2  20 200   8  14  22]
  [  3  30 300   9  15  23]]

 [[  4  40 400  10  16  24]
  [  5  50 500  11  17  25]
  [  6  60 600  12  18  26]]]

Process finished with exit code 0

posted @ 2025-06-26 16:01  无左无右  阅读(33)  评论(0)    收藏  举报