numpy list 切片操作

import numpy as np
a = np.arange(1,26).reshape((5,5))

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]
# 行列下标从0 开始,切不包括后一个数。输出1、2 行的2到4 列
a[1:3, 2:5]
array([[ 8,  9, 10],
       [13, 14, 15]])
# 输出所有的行
a[:, 2:5]
array([[ 3,  4,  5],
       [ 8,  9, 10],
       [13, 14, 15],
       [18, 19, 20],
       [23, 24, 25]])
a[:, None]
array([[[ 1,  2,  3,  4,  5]],

       [[ 6,  7,  8,  9, 10]],

       [[11, 12, 13, 14, 15]],

       [[16, 17, 18, 19, 20]],

       [[21, 22, 23, 24, 25]]])
a[:, None].shape # None表示 增加一维
(5, 1, 5)
a[:,:, None].shape  # 在第三维增加一维
(5, 5, 1)
a[:,:, None, None].shape   # 在第三、四维增加一维
(5, 5, 1, 1)
a[..., None].shape  # 在现有维度基础上的最后增加一维。就是因为…代替了前面两个冒号
(5, 5, 1)
发布了21 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_37206602/article/details/89388546
今日推荐