[转]Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

觉得有用的话,欢迎一起讨论相互学习~

我的微博我的github我的B站

转载链接:https://blog.csdn.net/GarfieldEr007/article/details/51378296

numpy.stack()函数

  • 函数原型:numpy.stack(arrays, axis=0)

程序实例:

>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)

>>>

>>> np.stack(arrays, axis=1).shape
(3, 10, 4)

>>>

>>> np.stack(arrays, axis=2).shape
(3, 4, 10)

>>>

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b))
array([[1, 2, 3],
       [2, 3, 4]])

>>>

>>> np.stack((a, b), axis=-1)
array([[1, 2],
       [2, 3],
       [3, 4]])

numpy.hstack()函数

  • 函数原型:numpy.hstack(tup)

  • 其中tup是arrays序列,The arrays must have the same shape, except in the dimensioncorresponding to axis (the first, by default).

  • 等价于:np.concatenate(tup, axis=1)

程序实例:

>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.hstack((a,b))
array([[1, 2],
       [2, 3],
       [3, 4]])

numpy.vstack()函数

  • 函数原型:numpy.vstack(tup)

  • 等价于:np.concatenate(tup, axis=0) if tup contains arrays thatare at least 2-dimensional.

程序实例:

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.vstack((a,b))
array([[1, 2, 3],
       [2, 3, 4]])

>>>

>>> a = np.array([[1], [2], [3]])
>>> b = np.array([[2], [3], [4]])
>>> np.vstack((a,b))
array([[1],
       [2],
       [3],
       [2],
       [3],
       [4]])

numpy.dstack()函数

  • 函数原型:numpy.dstack(tup)

  • 等价于:np.concatenate(tup, axis=2)

程序实例:

>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.dstack((a,b))
array([[[1, 2],
        [2, 3],
        [3, 4]]])

>>>

>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.dstack((a,b))
array([[[1, 2]],
       [[2, 3]],
       [[3, 4]]])

numpy.concatenate()函数

  • 函数原型:numpy.concatenate((a1, a2, ...), axis=0)

程序实例:

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])

This function will not preserve masking of MaskedArray inputs.
>>>

>>> a = np.ma.arange(3)
>>> a[1] = np.ma.masked
>>> b = np.arange(2, 5)
>>> a
masked_array(data = [0 -- 2],
             mask = [False  True False],
       fill_value = 999999)
>>> b
array([2, 3, 4])
>>> np.concatenate([a, b])
masked_array(data = [0 1 2 2 3 4],
             mask = False,
       fill_value = 999999)
>>> np.ma.concatenate([a, b])
masked_array(data = [0 -- 2 2 3 4],
             mask = [False  True False False False False],
       fill_value = 999999)

numpy.vsplit()函数

  • 函数原型:numpy.vsplit(ary, indices_or_sections)

程序实例:

>>> x = np.arange(16.0).reshape(4, 4)
>>> x
array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.],
       [ 12.,  13.,  14.,  15.]])
>>> np.vsplit(x, 2)
[array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  6.,  7.]]),
 array([[  8.,   9.,  10.,  11.],
       [ 12.,  13.,  14.,  15.]])]
>>> np.vsplit(x, np.array([3, 6]))
[array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.]]),
 array([[ 12.,  13.,  14.,  15.]]),
 array([], dtype=float64)]

With a higher dimensional array the split is still along the first axis.
>>>

>>> x = np.arange(8.0).reshape(2, 2, 2)
>>> x
array([[[ 0.,  1.],
        [ 2.,  3.]],
       [[ 4.,  5.],
        [ 6.,  7.]]])
>>> np.vsplit(x, 2)
[array([[[ 0.,  1.],
        [ 2.,  3.]]]),
 array([[[ 4.,  5.],
        [ 6.,  7.]]])]
posted @ 2018-11-12 15:10  WUST许志伟  阅读(1961)  评论(0编辑  收藏  举报