import numpy as np
A = np.arange(12).reshape((3,4))
print(A)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
用split进行分割
split()包含三个参数,array的名称、分割成几份以及按哪个方向分割(axis = 0或1)
- 上下分割 axis = 0
print(np.split(A,3,axis = 0))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
- 左右分割 axis = 1
print(np.split(A,2,axis = 1))
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
- 用array_split进行不等量的分割
print(np.array_split(A,3,axis = 1))
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
用vsplit和hsplit进行分割
vsplit和hsplit包含两个参数,array的名称和分割成几份
- vertical split 上下分割
print(np.vsplit(A,3))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
- horizontal split 左右分割
print(np.hsplit(A,2))
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
Numpy学习教程来源请戳这里