DM03-NumPy的索引总结

这里写图片描述

摘要:NumPy的索引,分解为基础索引,多维度,增加维度,省略号,布尔等几个方面去总结,方便数据的获取。

基础索引[i:j:k模型]

import numpy as np
# 基础索引[i:j:k模型]
x = np.arange(15)
print('[1]:seq:\n', x)
[1]:seq:
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
 # i:j:k模型【where i is the starting index, j is the stopping index, and k is the step】
print('x[5:10:2]:\n', x[5:10:2])
x[5:10:2]:
 [5 7 9]
# i默认为0,j默认为n,k默认为1
print('x[5:10]:\n', x[5:10])
print('x[:10]:\n', x[:10])
print('x[:10:2]:\n', x[:10:2])
print('x[5::2]:\n', x[5::2])
print('x[::2]:\n', x[::2])
x[5:10]:
 [5 6 7 8 9]
x[:10]:
 [0 1 2 3 4 5 6 7 8 9]
x[:10:2]:
 [0 2 4 6 8]
x[5::2]:
 [ 5  7  9 11 13]
x[::2]:
 [ 0  2  4  6  8 10 12 14]
# An integer, i, returns the same values as i:i+1
print('x[5]:\n', x[5])
x[5]:
 5
# 负i/j/k模型[n+i进行处理,可以接受前面的变换]
print('x[10:5:-1]:\n', x[10:5:-1])
print('x[-10:-5:-1]:\n', x[-5:-10:-1])
x[10:5:-1]:
 [10  9  8  7  6]
x[-10:-5:-1]:
 [10  9  8  7  6]

增加维度

# 增加一个维度
print('x[:, np.newaxis]\n', x[:, np.newaxis])
print('x[np.newaxis,:]:\n', x[np.newaxis, :])
x[:, np.newaxis]
 [[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]
 [11]
 [12]
 [13]
 [14]]
x[np.newaxis,:]:
 [[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]]
x = np.arange(15)
print('seq:\n', x)
x.shape = (3, 5)
print('3 * 5:\n', x)

seq:
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
3 * 5:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
y2 = x[:, np.newaxis, :]
print('y2:', y2)
print('y2.shap:', y2.shape)
print('y2[...,2]:\n', y2[..., 2])
print('y2[:,:,2]:\n', y2[:, :, 2])
print('y2[...,2]:\n', y2[1, ..., 2])
y2: [[[ 0  1  2  3  4]]

 [[ 5  6  7  8  9]]

 [[10 11 12 13 14]]]
y2.shap: (3, 1, 5)
y2[...,2]:
 [[ 2]
 [ 7]
 [12]]
y2[:,:,2]:
 [[ 2]
 [ 7]
 [12]]
y2[...,2]:
 [7]

多维数组的索引

# 多维数组的索引【花式索引】
print('x[1,2]:\n', x[1, 2])
print('x[1][2]:\n', x[1][2])
print('x[[1,2],::2]:\n', x[[1, 2], ::2])
print('x[,[1,2]]:\n', x[:, [1, 2]])
print('x[1:2]:\n', x[1:2])
print('x[[0,2],1]:\n', x[[0, 2], 1])
print('x[[0,2],0:2]:\n', x[[0, 2], 0:2])
# 这个注意一下,行与列的下标维度要一致,这个是两两组合成一个坐标来选择元素的。
print('x[[1,2,1],[1,3,2]]:\n', x[[1, 2, 1], [1, 3, 2]])
# 这种写法是会出现问题的 index 3 is out of bounds for axis 0 with size 3
# print('x[[1,2,1]][[1,3,2]]:\n', x[[1, 2, 1]][[1, 3, 2]])
x[1,2]:
 7
x[1][2]:
 7
x[[1,2],::2]:
 [[ 5  7  9]
 [10 12 14]]
x[,[1,2]]:
 [[ 1  2]
 [ 6  7]
 [11 12]]
x[1:2]:
 [[5 6 7 8 9]]
x[[0,2],1]:
 [ 1 11]
x[[0,2],0:2]:
 [[ 0  1]
 [10 11]]
x[[1,2,1],[1,3,2]]:
 [ 6 13  7]

布尔类型的索引

 # 布尔类型的索引
b = x > 3
print('b:',b)
print('x[b]:',x[b])
print('x[x > 3]:', x[x > 3])
b: [[False False False False  True]
 [ True  True  True  True  True]
 [ True  True  True  True  True]]
x[b]: [ 4  5  6  7  8  9 10 11 12 13 14]
x[x > 3]: [ 4  5  6  7  8  9 10 11 12 13 14]

省略号

