#### World's Best AI Learning Platform with profoundly Demanding Certification Programs

Designed by IITian's, only for AI Learners.

Designed by IITian's, only for AI Learners.

New to InsideAIML? Create an account

Employer? Create an account

Download our e-book of Introduction To Python

How to leave/exit/deactivate a Python virtualenvironment Exception Type: JSONDecodeError at /update/ Exception Value: Expecting value: line 1 column 1 (char 0) how to store the name of independent variable in a list which show non linear behavior HOW TO REMOVE OBJECT COLUMNS IN DATAFRAME. For loop giving incorrect answer What is the difference between a module and a package in Python? What is RMSE and MSE in linear regression models? How to know given a binary tree is a binary search tree or not? Join Discussion

4.5 (1,292 Ratings)

547 Learners

Oct 2nd (4:00 PM) 174 Registered

Shashank Shanu

10 months ago

- Joining NumPy Arrays using concatenate () function

- Joining Arrays using Stack Functions in python.

- Stacking Along Rows: hstack()

- Stacking Along Columns

As you
observed many times, we need to join two different arrays while performing any
machine learning or deep learning tasks. In this article, I will try to show
you how we can join two arrays using different available functions in python.

So, let’s
start

In SQL, we
join two different tables based on a primary key, whereas in NumPy we join
arrays based on their axes.

Let’s
take an example:

```
# import library
import numpy as np
# create array
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
joined_arr = np.concatenate((array1, array2))
print(joined_arr)
```

`[ 1 2 3 4 5 6 7 8 9 10]`

In the
above example, we can see that we used. concatenate () function of python,
which takes a sequence of arrays that we want to join along with the axis. If
axis is not explicitly given as an argument, it by default takes 0.

There is one method to join array in python called
Stacking. It is same as concatenation but the only difference is that stacking
is done along a new axis.

We can concatenate two 1-D arrays along the second
axis which would result in putting them one over the other. This is known as stacking.

Let’s take an example:

```
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
stacked_array = np.stack((array1, array2), axis=1)
print(stacked_array)
```

```
[[ 1 6]
[ 2 7]
[ 3 8]
[ 4 9]
[ 5 10]]
```

In the above example, we used the stack () function
present in python. We have to pass a sequence of arrays that we want to join along
with the axis. If the axis is not explicitly passed it is taken as 0.

NumPy packages provide us with a helper function: hstack() which
is used when we want to stack two arrays along rows.

```
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
hstacked_array = np.hstack((array1, array2))
print(hstacked_array)
```

`[ 1 2 3 4 5 6 7 8 9 10]`

Note: While applying hstack function we do not provide any axis. As it horizontally stacks python understand itself.

NumPy package also provides us with a helper function known
as: vstack() function which is used when we want to stack two arrays along
columns.

```
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
vstacked_array = np.vstack((array1, array2))
print(vstacked_array)
```

```
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
```

Note: While applying vstack function we do not to
provide any axis. As it vertically stacks python understand itself.

I hope after you enjoyed reading this article and finally,
you came to know about **Join array in python**

For more such blogs/courses on data science, machine
learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.

Thanks for reading…

Happy Programming…