Numpy Array Manipulation

Joining the arrays

Concatenate

Concatenation refers to joining. This function is used to join two or more arrays of the same shape along a 
specified axis i.e row(axis=0) or column(axis=1) wise. Let us seen the syntax and some example of numpy 
concatenate operation

Syntax 
concatenate((a1, a2, ...), axis=0, out=None)
a1, a2, ... : a sequence of array_like
The arrays must have the same shape, except in the dimension corresponding to the axis (the first, by default).

axis: int, optional
The axis along which the arrays will be joined. The default is 0.

out: ndarray, optional
If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.

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

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

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

Stack

Join a sequence of arrays along a new axis.
The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

stack(arrays, axis=0, out=None)

arrays-Sequence of arrays of the same shape
axis-Axis in the resultant array along which the input arrays are stacked

>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[5,6],[7,8]])

>>> np.stack((a,b),axis=0)#shape-(2,2,2)
array([[[1, 2],
        [3, 4]],
       [[5, 6],
        [7, 8]]])

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

hstack

Stack arrays in sequence horizontally (column wise).
This is equivalent to concatenation along the second axis, except for 1-D arrays where it concatenates along the first axis. Rebuilds arrays divided by hsplit.

>>> import numpy as np
>>> a=np.array([1,2,3])
>>> a.shape #(3,)
>>> b=np.array([5,6])
>>> b.shape #(2,)

>>> c=np.hstack((a,b)) 
>>> c.shape #(5,)
>>> c
array([1, 2, 3, 5, 6])

>>> x=np.array([[1,2],[3,4],[5,6]])  #(3,2)
>>> y=np.array([[7,8],[9,10]])       #(2, 2)

>>> z=np.hstack((x,y)) #ValueError: all the input array dimensions except for the concatenation axis must match exactly

>>> y=np.array([[7,8],[9,10],[11,12]]) #(3,2)
>>> z=np.hstack((x,y))
>>> z
array([[ 1,  2,  7,  8],
       [ 3,  4,  9, 10],
       [ 5,  6, 11, 12]])

vstack

Stack arrays in sequence vertically (row wise).
This is equivalent to concatenation along the first axis after 1-D arrays of shape (N,) have been reshaped to (1,N). Rebuilds arrays divided by vsplit.

>>> import numpy as np

>>> a=np.array([1,2,3])
>>> a.shape#(3,)
>>> b=np.array([5,6])
>>> b.shape#(2,)

>>> c=np.vstack((a,b))#ValueError: all the input array dimensions except for the concatenation axis must match exactly

>>> b=np.array([5,6,7])
>>> b.shape#(3,)

>>> c=np.vstack((a,b)) 
array([[1, 2, 3],
       [5, 6, 7]])

You May Also Like

About the Author: Nitesh

I am a software engineer and Enthusiastic to learn new things

Leave a Reply

Your email address will not be published. Required fields are marked *