4 Joining the arrays
4.1 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]])
4.2 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]]])
4.3 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]])
4.4 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]])
Conclusion:
In conclusion, NumPy is an essential library for Python, providing a powerful suite of operations that are crucial for numerical computing. Whether you’re manipulating arrays, performing mathematical operations, or optimizing performance with vectorization, NumPy offers efficient solutions that are both fast and easy to implement. By mastering NumPy operations, you’ll be well-equipped to tackle complex data science tasks, from basic data manipulation to advanced machine learning algorithms.
For anyone looking to delve deeper into NumPy, it is highly recommended to explore its official documentation by clicking here. If you’re interested to learn the python concepts then check out our more articles by clicking here