副本与视图
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)
# [-1 2 3 4 5 6 7 8]
#???????x为啥变了呢
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]
数组切片操作返回的对象只是原数组的视图
NumPy 允许使用…表示足够多的冒号来构建完整的索引列表。
比如,如果 x 是 5 维数组:
x[1,2,…] 等于 x[1,2,:,:,:]
x[…,3] 等于 x[:,:,:,:,3]
x[4,…,5,:] 等于 x[4,:,:,5,:]
np.apply_along_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) #对每列求和
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.]
练习
1,交换两行
import numpy as np
arr = np.arange(9).reshape(3, 3)
print(arr)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
x = arr[:, [2, 1, 0]] #中间用方括号
print(x)
# [[2 1 0]
# [5 4 3]
# [8 7 6]]
2,反转二维数组的行
import numpy as np
arr = np.arange(9).reshape(3, 3)
print(arr)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
x = arr[::-1, :] #反转记得:切片中::-1
print(x)
# [[6 7 8]
# [3 4 5]
# [0 1 2]]
数组操作
通过数组名.shape 来改变数组的形状
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
x.shape = [2, 4] #用x.shape来更改
print(x)
# [[1 2 9 4]
# [5 6 7 8]]
numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个
元素。
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.flat
print(y)
# <numpy.flatiter object at 0x0000020F9BA10C60>
for i in y:
print(i, end=' ')
# 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
ravel()返回的是视图
y = np.ravel(x)
#更改y,不会变化x
numpy.reshape(a, newshape[, order=‘C’])在不更改数据的情况下为数组赋予新的形状。
reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
reshape()函数当参数newshape = -1时,表示将数组降为一维。
x = np.arange(12)
y = np.reshape(x, [3, 4])
print(y.dtype) # int32
print(y)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
y = np.reshape(x, [3, -1])
print(y)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
y = np.reshape(x, -1)
print(y)
# [ 0 1 2 3 4 5 6 7 8 9 10 11]
数组转置:y = x.T 或者 y = np.transpose(x)
使用newaxis参数来增加一个维度。
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
print(x) # [1 2 9 4 5 6 7 8]
y = x[np.newaxis, :]
print(y.shape) # (1, 8)
print(y) # [[1 2 9 4 5 6 7 8]]
y = x[:, np.newaxis]
print(y.shape) # (8, 1)
print(y)
# [[1]
# [2]
# [9]
# [4]
# [5]
# [6]
# [7]
# [8]]
与np.newaxis相对的是np.squeeze
numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
import numpy as np
x = np.arange(10)
print(x.shape) # (10,)
x = x[np.newaxis, :]
print(x.shape) # (1, 10)
y = np.squeeze(x)
print(y.shape) # (10,)
**np.concatenate([x, y])**数组拼接
原来x,y是几维的,拼接后就是几维的
#一维度
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.concatenate([x, y])
print(z)
# [1 2 3 7 8 9]
z = np.concatenate([x, y], axis=0)
print(z)
# [1 2 3 7 8 9]
#二维度
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.concatenate([x, y])
print(z)
# [[ 1 2 3]
# [ 7 8 9]]
z = np.concatenate([x, y], axis=0) #垂直叠加
print(z)
# [[ 1 2 3]
# [ 7 8 9]]
z = np.concatenate([x, y], axis=1) #平行相叠
print(z)
# [[ 1 2 3 7 8 9]]
stack沿着新的轴加入一系列数组
hstack(),vstack()分别表示水平和竖直的拼接方式
np.concatenate([a, b], axis=0)等同于np.vstack([a, b])
np.concatenate([a, b], axis=1)等同于np.hstack([a, b])
numpy.split数组拆分
练习
1,访问二维数组的全部元素,并按列输出数组名.flatten(order=‘F’)
2,验证两数组是否相等np.allclose(A,B) 或者np.array_equal(A,B) ,返回布尔类型