| by Arround The Web | No comments

NumPy hstack()

NumPy” is an extensively utilized Python library/package for scientific computing and data analysis. It offers a high-performance array object and numerous tools to manipulate arrays and process them. To combine data from different sources or append new columns to an existing array, the “numpy.hstack()” function is used in Python.

This tutorial will present a complete guide on Python “numpy.hstack()” function using numerous examples by covering the following content areas:

What is the “numpy.hstack()” Function in Python?

The “numpy.hstack()” function stacks two or more arrays horizontally along their second axis. This means that the arrays are aligned row-wise and concatenated column-wise.

Syntax

numpy.hstack(tup)

Parameters

In the above syntax, the “numpy.hstack()” function accepts the “tup” parameter that is used to specify a tuple or a list of arrays to be stacked horizontally.

Return Value

This function returns a new array that is the horizontal stacking of the initialized arrays. The retrieve array has the identical number of rows as the defined arrays and the sum of their columns. Also, it has the same data type as the defined arrays.

Example 1: Horizontally Stacking Two Arrays Using the “numpy.hstack()” Function

The below code is used to horizontally stack the two input arrays:

import numpy

value_1 = numpy.array([45, 33, 78])

value_2 = numpy.array([23, 49, 19])

output = numpy.hstack((value_1, value_2))

print(output)

In the above code:

  • The “numpy” library is imported and the “numpy.array()” function is used to generate two arrays.
  • The “np.hstack()” function is used to stack the defined arrays horizontally.

Output

As seen, the initialized arrays have been stacked successfully.

Example 2: Horizontally Stacking Two Dimensional Arrays Using the “numpy.hstack()” Function

The following code is used to stack the “2-D” dimensional array:

import numpy

value_1 = numpy.array([[22, 32], [33, 44], [55, 26]])

value_2 = numpy.array([[47, 58], [39, 40], [61, 82]])

output = numpy.hstack((value_1, value_2))

print(output)

In the above code lines, the “numpy.hstack()” function takes the two “2-D” arrays as its arguments and stacks them horizontally.

Output

The two-dimensional array has been stacked successfully in the above output.

Example 3: Horizontally Stacking Three “1-D” Arrays Using the “numpy.hstack()” Function

The below-stated code is used to stack three “1-D” arrays:

import numpy

value_1 = numpy.array([45, 33, 78])

value_2 = numpy.array([23, 49, 19])

value_3 = numpy.array([23])

output = numpy.hstack((value_1, value_2, value_3))

print(output)

According to the above code, the “numpy.hstack()” function is used to stack three given “1-D” arrays horizontally.

Output

The three defined “1-D” arrays have been horizontally stacked successfully.

Conclusion

In Python, the “numpy.hstack()” function of the “numpy” library is used to stack two or more arrays horizontally along their second axis. This Python blog post explained the working of the “numpy.hstack()” function using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply