十月学习打卡-numpy-2
import numpy as np
import random
1.调整数组的形状
four = np.array([[1,2,3],[4,5,6]])
# 修改的是原有的
four.shape = (3,2)
print(four)
[[1 2]
[3 4]
[5 6]]
# 返回一个新的数组
four_other = np.array([[1,2,3],[4,5,6]])
four_other_new = four.reshape(3,2)
print(four_other)
print(four_other_new)
[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
# 将多维变成一维数组
five = four.reshape((6,),order='F')
# 默认情况下‘C’以行为主的顺序展开,‘F’(Fortran风格)意味着以列的顺序展开
six = four.flatten(order='C')
print(five)
print(six)
[1 3 5 2 4 6]
[1 2 3 4 5 6]
1.1 多维数组的形状
t = np.arange(24)
print(t)
print("shape:",t.shape)
# 转换成二维
t1 = t.reshape((4,6))
print(t1)
print("shape:",t1.shape)
# 转成三维
t2 = t.reshape((2,3,4))
print(t2)
print("shape:",t2.shape)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
shape: (24,)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
shape: (4, 6)
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
shape: (2, 3, 4)
tolist() 数组转list
a= np.array([9, 12, 88, 14, 25])
list_a = a.tolist()
print(list_a)
print(type(list_a))
[9, 12, 88, 14, 25]
<class 'list'>
1.2 NumPy 的数据类型
dtype 获取数据类型
astype 改变数据类型
f = np.array([1,2,3,4,5], dtype = np.int16)
# 返回数组中每个元素的字节单位长度
print(f.itemsize)
# 获取数据类型
print(f.dtype)
# 调整数据类型
f1 = f.astype(np.int64)
print(f1.dtype)
2
int16
int64
# 随机生成小数
# 使用python语法,保留两位
print(round(random.random(),2))
#随机生成十个数字
arr = np.array([random.random() for i in range(10)])
print(arr)
# 取小数点后两位
print(np.round(arr,2))
0.93
[0.57458443 0.35506316 0.23451839 0.66950003 0.47128081 0.55178002
0.48226117 0.33951354 0.855257 0.15476563]
[0.57 0.36 0.23 0.67 0.47 0.55 0.48 0.34 0.86 0.15]
2.数组的副本操作
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]
【例】数组切片操作返回的对象只是原数组的视图。
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
# ::2 代表每隔两行取一次
# :3:2 表示从第0列到第3列,但是不包括第三列,每隔两列取一次
y[::2, :3:2] = -555
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)
# [[-555 12 -555 14 15]
# [ 16 17 18 19 20]
# [-555 22 -555 24 25]
# [ 26 27 28 29 30]
# [-555 32 -555 34 35]]
【例】
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)
# [[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(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]]
3.索引与切片
一维数组的操作方法:冒号分隔切片参数 [start:stop:step] 来进行切片操作
a = np.arange(10)
print(a)
print(a[2:7:2])# 从索引 2 开始到索引 7 停止,间隔为 2
# 如果只放置一个参数,如 [2],将返回与该索引相对应的单个元素
print(a[2],a)
# 如果为 [2:],表示从该索引开始以后的所有项都将被提取
print(a[2:])
[0 1 2 3 4 5 6 7 8 9]
[2 4 6]
2 [0 1 2 3 4 5 6 7 8 9]
[2 3 4 5 6 7 8 9]
3.1多维数组的操作方法
t1 = np.arange(24).reshape(4,6)
print(t1)
print('*'*20)
print(t1[1]) # 取一行(一行代表是一条数据,索引也是从0开始的)
print('*'*20)
print(t1[1,:]) # 取一行
print('*'*20)
print(t1[1:])# 取连续的多行
print('*'*20)
print(t1[1:3,:])# 取连续的多行
print('*'*20)
print(t1[[0,2,3]])# 取不连续的多行
print('*'*20)
print(t1[[0,2,3],:])# 取不连续的多行
print('*'*20)
print(t1[:,1])# 取一列
print('*'*20)
print(t1[:,1:])# 连续的多列
print('*'*20)
print(t1[:,[0,2,3]])# 取不连续的多列
print('*'*20)
print(t1[2,3])# # 取某一个值,三行四列
print('*'*20)
print(t1[[0,1,1],[0,1,3]])# 取多个不连续的值,[[行,行。。。],[列,列。。。]]
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
********************
[ 6 7 8 9 10 11]
********************
[ 6 7 8 9 10 11]
********************
[[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
********************
[[ 6 7 8 9 10 11]
[12 13 14 15 16 17]]
********************
[[ 0 1 2 3 4 5]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
********************
[[ 0 1 2 3 4 5]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
********************
[ 1 7 13 19]
********************
[[ 1 2 3 4 5]
[ 7 8 9 10 11]
[13 14 15 16 17]
[19 20 21 22 23]]
********************
[[ 0 2 3]
[ 6 8 9]
[12 14 15]
[18 20 21]]
********************
15
********************
[0 7 9]
3.2数组中的数值修改
t = np.arange(24).reshape(4,6)
# 修改某一行的值
t[1,:]=0
# 修改某一列的值
t[:,1]=0
# 修改连续多行
t[1:3,:]=0
# 修改连续多列
t[:,1:4]=0
# 修改多行多列,取第二行到第四行,第三列到第五列
t[1:4,2:5]=0
# 修改多个不相邻的点
t[[0,1],[0,3]]=0
# 可以根据条件修改,比如讲小于10的值改掉
t[t<10]=0
t
array([[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[18, 0, 0, 0, 0, 23]])
# 使用逻辑判断
# np.logical_and &
# np.logical_or |
# np.logical_not ~
t[(t>2)&(t<6)]=1# 与
t[(t<2)|(t>6)]=0# 或
t[~(t>6)]=0# 非
print(t)
# 拓展
# 三目运算( np.where(condition, x, y)满足条件(condition),输出x,不满足输出y。))
score = np.array([[80,88],[82,81],[75,81]])
result = np.where(score>80,True,False)
print(result)
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[False True]
[ True True]
[False True]]
3.3 dots 索引
NumPy 允许使用…表示足够多的冒号来构建完整的索引列表。
比如,如果 x 是 5 维数组:
x[1,2,…] 等于 x[1,2,:,:,:]
x[…,3] 等于 x[:,:,:,:,3]
x[4,…,5,:] 等于 x[4,:,:,5,:]
[例]
x = np.random.randint(1, 100, [2, 2, 3])
print(x)
# [[[69 65 40]
# [51 78 71]]
# [[52 48 63]
# [28 84 64]]]
print(x[1, ...])
# [[52 48 63]
# [28 84 64]]
print(x[..., 2])
# [[40 71]
# [63 64]]
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]])
#第0行第1列
#第1行第2列
#第2行第2列
y = x[0:3, [1, 2, 2]]
print(y)
[[12 13 13]
[17 18 18]
[22 23 23]]
应注意:使用切片索引到numpy数组时,生成的数组视图将始终是原始数组的子数组, 但是整数数组索引,不是其子数组,是形成新的数组。
#切片索引
a=np.array([[1,2],[3,4],[5,6]])
b=a[0:1,0:1]
b[0,0]=2
print(a[0,0]==b)
#[[True]]
[[ True]]
# 整数数组索引
a=np.array([[1,2],[3,4],[5,6]])
b=a[0,0]
b=2
print(a[0,0]==b)
#False
False
3.4布尔索引
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x > 5
print(y)
# [False False False False False True True True]
print(x[x > 5])
# [6 7 8]
x = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])
y = np.logical_not(np.isnan(x))
print(x)
print(x[y])
[nan 1. 2. nan 3. 4. 5.]
[1. 2. 3. 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]])
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]]
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)
print(len(x)) # 50
plt.plot(x, y)
50
mask = y >= 0
print(len(x[mask])) # 25
print(mask)
plt.plot(x[mask], y[mask], 'bo')
25
[ True True True True True True True True True True True True
True True True True True True True True True True True True
True False False False False False False False False False False False
False False False False False False False False False False False False
False False]
mask = np.logical_and(y >= 0, x <= np.pi / 2)
print(mask)
plt.plot(x[mask], y[mask], 'go')
plt.show()
[ True True True True True True True True True True True True
True False False False False False False False False False False False
False False False False False False False False False False False False
False False False False False False False False False False False False
False False]
4.数组迭代
apply_along_axis(func, axis, arr) Apply a function to 1-D slices along the given axis.
函数func(arr)中的arr是一个数组,函数的主要功能就是对数组里的每一个元素进行变换,得到目标的结果。
其中axis表示函数func对数组arr作用的轴
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) # [105 110 115 120 125]
y = np.apply_along_axis(np.sum, 1, x)
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.]
[105 110 115 120 125]
[ 65 90 115 140 165]
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]