一.组合
1.水平组合
1.1 np.hstack()(该函数组合后不会升高维度)
1.1.1 一维数组的水平组合
[In] a = np.array([1,2,3])
[In] b = 2 * a
[In] np.hstack((a,b))
[Out] array([1, 2, 3, 2, 4, 6]) #组合结果仍为一维数组(维度没有升高)
1.1.2 一维向量的水平组合(向量也是矩阵)
[In] a = np.array([[1,2,3]])
[In] b = 2 * a
[In] np.hstack((a,b))
[Out] array([[1, 2, 3, 2, 4, 6]]) #组合为仍为一维向量(维度没有升高)
1.1.3 二维数组的水平组合
[In] a = np.arange(9).reshape(3,-1)
a
[Out] array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
[In] b = 2*a
[In] b
[Out] array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
[In] np.hstack((a,b))
[Out] array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]]) #按照常规意义的水平组合理解即可
对于二维矩阵,组合单位为该矩阵
1.1.4 三维数组的水平组合
[In] a = np.array([[[1,2,3],
[4,5,6]],
[[7,8,9],
[10,11,12]]])
[In] b = np.array([[[13,14,15],
[16,17,18]],
[[19,20,21],
[22,23,24]]])
[In] np.hstack((a,b))
[Out] array([[[ 1, 2, 3],
[ 4, 5, 6],
[13, 14, 15],
[16, 17, 18]],
[[ 7, 8, 9],
[10, 11, 12],
[19, 20, 21],
[22, 23, 24]]])
对于三维矩阵,组合单位为二维矩阵
具体到这个例子中我们将“[[1,2,3],[4,5,6]]”、“[[7,8,9],[10,11,12]]”分别当为一个元素A,B,则a = [A,B],同理b = [C,D]。对A,C进行水平组合。
1.2 np.concatenate()
在这里插入代码片
2.垂直组合
2.1 np.vstack()
2.1.1 一维数组的垂直组合
[In] import numpy as np
[In] a = np.array([1,2,3])
[In] b = np.array([4,5,6])
[In] np.vstack((a,b))
[Out] array([[1, 2, 3],
[4, 5, 6]])
2.1.2 一维向量的垂直组合(向量也是矩阵)
[In] a = np.array([[1,2,3]])
[In] b = np.array([[4,5,6]])
[In] np.vstack((a,b))
[Out] array([[1, 2, 3],
[4, 5, 6]])
有2.1.1和2.1.2可知,在进行垂直组合的时候,数组和向量没有区别。
2.1.3 二维数组的垂直组合
[In] a = np.array([[1,2,3],[4,5,6],[7,8,9]])
[In] b = np.array([[10,11,12],[13,14,15],[16,17,18]])
[In] np.vstack((a,b))
[Out] array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
2.1.4 三维数组的垂直组合
[In] a = np.array([[[1,2,3],
[4,5,6]],
[[7,8,9],
[10,11,12]]])
[In] b = np.array([[[13,14,15],
[16,17,18]],
[[19,20,21],
[22,23,24]]])
[In] np.vstack((a,b))
[Out] array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]],
[[13, 14, 15],
[16, 17, 18]],
[[19, 20, 21],
[22, 23, 24]]])
垂直组合很好理解,只需要直接将两个矩阵叠在一起即可。
3.深度组合
3.1 数组的深度组合
a = np.array([1,2,3])
b = np.array([4,5,6])
np.dstack((a,b))
array([[[1, 4],
[2, 5],
[3, 6]]])
3.2 向量的深度组合(向量也是矩阵)
a = np.array([[1,2,3]])
b = np.array([[4,5,6]])
np.dstack((a,b))
array([[[1, 4],
[2, 5],
[3, 6]]])
和数组组合出来的结果一样
3.3 二维矩阵的深度组合
a = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
b = np.array([[10,11,12],
[13,14,15],
[16,17,18]])
c = np.dstack((a,b))
c
array([[[ 1, 10],
[ 2, 11],
[ 3, 12]],
[[ 4, 13],
[ 5, 14],
[ 6, 15]],
[[ 7, 16],
[ 8, 17],
[ 9, 18]]])
二维矩阵的深度组合单位为矩阵中的元素
3.4 三维矩阵的深度组合
a = np.arange(12).reshape(2,2,3)
a
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
b = np.arange(12,24).reshape(2,2,3)
b
array([[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]])
np.dstack((a,b))
array([[[ 0, 1, 2, 12, 13, 14],
[ 3, 4, 5, 15, 16, 17]],
[[ 6, 7, 8, 18, 19, 20],
[ 9, 10, 11, 21, 22, 23]]])
深度组合首先确定组合单位。
对于数组、向量、二维矩阵,深度组合的组合单位为单个元素;
对于三维矩阵,深度组合的单位为去掉最外层的两个“[”后接下来的“[”和“]”之间的内容。在本例中就是深度组合的单位就是[0,1,2]
4. 行组合和列组合
(1)对于一维数组,行(列)组合就是将数组变成行(列)向量,然后一行一行(一列一列)地放一起(数组既不是行向量也不是列向量);对于行列向量则直接组合即可。
[In] one = np.arange(2)
two = one * 2
one
[Out] array([0, 1])
[In] two
[Out] array([0, 2])
[In] np.row_stack((one,two))
[Out] array([[0, 1],
[0, 2]])
[In] np.column_stack((one,two))
[Out] array([[0, 0],
[1, 2]])
[In] x = one.reshape(1,-1)
x
[Out] array([[0,1]])
[In] y = two.reshape(1,-1)
y
[Out] array([[0,2]])
[In] np.row_stack((x,y))
[Out] array([[0,1],
[0,2]])
[In] np.column_stack((x,y))
[Out] array([[0,1,0,2]])
(2)对于二维矩阵,行组合相当于vstask();列组合相当于和hstack()
[In] np.row_stack((a,b))
[Out] array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
[In] np.column_stack((a,b))
[Out] array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]])
二.分割
1.水平切割
[In] a = np.arange(9).reshape(3,3)
a
[Out] array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
[In] np.hsplit(a,3)
[Out] [
array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
[In] np.split(a,3,axis = 1)
[Out] [
array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
2.垂直切割
[In] np.vsplit(a,3)
[Out] [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
[In] np.split(a,3,axis = 0)
[Out] [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
水平/垂直切割不会减少“[”和“]”的深度
3.深度切割
深度切割要求数组维数大于等于3维
[In] c = np.arange(27).reshape(3,3,3)
[In] c
[Out] array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
[In] np.dsplit(c,3)
[Out] [array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]),
array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]),
array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]