numpy索引、切片与迭代

1.numpy中,在做数组运算和操作时,返回结果不是数组的副本就是视图。而赋值运算不会为数组和任何元素创建副本。

numpy.ndarray.copy() 函数创建一个副本。对副本修改,不会影响到原始数据。

>>> x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
>>> y = x
>>> y[0] = -1
>>> print(x)
[-1  2  3  4  5  6  7  8]  #没有创建副本,直接进行了赋值运算。对原数组进行了修改。
print(y)
[-1  2  3  4  5  6  7  8]

>>> x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
>>> y = x.copy()
>>> y[0] = -1
>>> print(x)
[1 2 3 4 5 6 7 8]  #创建了副本,只修改了副本,对原数组没有影响。
>>> print(y)
[-1  2  3  4  5  6  7  8]

3.索引(数组索引机制指的是用方括号([])加序号的形式引用单个数组元素,它的用处很多,比如抽取元素,选取数组的几个元素,甚至为其赋一个新值。)

(1)整数索引

>>> x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
>>> print(x[2])  # 3
3
>>> x = np.array([[11, 12, 13, 14, 15],
...               [16, 17, 18, 19, 20],
...               [21, 22, 23, 24, 25],
...               [26, 27, 28, 29, 30],
...               [31, 32, 33, 34, 35]])
>>> print(x[2])  # [21 22 23 24 25]
[21 22 23 24 25]
>>> print(x[2][1])  # 22
22
>>> print(x[2, 1])  # 22
22
import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x
y[::2, :3:2] = -1     #行:最小索引到最大索引步长为2,列:截止到4列步长为2,全部赋值为-1
print(x)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]
print(y)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]

(2)切片索引

对 python 列表list进行切片操作得到的数组是原数组的副本,而对 Numpy 数据进行切片操作得到的数组则是指向相同缓冲区的视图。切片形式 (start:stop:step )隔开的数字置于方括号内。

如省去第一个数字,numpy 会认为第一个数字是0;如省去第二个数字,numpy 则会认为第二个数字是数组的最大索引值;如省去最后一个数字,它将会被理解为1,也就是抽取所有元素而不再考虑间隔。二维数组,第一片定义了行的切片,第二片定义了列的切片。

一维数组切片:

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x[0:2])  # [1 2]
#利用负数下标翻转数组
print(x[::-1])  # [8 7 6 5 4 3 2 1]

二维数组切片:

import numpy as np
>>> x = np.array([[11, 12, 13, 14, 15],
...               [16, 17, 18, 19, 20],
...               [21, 22, 23, 24, 25],
...               [26, 27, 28, 29, 30],
...               [31, 32, 33, 34, 35]])
>>> print(x[::2, ::2])   #从最小索引到最大索引步长为2
[[11 13 15]
 [21 23 25]
 [31 33 35]]


#行反转
print(x[::-1, :])
# [[31 32 33 34 35]
#  [26 27 28 29 30]
#  [21 22 23 24 25]
#  [16 17 18 19 20]
#  [11 12 13 14 15]]

#列反转
print(x[:, ::-1])
# [[15 14 13 12 11]
#  [20 19 18 17 16]
#  [25 24 23 22 21]
#  [30 29 28 27 26]
#  [35 34 33 32 31]]

3.整数数组索引

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
r = [0, 1, 2]
print(x[r])
# [1 2 3]

r = [0, 1, -1]
print(x[r])
# [1 2 8]

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

r = [0, 1, 2]
print(x[r])
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]]

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
r = np.array([[0, 1], [3, 4]])   #行索引
print(x[r])
# [[1 2]
#  [4 5]]

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

r = np.array([[0, 1], [3, 4]])
print(x[r])
# [[[11 12 13 14 15]
#   [16 17 18 19 20]]
#
#  [[26 27 28 29 30]
#   [31 32 33 34 35]]]

# 获取了 5X5 数组中的四个角的元素。
# 行索引是 [0,0] 和 [4,4],而列索引是 [0,4] 和 [0,4]。
r = np.array([[0, 0], [4, 4]])
c = np.array([[0, 4], [0, 4]])
y = x[r, c]
print(y)
# [[11 15]
#  [31 35]]

借助切片:与整数数组组合。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

y = x[0:3, [1, 2, 2]]  #行:0·2,列:第一列,2列
print(y)
# [[12 13 13]
#  [17 18 18]
#  [22 23 23]]
  • numpy. take(a, indices, axis=None, out=None, mode='raise') Take elements from an array along an axis.
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

r = [0, 1, 2]
print(np.take(x, r, axis=0))  #axis=0,只对行操作
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]]

布尔索引:

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x > 5  #这里5不是索引,而是列表里面的值
print(y)
# [False False False False False  True  True  True]
print(x[x > 5])
# [6 7 8]
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x > 25
print(y)
# [[False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]
print(x[x > 25])
# [26 27 28 29 30 31 32 33 34 35]

数组迭代:

除了for循环,Numpy 还提供另外一种更为优雅的遍历方法。

  • apply_along_axis(func1d, axis, arr) Apply a function to 1-D slices along the given axis.
import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

y = np.apply_along_axis(np.sum, 0, x)   #axis=0,对行操作
print(y)  # [105 110 115 120 125]
y = np.apply_along_axis(np.sum, 1, x)   #axis=1,对列操作
print(y)  # [ 65  90 115 140 165]

y = np.apply_along_axis(np.mean, 0, x)
print(y)  # [21. 22. 23. 24. 25.]
y = np.apply_along_axis(np.mean, 1, x)
print(y)  # [13. 18. 23. 28. 33.]

def my_func(x):
    return (x[0] + x[-1]) * 0.5


y = np.apply_along_axis(my_func, 0, x)
print(y)  # [21. 22. 23. 24. 25.]
y = np.apply_along_axis(my_func, 1, x)
print(y)  # [13. 18. 23. 28. 33.]

参考

阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值