Skip to main content

How to join Arrays in Numpy

How to join Arrays in Numpy
Hi guys, welcome to another exciting time on this platform. If you new here please do checkout our previous article on python and other programming language that are of interest to you. Also subscribe to our YouTube channel to get the full video tutorial.

In this article, we are going to see how arraying joining works.

Joining simply means combining two or more objects. As we have seen in the previous articles, we deal with a variety of multidimensional arrays in python. In joining the contents two arrays, we use the concept of joining. In most cases, we try joining arrays with the SQL with the help of primary key, but in NumPy with the help axis. And also, with the help of functions that will be examined very soon. Below is a brief content of what we are going see in this article:

  • Joining using concatenate function
  • Joining using stack function
  • Row stacking and column stacking
  • Height stacking

Joining two or more arrays using the concatenate form

We used this function in joining two completely two completely different arrays and this operation takes place along their along axis. And if we don’t specify the axis, the compiler considers the axis as 0.

Example 

import numpy as np
arr1 = np.array([[2, 4]])
arr2 = np.array([[6, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)

NumPy instructs the catenate function to join the two in arrays with respect to the specified axis., which in our case is 1.

OUTPUT

How to join Arrays in Numpy
Let’s take an example with 2-D dimension
import numpy as np
arr1 = np.array([[2, 4], [6, 8]])
arr2 = np.array([[10, 12], [14, 16]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)

Just like in the 1-D array, NumPy also instructs the catenate function to join the 2-D array, while respecting the specified axis, which is still one.

OUTPUT

How to join Arrays in Numpy
Joining arrays using the stack functions.

This simply means putting one array over the other. With this method, we are going to specify a new axis in order to join two arrays. This is no difference from catenating, the only difference comes in with the existence of the new axis.

import numpy as np
arr1= np.array([2, 4, 6])
arr2 = np.array([8, 10, 12])
arr = np.stack((arr1, arr2), axis=1)
print(arr)

Here we are trying to stack along the axis one.

How to join Arrays in Numpy
Stacking along rows

In the NumPy package, we have function called hstack () that helps us to stack along rows.

Example

import numpy as np
arr1 = np.array([4, 5, 3])
arr2 = np.array([2, 7, 6])
arr = np.hstack((arr1, arr2))
print(arr)

We are just trying to stack rows without specifying any number of axis for the row. 

OUTPUT

How to join Arrays in Numpy
Stacking along columns, we are going to use a function called vstack() which will helps us to stack along columns

Example

import numpy as np
arr1 = np.array(['a', 'b', 'c'])
arr2 = np.array(['f', 'e', 'f'])
arr = np.vstack((arr1, arr2))
print(arr)

In this program, we are just stacking up the arrays along the column.

OUTPUT

How to join Arrays in Numpy
Height stacking (depth)

Also known as depth, we can use the dstack() function to stack along arrays with respect to height. 

Example

import numpy as np
arr1 = np.array(['a', 'b', 'c'])
arr2 = np.array(['f', 'e', 'f'])
arr = np.dstack((arr1, arr2))
print(arr)

OUTPUT

How to join Arrays in Numpy
I hope this article was very interesting. If so, do share it with those are willing to learn python and other programming languages. Have a nice time coding guy.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...