Numpy:数组拼接——np.hstack() ; np.vstack()和数组拆分——np.hsplit() ;np.vsplit()用法总结

数组拼接——np.hstack()和np.vstack()

数组拼接:

  • np.hstack():横向拼接,增加特征量
  • np.vstack():纵向拼接,增加样本个数

实例:

import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print (a)
print ('--------------------')
print (b)
>>>
[[4. 9.]
 [0. 9.]]
--------------------
[[5. 0.]
 [6. 8.]]
 
print ('----------np.hstack():横向拼接,增加特征量------------')
print(np.hstack((a,b)))

print ('----------np.vstack():纵向拼接,增加样本个数------------')
print (np.vstack((a,b)))
>>>
----------np.hstack():横向拼接,增加特征量------------
[[4. 9. 5. 0.]
 [0. 9. 6. 8.]]
----------np.vstack():纵向拼接,增加样本个数------------
[[4. 9.]
 [0. 9.]
 [5. 0.]
 [6. 8.]]

数组拆分——np.hsplit()和np.vsplit()

数组拆分:

  • np.hsplit():纵向切割,将横长数组切割为横短数组
  • np.vsplit( ):横向切割,将纵长数组切割为纵短数组

np.hsplit()函数的常见用法:

通过np.hsplit()函数水平(按列)将一个数组拆分为多个子数组。hsplit相当于split与axis=1,无论数组尺寸如何,数组始终沿第二条轴拆分。

  • np.hsplit(a, m): # Split a into m—— 将数组a平均拆分成m个数组
  • np.hsplit(a, (m, n)): # Split a before the index of the columns of m and n—— 将数组a从第m和第n列的索引之前拆分成3个数组
  • np.hsplit(a, (m, n, p)): # Split a before the index of the columns of m、n and p——将数组a从从第m、n、p列的索引之前拆分成4个数组

np.hsplit()函数的实例:

a = np.floor(10*np.random.random((2,18)))   # 生成2*18的随机数组,同时向下取整
print(a)
array([[8., 8., 5., 7., 0., 6., 2., 5., 1., 1., 3., 6., 8., 3., 2., 3.,
        0., 0.],
       [5., 6., 5., 5., 6., 4., 3., 7., 0., 6., 3., 9., 4., 8., 9., 9.,
        4., 5.]])
# 将数组a平均切割成3份
print(np.hsplit(a, 3))
>>>
[array([[8., 8., 5., 7., 0., 6.],
        [5., 6., 5., 5., 6., 4.]]),
 array([[2., 5., 1., 1., 3., 6.],
        [3., 7., 0., 6., 3., 9.]]),
 array([[8., 3., 2., 3., 0., 0.],
        [4., 8., 9., 9., 4., 5.]])]
# 将数组a从第m和第n列的索引之前拆分成3个数组 
print(np.hsplit(a, (3,4)))
>>>
[array([[8., 8., 5.],
        [5., 6., 5.]]),
 array([[7.],
        [5.]]),
 array([[0., 6., 2., 5., 1., 1., 3., 6., 8., 3., 2., 3., 0., 0.],
        [6., 4., 3., 7., 0., 6., 3., 9., 4., 8., 9., 9., 4., 5.]])]
# 将数组a从第m和第n列的索引之前拆分成3个数组 
print(np.hsplit(a, (3,6)))
>>>
[array([[8., 8., 5.],
        [5., 6., 5.]]),
 array([[7., 0., 6.],
        [5., 6., 4.]]),
 array([[2., 5., 1., 1., 3., 6., 8., 3., 2., 3., 0., 0.],
        [3., 7., 0., 6., 3., 9., 4., 8., 9., 9., 4., 5.]])]
# 从第3、4、5 列之间切开,拆分成4个数组
print(np.hsplit(a, (2,6,10)))
>>>
[array([[8., 8.],
        [5., 6.]]),
 array([[5., 7., 0., 6.],
        [5., 5., 6., 4.]]),
 array([[2., 5., 1., 1.],
        [3., 7., 0., 6.]]),
 array([[3., 6., 8., 3., 2., 3., 0., 0.],
        [3., 9., 4., 8., 9., 9., 4., 5.]])]

对于高维数组,拆分仍沿第二轴。

x = np.arange(8.0).reshape(2, 2, 2)
x
>>>
array([[[0., 1.],
        [2., 3.]],

       [[4., 5.],
        [6., 7.]]])
print(np.hsplit(x,2))
>>>
[array([[[0., 1.]],
 
        [[4., 5.]]]),
 array([[[2., 3.]],
 
        [[6., 7.]]])]

np.vsplit()函数的实例:

np.vsplit(a,2)
>>>
[array([[8., 8., 5., 7., 0., 6., 2., 5., 1., 1., 3., 6., 8., 3., 2., 3.,
         0., 0.]]),
 array([[5., 6., 5., 5., 6., 4., 3., 7., 0., 6., 3., 9., 4., 8., 9., 9.,
         4., 5.]])]

猜你喜欢

转载自blog.csdn.net/weixin_42782150/article/details/108197526