文章目录
- Numpy之矩阵的堆叠与拆分
- hsplit
- vstack
- 代码实现
- 代码实现
- stack
- vstack
- hstack
- column_stack
- 代码实现
- 代码实现
- 代码实现
- 代码实现
- 矩阵的堆叠
- 矩阵的拆分
矩阵的堆叠
stack
原型:numpy.stack(arrary a, axis=0)
功能:矩阵堆叠扩展1个维度
代码实现
import numpy as np a=[[1,2,3], [4,5,6]]b=[[1,2,3], [4,5,6]]c=[[1,2,3], [4,5,6]]print("a=",a)print("b=",b)print("c=",c)print("增加一维,新维度的下标为0")d=np.stack((a,b,c),axis=0)print(d)print("增加一维,新维度的下标为1")d=np.stack((a,b,c),axis=1)print(d)print("增加一维,新维度的下标为2")d=np.stack((a,b,c),axis=2)print(d)
输出
a= [[1, 2, 3], [4, 5, 6]]b= [[1, 2, 3], [4, 5, 6]]c= [[1, 2, 3], [4, 5, 6]]增加一维,新维度的下标为0[[[1 2 3] [4 5 6]] [[1 2 3] [4 5 6]] [[1 2 3] [4 5 6]]]增加一维,新维度的下标为1[[[1 2 3] [1 2 3] [1 2 3]] [[4 5 6] [4 5 6] [4 5 6]]]增加一维,新维度的下标为2[[[1 1 1] [2 2 2] [3 3 3]] [[4 4 4] [5 5 5] [6 6 6]]]
import numpy as np a=[1,2,3,4]b=[5,6,7,8]c=[9,10,11,12]print("a=",a)print("b=",b)print("c=",c)print("增加一维,新维度的下标为0")d=np.stack((a,b,c),axis=0)print(d)print("增加一维,新维度的下标为1")d=np.stack((a,b,c),axis=1)print(d)
a= [1, 2, 3, 4]b= [5, 6, 7, 8]c= [9, 10, 11, 12]增加一维,新维度的下标为0[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]增加一维,新维度的下标为1[[ 1 5 9] [ 2 6 10] [ 3 7 11] [ 4 8 12]]
vstack
原型:numpy.vstack(array a, array b)
功能:把b矩阵在a的基础上扩充行
代码实现
import numpy as np a = np.floor(10*np.random.random((3, 3)))print(a)print("=================")b = np.floor(10*np.random.random((3, 3)))print(b)print("=================")print(np.vstack((a, b)))
输出
[[6. 8. 3.] [8. 7. 3.] [2. 4. 4.]]=================[[3. 9. 3.] [1. 3. 9.] [1. 8. 4.]]=================[[6. 8. 3.] [8. 7. 3.] [2. 4. 4.] [3. 9. 3.] [1. 3. 9.] [1. 8. 4.]]
hstack
原型:numpy.hstack(array a, array b)
功能:把b矩阵在a的基础上扩充列
代码实现
import numpy as np a = np.floor(10*np.random.random((3, 3)))print(a)print("=================")b = np.floor(10*np.random.random((3, 3)))print(b)print("=================")print(np.hstack((a, b)))
输出
[[6. 8. 3.] [8. 7. 3.] [2. 4. 4.]]=================[[3. 9. 3.] [1. 3. 9.] [1. 8. 4.]]=================[[6. 8. 3. 3. 9. 3.] [8. 7. 3. 1. 3. 9.] [2. 4. 4. 1. 8. 4.]]
column_stack
原型:numpy.column_stack(array a)
功能:将一维矩阵作为列堆叠成二维矩阵
代码实现
import numpy as npfrom numpy import newaxis a = np.floor(10*np.random.random((3, 3)))print(a)print("=================")b = np.floor(10*np.random.random((3, 3)))print(b)print("=================")print(np.column_stack((a, b)))print("=================")a = np.array([4., 2.])b = np.array([3., 8.])print(np.column_stack((a, b)))print("=================")print(a[:, newaxis])print("=================")print(b[:, newaxis])print("=================")print(np.column_stack((a[:, newaxis], b[:, newaxis])))print("=================")print(np.hstack((a[:, newaxis], b[:, newaxis])))print("=================")
输出
[[3. 5. 6.] [3. 2. 4.] [6. 3. 5.]]=================[[2. 2. 0.] [0. 3. 5.] [7. 4. 7.]]=================[[3. 5. 6. 2. 2. 0.] [3. 2. 4. 0. 3. 5.] [6. 3. 5. 7. 4. 7.]]=================[[4. 3.] [2. 8.]]=================[[4.] [2.]]=================[[3.] [8.]]=================[[4. 3.] [2. 8.]]=================[[4. 3.] [2. 8.]]=================
矩阵的拆分
hsplit
原型:numpy.hsplit(ary, indices_or_sections)
功能:将数组拆分为多个大小相等的子数组。使用hsplit,通过指定要返回的相同shape的array的数量,或者通过指定分割应该发生之后的列来沿着其横轴拆分原array
代码实现
import numpy as np x = np.arange(16.0).reshape(4, 4)print(x)print("=================")print(np.hsplit(x, 2))print("=================")print(np.hsplit(x, np.array([3, 6])))
输出
[[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.] [12. 13. 14. 15.]]=================[array([[ 0., 1.], [ 4., 5.], [ 8., 9.], [12., 13.]]), array([[ 2., 3.], [ 6., 7.], [10., 11.], [14., 15.]])]=================[array([[ 0., 1., 2.], [ 4., 5., 6.], [ 8., 9., 10.], [12., 13., 14.]]), array([[ 3.], [ 7.], [11.], [15.]]), array([], shape=(4, 0), dtype=float64)]
vstack
原型:numpy.vstack(ary, indices_or_sections)
功能:vsplit沿着垂直轴分割,其分割方式与hsplit用法相同。
代码实现
import numpy as np x = np.arange(16.0).reshape(4, 4)print(x)print("=================")print(np.vsplit(x, 2))print("=================")print(np.vsplit(x, np.array([3, 6])))
输出
[[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.] [12. 13. 14. 15.]]=================[array([[0., 1., 2., 3.], [4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.], [12., 13., 14., 15.]])]=================[array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]]), array([[12., 13., 14., 15.]]), array([], shape=(0, 4), dtype=float64)]