1.数组的拼接
import numpy as np
1.横着拼
>> x = np.array([1,2,3])
>> y = np.array([3,2,1])
>> np.concatenate([x,y])
array([1, 2, 3, 3, 2, 1])
>> z = [99,99,99]
>> print(np.concatenate([x,y,z]))
[ 1 2 3 3 2 1 99 99 99]
2.二维数组拼接,沿着第一个轴拼接
>> grid = np.array([[1,2,3],[4,5,6]])
>> np.concatenate([grid,grid])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
3.二维数组拼接,沿着第二个轴拼接
np.concatenate([grid,grid],axis = 1)
>> array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
4.垂直拼接 vertically 垂直地 [ˈvɜrtɪkli]
>> x = np.array([1,2,3])
>> grid = np.array([[9,8,7],[6,5,4]])
>> np.vstack([x,grid])
array([[1, 2, 3],
[9, 8, 7],
[6, 5, 4]])
5.水平拼接 horizontally 水平地 [ˌhɔːrɪˈzɑːntəli]
>> grid = np.array([[9, 8, 7],
[6, 5, 4]])
>> y = np.array([[99],[99]])
>> np.hstack([grid,y])
array([[ 9, 8, 7, 99],
[ 6, 5, 4, 99]])
2. 数组的分裂
与拼接的过程相反,可以通过np.split、np.hsplit、np.vsplit
>> x = [1,2,3,99,99,3,2,1]
>> x1,x2,x3 = np.split(x,[3,5])
array([1, 2, 3]), array([99, 99]), array([3, 2, 1])
>> grid = np.arange(16).reshape(4,4)
>> upper,lower = np.vsplit(grid,[2])
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>> left,right = np.hsplit(grid,[2])
array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]])
array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])