numpy expand_dims,Python

import numpy as np

if __name__ == "__main__":
    LEN = 6

    print('-----')
    print('a')
    a = [0, 1, 2, 3, 4, 5]
    a = np.array(a).reshape(2, 3)
    print(a)
    print(a.shape)
    print(a.ndim)
    print(a[0])

    print('-----')
    print('b0')
    b0 = np.expand_dims(a, axis=0)
    print(b0)
    print(b0.shape)
    print(b0.ndim)
    print(b0[0])

    print('-----')
    print('b1')
    b1 = np.expand_dims(a, axis=1)
    print(b1)
    print(b1.shape)
    print(b1.ndim)
    print(b1[0])

    print('-----')
    c = [[0], [1], [2]]
    c0 = np.expand_dims(c, axis=0)
    print('c0')
    print(c0.shape)
    print(c0)
    c1 = np.expand_dims(c, axis=1)
    print('c1')
    print(c1.shape)
    print(c1)

输出:

-----
a
[[0 1 2]
 [3 4 5]]
(2, 3)
2
[0 1 2]
-----
b0
[[[0 1 2]
  [3 4 5]]]
(1, 2, 3)
3
[[0 1 2]
 [3 4 5]]
-----
b1
[[[0 1 2]]

 [[3 4 5]]]
(2, 1, 3)
3
[[0 1 2]]
-----
c0
(1, 3, 1)
[[[0]
  [1]
  [2]]]
c1
(3, 1, 1)
[[[0]]

 [[1]]

 [[2]]]
发布了1029 篇原创文章 · 获赞 987 · 访问量 336万+

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/103203553