Python

# NumPy hstack()

The NumPy package in Python has a plethora of useful methods. The NumPy hstack() utility is one well innovative and time-saving solution. We frequently need to combine many matrices into a single array avoiding deleting their values. And this is all accomplished with just 1 piece of code. The hstack() method is used to tackle this issue. The hstack() method is being used to build a single array by stacking the series of input matrices horizontally (i.e. column evenly). Apart from 1-D arrays, in which it concatenates across the first axis, this is equal to combining all along the second axis. Rebuilds arrays that have been partitioned by a split() method. Except for the second axis, the arrays should be identical in form. This method worked well with arrays of up to three dimensions. Within this article, we will be looking at the hstack() function’s working in Spyder 3. Thus, let’s take a fresh start with some examples.

## Example 01:

You need to import the NumPy package first in the code with its object as “n” through the keyword “import”. After this, we have to define two NumPy arrays named A1 and A2 with the help of a NumPy array() function. Both the arrays are 2 dimensional here, i.e. 2 columns each. Now, the hstack() function is here to join both the arrays and align them horizontally. So, the first dimensional values of array A1 will be joined with the 1st-dimensional values of array A2. Both the arrays have been passed to the hstack() function of NumPy and saved the concatenated array into new variable A. After this, single arrays have been outputted using the print() method. In last, the concatenated horizontal aligned array is also printed out.

import numpy as n
A1 = n.array([[1, 2], [3, 4]])
A2 = n.array([[5, 6], [7, 8]])
A = n.hstack((A1, A2))
print("Array 1:\n", A1)
print("Array 2:\n", A2)
print("Single Array:\n", A)

After running the code for the hstack() function on NumPy arrays, we have got two single arrays first and then the concatenated string matrix in horizontal alignment as the output below.

## Example 02:

Let’s take a look at the hstack() function about how it works on the 1-dimensional array of Numpy. So, the code has been started with the same NumPy library import as “n”. After this, two NumPy arrays have been declared using the NumPy “array()” function and saved to the variables A1 and A2. Both arrays are 1 dimensional, i.e. 1 column. The hstack() function of NumPy is here to take both the single NumPy arrays and concatenate them horizontally. The newly made concatenated array will be saved to the new variable A. The print methods are here to display the single NumPy arrays first and then the concatenated array on the output.

import numpy as n
A1 = n.array([[1, 2, 3, 4]])
A2 = n.array([[5, 6, 7, 8]])
A = n.hstack((A1, A2))
print("Array 1:", A1)
print("Array 2:", A2)
print("Single Array:", A)

After running this piece of code, both the single arrays have been displayed as it is. After that, the concatenated new array has been displayed horizontally in a single line using the hstack() function in the code.

## Example 03:

The hstack() function works not only on integer type Numpy arrays but also on string type arrays. So, we will be looking at the 1-dimensional Numpy arrays concatenation using the hstack(). Therefore, the code has been started with initialising two 1-dimensional arrays using the NumPy’s array() function, taking 1 column string type values. The arrays have been saved to the variables A1 and A2. The hstack() function is called with NumPy object “n” to concatenate A1 and A2 arrays horizontally and save the resultant array to the variable A. This new array A will be displayed on the Spyder 3 output screen with the help of the print function.

import numpy as n
A1 = n.array(('One', 'Two', 'Three'))
A2 = n.array(('Four', 'Five', 'Six'))
A = n.hstack((A1, A2))
print("Horizontal Array:", A)

On running the newly made code of python, we have got the display of horizontally made concatenated array from two 1-dimensional arrays A1 and A2.

## Example 04:

Here is the last example of this article today. We have been starting this code with the import of NumPy Library. After that, we have declared and initialized two 3-dimensional Numpy arrays using the “array” function. Both the arrays are of string type and saved to the variables A1 and A2. Here comes the hstack() function taking both the 3-dimensional arrays as an argument to create a single concatenated array of NumPy and save it to the new variable A. The newly made horizontally stacked NumPy array has been outputted using the print() function on the python tool screen.

import numpy as n
A1 = n.array([['One'], ['Two'], ['Three']])
A2 = n.array([['Four'], ['Five'], ['Six']])
A = n.hstack((A1, A2))
print("Horizontal Array:\n", A)

After executing this code, we have got the 3-dimensional horizontally stacked array A.

## Conclusion:

Finally! We implemented all the examples related to the hstack() function of python’s NumPy package. We have tried our best to show the working of hstack() on 1-dimensional, 2 dimensional, and 3-dimensional NumPy arrays. We are extremely motivated that our users will find this article as the bundle of all necessary things to understand the horizontal concatenation using the hstack() function.