1.索引,切片和迭代
1.副本与视图
在Numpy中,尤其是在做数组运算或数组操作中,返回结果不是数组的副本就是视图。
在 Numpy 中,所有赋值运算不会为数组和数组中的任何元素创建副本。
numpy.ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。
2.索引与切片
数组索引机制指的是用方括号([])加序号的形式引用单个数组元素,它的用处很多,比如抽取元素,选取数组的几个元素,甚至为其赋一个新值。
整数索引:要获取数组的单个元素,指定元素的索引即可。
切片索引:切片操作是指抽取数组的一部分元素生成新数组。对 python 列表进行切片操作得到的数组是原数组的副本,而对 Numpy 数据进行切片操作得到的数组则是指向相同缓冲区的视图。
dots索引:NumPy 允许使用...表示足够多的冒号来构建完整的索引列表。
比如,如果 x 是 5 维数组:
x[1,2,...] 等于 x[1,2,:,:,:]
x[...,3] 等于 x[:,:,:,:,3]
x[4,...,5,:] 等于 x[4,:,:,5,:]
整数数组索引:方括号内传入多个索引值,可以同时选择多个元素。
应注意:使用切片索引到numpy数组时,生成的数组视图将始终是原始数组的子数组, 但是整数数组索引,不是其子数组,是形成新的数组。
布尔索引:我们可以通过一个布尔数组来索引目标数组。
数组迭代:除了for循环,Numpy 还提供另外一种更为优雅的遍历方法。
代码:
python
import numpy as np
# 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
y[::2, :3: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]]
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]]
# 整数索引
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x[2]) # 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]
print(x[2][1]) # 22
print(x[2, 1]) # 22
# 切片索引
# 对一维数组切片
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x[0:2]) # [1 2]
#用下标0~5,以2为步长选取数组
print(x[1:5:2]) # [2 4]
print(x[2:]) # [3 4 5 6 7 8]
print(x[:2]) # [1 2]
print(x[-2:]) # [7 8]
print(x[:-2]) # [1 2 3 4 5 6]
print(x[:]) # [1 2 3 4 5 6 7 8]
#利用负数下标翻转数组
print(x[::-1]) # [8 7 6 5 4 3 2 1]
# 对二维数组切片 # 逗号左边切行,逗号右边切列
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])
# [[11 12 13 14 15]
# [16 17 18 19 20]]
print(x[1:5:2])
# [[16 17 18 19 20]
# [26 27 28 29 30]]
print(x[2:])
# [[21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]
print(x[:2])
# [[11 12 13 14 15]
# [16 17 18 19 20]]
print(x[-2:])
# [[26 27 28 29 30]
# [31 32 33 34 35]]
print(x[:-2])
# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]]
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(x[2, :]) # [21 22 23 24 25]
print(x[:, 2]) # [13 18 23 28 33]
print(x[0, 1:4]) # [12 13 14]
print(x[1:4, 0]) # [16 21 26]
print(x[1:3, 2:4])
# [[18 19]
# [23 24]]
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(x[::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]]
# dots索引
x = np.random.randint(1, 100, [2, 2, 3])
print(x)
# [[[ 5 64 75]
# [57 27 31]]
#
# [[68 85 3]
# [93 26 25]]]
print(x[1, ...])
# [[68 85 3]
# [93 26 25]]
print(x[..., 2])
# [[75 31]
# [ 3 25]]
# 整数数组索引
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]]
r = [0, 1, -1]
print(x[r])
# [[11 12 13 14 15]
# [16 17 18 19 20]
# [31 32 33 34 35]]
r = [0, 1, 2]
c = [2, 3, 4]
y = x[r, c]
print(y)
# [13 19 25]
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]]
# 可以借助切片:与整数数组组合。
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]]
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]]
a=np.array([[1,2],[3,4],[5,6]])
b=a[0,0]
b=2
print(a[0,0]==b)
#False
# 布尔索引
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[y])
# [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]]
print(x[x > 25])
# [26 27 28 29 30 31 32 33 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 = 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.]
练习代码及答案:
python
import numpy as np
# 交换数组arr中的列1和列3。
arr = np.arange(9).reshape(3,3)
x = arr[:, [2,1,0]]
print(x)
# [[2 1 0]
# [5 4 3]
# [8 7 6]]
# 交换数组arr中的第1行和第2行。
arr = np.arange(9).reshape(3,3)
x = arr[[1,0,2] ,:]
print(x)
# [[3 4 5]
# [0 1 2]
# [6 7 8]]
# 反转二维数组arr的行。
arr = np.arange(9).reshape(3,3)
x = arr[::-1,:]
print(x)
# [[6 7 8]
# [3 4 5]
# [0 1 2]]
# 反转二维数组arr的列。
arr = np.arange(9).reshape(3,3)
x = arr[:,::-1]
print(x)
#[[2 1 0]
# [5 4 3]
# [8 7 6]]
2.数组操作
更改形状:在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。通过修改 shape 属性来改变数组的形状。
numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。通过修改 shape 属性来改变数组的形状。
numpy.ndarray.flatten([order='C']) 将数组的副本转换为一维数组,并返回。order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。
数组的转置:
numpy.transpose(a, axes=None)置换数组的维度。
numpy.ndarray.T相同,但如果 self.ndim < 2,则返回 self,相当于 self.transpose()
更改维度:
当创建一个数组之后,还可以给它增加一个维度,这在矩阵计算中经常会用到。
python
numpy.newaxis = None
numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
a表示输入的数组;
axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错
数组组合:
拼接操作:numpy.concatenate((a1, a2, ...), axis=0, out=None)
numpy.stack()在新的维中拼接
numpy.vstack(tup) 将数组按顺序垂直堆叠(按行)。
numpy.hstack(tup) 将数组按顺序水平堆叠(按列)。
hstack(),vstack()分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于concatenate,用于在已有轴上进行操作。
数组拆分:
numpy.split(ary, indices_or_sections, axis=0) 将数组分割成多个子数组,这些子数组是对原数组 ary 的视图。
numpy.vsplit()垂直拆分
numpy.hsplit()水平拆分
数组平铺:
numpy.tile(A, reps)
numpy.repeat(a, repeats, axis=None)
axis=0,沿着y轴复制,实际上增加了行数。
axis=1,沿着x轴复制,实际上增加了列数。
repeats,可以为一个数,也可以为一个矩阵。
axis=None时就会flatten当前矩阵,实际上就是变成了一个行向量
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) 找出数组中的唯一元素。
return_index:返回输入数组中产生唯一值的索引
return_inverse:返回可以重建输入数组的唯一数组的索引
return_counts:返回每个唯一值在输入数组中出现的次数
python
import numpy as np
# 通过修改 shape 属性来改变数组的形状。
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
x.shape = [2, 4]
print(x)
# [[1 2 9 4]
# [5 6 7 8]]
# numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素
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
y[3] = 0
print(end='\n')
print(x)
# [[11 12 13 0 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]
# flatten()函数返回的是拷贝。order = ('C':按行拷贝,'F':按列拷贝,'A':原格式拷贝,'K':内存大小拷贝)
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.flatten()
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]
y[3] = 0
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]]
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.flatten(order='F')
print(y)
# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
# 35]
y[3] = 0
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]]
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.ravel(x)
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]
y[3] = 0
print(x)
# [[11 12 13 0 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]
# ravel()返回的是视图
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.ravel(x)
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]
y[3] = 0
print(x)
# [[11 12 13 0 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]
# order=F 就是拷贝
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.ravel(x, order='F')
print(y)
# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
# 35]
y[3] = 0
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]]
# reshape()函数当参数newshape = -1时,表示将数组降为一维。
x = np.random.randint(12, size=[2, 2, 3])
print(x)
# [[[11 9 1]
# [ 1 10 3]]
#
# [[ 0 6 1]
# [ 4 11 3]]]
y = np.reshape(x, -1)
print(y)
# [11 9 1 1 10 3 0 6 1 4 11 3]
# numpy.transpose 和 numpy.ndarray.T
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[6.74 8.46 6.74 5.45 1.25]
# [3.54 3.49 8.62 1.94 9.92]
# [5.03 7.22 1.6 8.7 0.43]
# [7.5 7.31 5.69 9.67 7.65]
# [1.8 9.52 2.78 5.87 4.14]]
y = x.T
print(y)
# [[6.74 3.54 5.03 7.5 1.8 ]
# [8.46 3.49 7.22 7.31 9.52]
# [6.74 8.62 1.6 5.69 2.78]
# [5.45 1.94 8.7 9.67 5.87]
# [1.25 9.92 0.43 7.65 4.14]]
y = np.transpose(x)
print(y)
# [[6.74 3.54 5.03 7.5 1.8 ]
# [8.46 3.49 7.22 7.31 9.52]
# [6.74 8.62 1.6 5.69 2.78]
# [5.45 1.94 8.7 9.67 5.87]
# [1.25 9.92 0.43 7.65 4.14]]
# numpy.newaxis = None 更改维度
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]]
print("--------------------------")
# numpy.squeeze 删除单维度条目
# 将表示向量的数组转化为秩为1的数组
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,)
# 指定维度的操作
x = np.array([[[0], [1], [2]]])
print(x.shape) # (1, 3, 1)
print(x)
# [[[0]
# [1]
# [2]]]
y = np.squeeze(x)
print(y.shape) # (3,)
print(y) # [0 1 2]
y = np.squeeze(x, axis=0)
print(y.shape) # (3, 1)
print(y)
# [[0]
# [1]
# [2]]
y = np.squeeze(x, axis=2)
print(y.shape) # (1, 3)
print(y) # [[0 1 2]]
# y = np.squeeze(x, axis=1)
# ValueError: cannot select an axis to squeeze out which has size not equal to one
# 数组的拼接操作
# 沿着现有轴的数组排列
# 原数组一维
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]]
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.concatenate([x, y])
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1 2 3 7 8 9]
# [ 4 5 6 10 11 12]]
# numpy.stack()沿着新的轴加入一系列数组 stack为增加维度的拼接
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.stack([x, y], axis=1)
print(z.shape) # (3, 2)
print(z)
# [[1 7]
# [2 8]
# [3 9]]
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.stack([x, y])
print(z.shape) # (2, 1, 3)
print(z)
# [[[1 2 3]]
#
# [[7 8 9]]]
z = np.stack([x, y], axis=1)
print(z.shape) # (1, 2, 3)
print(z)
# [[[1 2 3]
# [7 8 9]]]
z = np.stack([x, y], axis=2)
print(z.shape) # (1, 3, 2)
print(z)
# [[[1 7]
# [2 8]
# [3 9]]]
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.stack([x, y])
print(z.shape) # (2, 2, 3)
print(z)
# [[[ 1 2 3]
# [ 4 5 6]]
#
# [[ 7 8 9]
# [10 11 12]]]
z = np.stack([x, y], axis=1)
print(z.shape) # (2, 2, 3)
print(z)
# [[[ 1 2 3]
# [ 7 8 9]]
#
# [[ 4 5 6]
# [10 11 12]]]
z = np.stack([x, y], axis=2)
print(z.shape) # (2, 3, 2)
print(z)
# [[[ 1 7]
# [ 2 8]
# [ 3 9]]
#
# [[ 4 10]
# [ 5 11]
# [ 6 12]]]
# numpy.vstack(tup) 将数组按顺序垂直堆叠(按行)。
# numpy.hstack(tup) 将数组按顺序水平堆叠(按列)。
# 一维的情况
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.vstack((x, y))
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.hstack((x, y))
print(z.shape) # (6,)
print(z)
# [1 2 3 7 8 9]
z = np.concatenate((x, y))
print(z.shape) # (6,)
print(z) # [1 2 3 7 8 9]
# 二维的情况
#1
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.vstack((x, y))
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.concatenate((x, y), axis=0)
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.hstack((x, y))
print(z.shape) # (1, 6)
print(z)
# [[ 1 2 3 7 8 9]]
z = np.concatenate((x, y), axis=1)
print(z.shape) # (1, 6)
print(z)
# [[1 2 3 7 8 9]]
#2
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.vstack((x, y))
print(z.shape) # (4, 3)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate((x, y), axis=0)
print(z.shape) # (4, 3)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.hstack((x, y))
print(z.shape) # (2, 6)
print(z)
# [[ 1 2 3 7 8 9]
# [ 4 5 6 10 11 12]]
# numpy.split,数组拆分
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.split(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
# numpy.vsplit 垂直拆分
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.vsplit(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
y = np.split(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
y = np.vsplit(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]])]
y = np.split(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]])]
y = np.vsplit(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=0)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
# numpy.hsplit() 水平拆分
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)
# [array([[11, 12],
# [16, 17],
# [21, 22]]), array([[13, 14],
# [18, 19],
# [23, 24]])]
y = np.split(x, 2, axis=1)
print(y)
# [array([[11, 12],
# [16, 17],
# [21, 22]]), array([[13, 14],
# [18, 19],
# [23, 24]])]
y = np.hsplit(x, [3])
print(y)
# [array([[11, 12, 13],
# [16, 17, 18],
# [21, 22, 23]]), array([[14],
# [19],
# [24]])]
y = np.split(x, [3], axis=1)
print(y)
# [array([[11, 12, 13],
# [16, 17, 18],
# [21, 22, 23]]), array([[14],
# [19],
# [24]])]
y = np.hsplit(x, [1, 3])
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
# 数组平铺
#numpy.tile 将原矩阵横向、纵向地复制
x = np.array([[1, 2], [3, 4]])
print(x)
# [[1 2]
# [3 4]]
y = np.tile(x, (1, 3))
print(y)
# [[1 2 1 2 1 2]
# [3 4 3 4 3 4]]
y = np.tile(x, (3, 1))
print(y)
# [[1 2]
# [3 4]
# [1 2]
# [3 4]
# [1 2]
# [3 4]]
y = np.tile(x, (3, 3))
print(y)
# [[1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]]
# numpy.repeat()
import numpy as np
x = np.repeat(3, 4)
print(x) # [3 3 3 3]
x = np.array([[1, 2], [3, 4]])
y = np.repeat(x, 2)
print(y)
# [1 1 2 2 3 3 4 4]
y = np.repeat(x, 2, axis=0)
print(y)
# [[1 2]
# [1 2]
# [3 4]
# [3 4]]
y = np.repeat(x, 2, axis=1)
print(y)
# [[1 1 2 2]
# [3 3 4 4]]
y = np.repeat(x, [2, 3], axis=0)
print(y)
# [[1 2]
# [1 2]
# [3 4]
# [3 4]
# [3 4]]
y = np.repeat(x, [2, 3], axis=1)
print(y)
# [[1 1 2 2 2]
# [3 3 4 4 4]]
# 查找数组的唯一元素
a=np.array([1,1,2,3,3,4,4])
b=np.unique(a,return_counts=True)
print(b[0][list(b[1]).index(1)])
#2