numpy基础 Task02

副本与视图

在 Numpy 中,尤其是在做数组运算或数组操作时,返回结果不是数组的 副本 就是 视图。

在 Numpy 中,所有赋值运算不会为数组和数组中的任何元素创建副本。

  • numpy.ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。
import numpy as np

x = np.array([1,2,3,4,5,6,7,8])
y = x
y[0] = -1
print(x)
print('+++++++++++++++')
print(y)

[-1 2 3 4 5 6 7 8]
+++++++++++++++
[-1 2 3 4 5 6 7 8]

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
print(x)
print('+++++++++++++++')
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]]
+++++++++++++++
[[-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]]

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.copy()
y[::2,:3:2] = -1
print(x)
print('+++++++++++++++')
print(y)

[[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]]
+++++++++++++++
[[-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]]

索引与切片

整数索引
  • 要获得数组的单个元素,指定元素的索引即可
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])
print(x[2][1])
print(x[2,1])

[21 22 23 24 25]
22
22

切片索引
  • 切片操作是指抽取数组的一部分元素生成新数组。对 python 列表进行切片操作得到的数组是原数组的副本,而对 Numpy 数据进行切片操作得到的数组则是指向相同缓冲区的视图。
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[0:2,1:])

[[12 13 14 15]
[17 18 19 20]]

dots索引

NumPy 允许使用…表示足够多的冒号来构建完整的索引列表。

比如,如果 x 是 5 维数组:

  • x[1,2,…] 等于 x[1,2,:,:,:]
  • x[…,3] 等于 x[:,:,:,:,3]
  • x[4,…,5,:] 等于 x[4,:,:,5,:]
整数数组索引
  • 方括号内传入多个索引值,可以同时选择多个元素
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]])

r = [0, 1, 2]
c = [2, 3, 4]
y = x[r, c]
print(y)

[13 19 25]

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]])

# 获取了 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 > 25
print(y)
print('+++++++++++++')
print(x[x > 25])

[[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]]
+++++++++++++
[26 27 28 29 30 31 32 33 34 35]

数组迭代

  • apply_along_axis(func1d, axis, arr)
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)#按列相加
print(y)
print('+++++++++++++++++++++')
y = np.apply_along_axis(np.sum, 1, x)#按行相加
print(y)

[105 110 115 120 125]
+++++++++++++++++++++
[ 65 90 115 140 165]

练习

交换数组arr中的列1和列2。
arr = np.arange(9).reshape(3, 3)
print(arr)
print('++++++++++++++')
x = arr[:,[1,0,2]]
print(x)

[[0 1 2]
[3 4 5]
[6 7 8]]
++++++++++++++
[[1 0 2]
[4 3 5]
[7 6 8]]

交换数组arr中的第1行和第2行。
arr = np.arange(9).reshape(3, 3)
print(arr)
print('++++++++++++++')
x = arr[[1,0,2],:]
print(x)

[[0 1 2]
[3 4 5]
[6 7 8]]
++++++++++++++
[[3 4 5]
[0 1 2]
[6 7 8]]

反转二维数组arr的行。
arr = np.arange(9).reshape(3, 3)
print(arr)
print('++++++++++++++')
x = arr[::-1,:]
print(x)

[[0 1 2]
[3 4 5]
[6 7 8]]
++++++++++++++
[[6 7 8]
[3 4 5]
[0 1 2]]

反转二维数组arr的列。
arr = np.arange(9).reshape(3, 3)
print(arr)
print('++++++++++++++')
x = arr[:,::-1]
print(x)

[[0 1 2]
[3 4 5]
[6 7 8]]
++++++++++++++
[[2 1 0]
[5 4 3]
[8 7 6]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigCabbageFy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值