# 省略号[...只可以用一次,这个很合适懒人]
y1 = x[:, :, np.newaxis]
print('y1:', y1)
print('y1.shap:', y1.shape)
print('y1[...,0]:\n', y1[..., 0])
print('y1[:,0]:\n', y1[:, 0])
y1: [[[ 0]
  [ 1]
  [ 2]
  [ 3]
  [ 4]]

 [[ 5]
  [ 6]
  [ 7]
  [ 8]
  [ 9]]

 [[10]
  [11]
  [12]
  [13]
  [14]]]
y1.shap: (3, 5, 1)
y1[...,0]:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
y1[:,0]:
 [[ 0]
 [ 5]
 [10]]

域访问

#域访问
x = np.zeros((2, 2), dtype=[('a', np.int32), ('b', np.float64, (3, 3))])
print('x:\n',x)
print('x[\'a\'].shape:',x['a'].shape)
print('x[\'b\'].shape:', x['b'].shape)
x:
 [[(0, [[ 0.,  0.,  0.], [ 0.,  0.,  0.], [ 0.,  0.,  0.]])
  (0, [[ 0.,  0.,  0.], [ 0.,  0.,  0.], [ 0.,  0.,  0.]])]
 [(0, [[ 0.,  0.,  0.], [ 0.,  0.,  0.], [ 0.,  0.,  0.]])
  (0, [[ 0.,  0.,  0.], [ 0.,  0.,  0.], [ 0.,  0.,  0.]])]]
x['a'].shape: (2, 2)
x['b'].shape: (2, 2, 3, 3)

完整代码

    import numpy as np
    # 基础索引[i:j:k模型]
    x = np.arange(15)
    print('seq:\n', x)
    # i:j:k模型【where i is the starting index, j is the stopping index, and k is the step】
    print('x[5:10:2]:\n', x[5:10:2])
    # i默认为0,j默认为n,k默认为1
    print('x[5:10]:\n', x[5:10])
    print('x[:10]:\n', x[:10])
    print('x[:10:2]:\n', x[:10:2])
    print('x[5::2]:\n', x[5::2])
    print('x[::2]:\n', x[::2])
    # An integer, i, returns the same values as i:i+1
    print('x[5]:\n', x[5])
    # 负i/j/k模型[n+i进行处理,可以接受前面的变换]
    print('x[10:5:-1]:\n', x[10:5:-1])
    print('x[-10:-5:-1]:\n', x[-5:-10:-1])

    # 增加一个维度
    print('x[:, np.newaxis]\n', x[:, np.newaxis])
    print('x[np.newaxis,:]:\n', x[np.newaxis, :])

    x = np.arange(15)
    print('seq:\n', x)
    x.shape = (3, 5)
    print('3 * 5:\n', x)

    # 省略号[...只可以用一次,这个很合适懒人]
    y1 = x[:, :, np.newaxis]
    print('y1:', y1)
    print('y1.shap:', y1.shape)
    print('y1[...,0]:\n', y1[..., 0])
    print('y1[:,0]:\n', y1[:, 0])

    y2 = x[:, np.newaxis, :]
    print('y2:', y2)
    print('y2.shap:', y2.shape)
    print('y2[...,2]:\n', y2[..., 2])
    print('y2[:,:,2]:\n', y2[:, :, 2])
    print('y2[...,2]:\n', y2[1, ..., 2])

    # 多维数组的索引【花式索引】
    print('x[1,2]:\n', x[1, 2])
    print('x[1][2]:\n', x[1][2])
    print('x[[1,2],::2]:\n', x[[1, 2], ::2])
    print('x[,[1,2]]:\n', x[:, [1, 2]])
    print('x[1:2]:\n', x[1:2])
    print('x[[0,2],1]:\n', x[[0, 2], 1])
    print('x[[0,2],0:2]:\n', x[[0, 2], 0:2])
    print('x[[1,2,1],[1,3,2]]:\n', x[[1, 2, 1], [1, 3, 2]])
    # 这种写法是会出现问题的 index 3 is out of bounds for axis 0 with size 3
    # print('x[[1,2,1]][[1,3,2]]:\n', x[[1, 2, 1]][[1, 3, 2]])
    # 布尔类型的索引
    b = x > 3
    print('b:',b)
    print('x[b]:',x[b])
    print('x[x > 3]:', x[x > 3])

    #域访问
    x = np.zeros((2, 2), dtype=[('a', np.int32), ('b', np.float64, (3, 3))])
    print('x:\n',x)
    print('x[\'a\'].shape:',x['a'].shape)
    print('x[\'b\'].shape:', x['b'].shape)

参考:
https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing

作者:happyprince, http://blog.csdn.net/ld326/article/details/78959614

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值