Array Concatenation and methods of stack | numpy array tutorial | Codin india

 Numpy is Python module that is used for array creation, modification and creating sub arrays for numpy arrays.

Concatenation of arrays

Concatenation or joining of two arrays in Numpy, is primarily accomplished through the routines np.concatenate, np.vstack and np.hstack. Let us see all of the one by one.

  • Concatenate:-

It will concatenate two arrays and gives the output as one array. Let us see by code:-

np.concatenate function


From the above code, it is clear that np.concatenate takes two argument as input and adds the value of both array and return the output as one array.

Concatenate 3 or more arrays:-

we can concatenate as any array we want. Let us talk, where the case of 3 arrays, we will concatenate all the 3 arrays

numpy concatenate


Concatenation of same array:-

We can also concatenate same array together. Like we can concatenate x + x.

Let's see the Code:-

concatenate same array

NOTE:- point here to be noted that, the arguments should be passed in [] brackets only. 

np.concatenate([x, y])

2-D or multidimensional Array Concatenation:-

We can also concatenate the 2-D array or multidimensional array as well.

Let us see the code where we concatenate 2-D array.

Code:-

2d array concatenate

By default is concatenate on the row but we can set its axis to 1 so that it can concatenate column-wise.

Code:- 

column wise concatenation

What is np.vstack?

Numpy provides concatenation vertically on 2D and multidimensional array. np.vstack concatenate array vertically.

Let us see by example:-

np.vstack

NOTE:- Point here to be noted that in np.vstack there should be same length of columns regardless of the rows.

What is np.hstack?

np.hstack provides the concatenation of the array horizontally. It will compare if both have same length of the row or not regardless of the size of columns. If the size of the row mismatch, it willl throw an array.

Code:-

np.vstack

I hope you can understand by our methods. Please Subscribe to our Youtube channel 

If you want to learn Data Science in hindi, you can go through our channel. We are creating Data Science Tutorial.


Codin India - India towards Digitalization


Thanks to be here

Comments