numpy expand_dims,Python
  TEZNKK3IfmPf 2023年11月14日 51 0
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]]]
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   39   0   0 python开发语言
  TEZNKK3IfmPf   2024年05月31日   28   0   0 python
  TEZNKK3IfmPf   2024年05月31日   31   0   0 python
TEZNKK3IfmPf