How to stack two numpy arrays in python ?


Introduction

In this article, we will be discussing how to stack two NumPy arrays in Python. The ability to combine multiple arrays into one is an essential feature of NumPy, as it allows for more complex calculations and data analysis.

Stacking refers to combining multiple arrays into a single array, either along a new axis or an existing one. This allows for easier and more efficient manipulation of data.

Numpy offers several methods for combining arrays, providing flexibility and versatility in array stacking. The stack function combines a sequence of arrays along an established axis, while the concatenate function merges two or more arrays into a single array.

Both methods are important to understand in order to effectively stack numpy arrays in python.

Checking arrays dimensions

Before combining two arrays, it is prudent to first check their dimensions. One approach to achieve this is by utilizing the shape function.

Let's consider an example where we have two arrays, A and B:

import numpy as np

A = np.array([1,2,3,4])
B = np.array([5,6,7,8])

print(A.shape)
print(A.shape)

it will return

(4,)
(4,)

respectively. We can see that both arrays possess a single dimension. We have 4 rows, but there is no dimension associated with the column. While this may be acceptable, it can potentially result in errors depending on how you intend to combine the arrays.

To prevent the potential errors that will be discussed in the following sections, a solution is to reshape your arrays.

A_new = A.reshape(A.shape[0],1)
B_new = B.reshape(A.shape[0],1)

print(A_new.shape)
print(B_new.shape)

it will then return

(4, 1)
(4, 1)

Please note that A_new and B_new have the same number of elements as A and B. However, A_new and B_new are two-dimensional arrays with a single column dimension.

Using numpy stack() function

To stack two numpy arrays, we can use the np.stack() function. This function takes in the two arrays as its arguments and returns a new array that contains both of the original arrays.

S = np.stack( (A,B) )

print(S)
print(S.shape)

Outputs

array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

and

(2, 4)

By default, np.stack() will stack the arrays along a new axis, creating a new dimension in the resulting array. In our example, we stacked two one-dimensional arrays and ended up with a two-dimensional array.

Specifying Axis for Stacking

We can also specify which axis to stack the arrays along using the axis parameter in`np.stack(). This allows for more control over how the arrays are combined. For example, if we want to stack our original arrays along the horizontal axis, we can specify axis=1 as shown below:

S = np.stack( (A,B), axis=1 )

print(S)
print(S.shape)

Outputs

[[1 5]
 [2 6]
 [3 7]
 [4 8]]

and

(4, 2)

This results in a two-dimensional array with the shape of (4, 2).

Note that axis=0 is the default axis

S = np.stack( (A,B), axis=0 )

print(S)
print(S.shape)

Outputs

[[1 2 3 4]
 [5 6 7 8]]

and

(2, 4)

same output as doing np.stack( (A,B) ).

If we try to use the axis parameter with a value that does not exist in the input arrays, such as in the case of

np.stack((A, B), axis=2)

, an error message will be displayed:

"AxisError: axis 2 is out of bounds for array of dimension 2."

Aside from np.stack(), there are also other functions in numpy that can be used to stack arrays. These include np.hstack() for horizontal stacking, np.vstack() for vertical stacking, and np.concatenate() for concatenating multiple arrays along a specified axis.

Stacking multiple arrays

Please be aware that the stack function offers an efficient way to combine multiple arrays simultaneously. This versatile function accepts a sequence of arrays as input, allowing for the combination of two or more arrays. Example:

C = np.array([9,10,11,12])

S = np.stack( (A,B,C), axis=1 )

print(S)
print(S.shape)

Outputs:

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

and

(4, 3)

Using numpy concatenate() function

Another approach to stack numpy arrays is by using the concatenate function. However, it is important to exercise caution as there are certain distinctions compared to the numpy stack function. While the stack function stacks arrays along a new axis, the concatenate function joins arrays along an existing axis. In simpler words, it merges two or more arrays based on a specified dimension. Examples:

S = np.concatenate((A, B))

print(S)
print(S.shape)

Outputs:

[1 2 3 4 5 6 7 8]

and

(8,)

Same as

S = np.concatenate((A, B), axis=0)

print(S)
print(S.shape)

Outputs:

[1 2 3 4 5 6 7 8]

and

(8,)

However:

S = np.concatenate((A, B), axis=1)

print(S)

, an error message will be displayed:

AxisError: axis 1 is out of bounds for array of dimension 1

To prevent this error, utilize the reshaped arrays that were defined in the initial section.

S = np.concatenate((A_new, B_new), axis=1)

print(S)
print(S.shape)

Outputs:

[[1 5]
 [2 6]
 [3 7]
 [4 8]]

and

(4, 2)

Stacking multiple arrays

Please be aware that the concatenate function offers an efficient way to combine multiple arrays simultaneously as well. This versatile function also accepts a sequence of arrays as input, allowing for the combination of two or more arrays. Example:

S = np.concatenate( (A,B,C) )

print(S)
print(S.shape)

Outputs:

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

and

(12,)

Conclusion

Stacking two numpy arrays in Python is a simple yet powerful technique that allows for easier data manipulation and analysis. By understanding the concept of stacking and utilizing the different functions available in numpy, we can efficiently combine our arrays to meet our data needs.

References

Links Site
numpy.concatenate numpy.org
numpy.column_stack numpy.org
numpy.stack numpy.org
numpy.reshape numpy.org