. Advertisement .
..3..
. Advertisement .
..4..
This guide shows you how to zip NumPy arrays, allowing you to combine multiple 1D arrays into a 2D array.
Zip NumPy Arrays
With zip()
The built-in function zip()
can aggregate elements of iterables and combine them into a zip object.
It can take iterables, such as lists, tuples, sets, dictionaries, and of course, NumPy arrays, as arguments. The result is an iterator of tuples storing values of the arguments as pairs, meaning the i-th tuple has the i-th element of every iterable or sequence.
You should use arguments of equal lengths because the zip()
function stops when it exhausts the shortest iterable in the input. If you want to use unmatched values from longer iterables, make use of the itertools.zip_longest()
function instead.
When there is only one iterable argument, the function returns an iterator of one tuple. When there is no argument, an empty iterator is returned.
Supposed we have to NumPy arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
If you “zip” them together (meaning to use them as arguments of the zip()
function), we will get a zip object that contains elements of the two arrays as pairs.
>>> zip(a, b)
<zip object at 0x7f71695df180>
However, in order to convert it to a NumPy array with the numpy.array()
function, we need to convert it to a Python list first.
>>> list(c)
[(1, 4), (2, 5), (3, 6)]
The numpy.array()
can then accept this list and convert it to a 2D NumPy array. The following example shows how you can make use of the zip()
function and convert two 1D NumPy arrays into a 2D one.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array(list(zip(a,b)))
print(c)
Output:
[[1 4]
[2 5]
[3 6]]
If you don’t include a conversion with the list()
function, we will end up with an array of the zip object, not elements:
>>> c = np.array(zip(a,b))
>>> print(c)
<zip object at 0x7f7167fc7e40>
With numpy.stack()
The stack()
function of the numpy module can join multiple arrays along a specific axis and form a stacked array. Compared to the input arrays, the resulting array will have one more dimension.
Note: you will need NumPy 1.10.0 or above. Check out this guide if you want to upgrade it.
Syntax:
numpy.stack(arrays, axis, out)
Parameters:
- arrays: the sequences of arrays (or array-like objects) that have the same shape.
- axis: this optional parameter controls the axis along which the function stacks the input arrays. The default value is 0 (the first dimension). If you want to join them along the last dimension, set it to 1.
- output: the destination of the result.
This is how you can rewrite the example above with the function stack()
:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.stack((a,b), axis = 1)
print(c)
Output:
[[1 4]
[2 5]
[3 6]]
We need to specify the axis parameter since the values of the returned array are stacked along the second dimension. Omit this parameter when you want to join the values along the first dimension:
c = np.stack((a,b))
print(c)
Output:
[[1 2 3]
[4 5 6]]
With numpy.column_stack()
The numpy.column_stack()
function is a shorthand of the stack(0 function with axis is set to 1.
c = np.column_stack((a,b))
print(c)
Output:
[[1 4]
[2 5]
[3 6]]
Summary
You can zip NumPy arrays with the zip()
, stack()
, and column_stack()
functions. They can join elements of two one-dimensional NumPy arrays into a single two-dimensional array.
Leave a comment