NumPy vstack() method – With Easy Examples

Filed Under: Python
NumPy Vstack() Method

Hello, readers! In this article, we will be focusing on the NumPy vstack() function with different examples.

So, let us begin!! 馃檪


Functioning of NumPy vstack() method

As we all know, Python offers the NumPy module as a point to begin in the section of mathematics. With the NumPy module in the picture, we have got a lot of methods to automate the processes and save time.

When it comes to the analysis and preparation of data, NumPy offers us arrays to store the data values at ease in a secular manner. During the same, we may come across scenarios where we would want to combine or stack different arrays into a single array without having to lose any data element from it.

This is when the NumPy vstack() function comes into the picture.

With the NumPy vstack() function, we can stack data arrays into a single array without losing the data values from them. Yes, it takes arrays as input and then concatenates them altogether vertically alongside the first dimension. By this, it clubs them into a single array.

Syntax:

numpy.vstack(tup)

It takes a sequence of arrays as input in the form of a tuple and concatenates them into a single array along the vertical axis.

Having understood the working of the NumPy vstack() function, let us now test it with different shapes of arrays.


Scenario 1: Basic Implementation of NumPy vstack() method

As a part of the basic implementation, we have created two 1-D arrays and then we make use of the vstack() function to club the arrays together to form a vertically stacked array.

Example:

In this example, we have created two simple NumPy arrays with array() function. Post which, we have applied the vstack() function to create a vertical stack.

import numpy
 
x = numpy.array([1])
y = numpy.array([2])
 
stk = numpy.vstack((x,y))
print("The stacked array")
print(stk)

Output:

The stacked array
[[1]
 [2]]

Scenario 2: Stacking 2-D arrays with vstack() function

In this scenario, we have created two 2-D arrays using the array() function. Now, the next task is to perform vertical stacking on these two arrays.

We club the arrays together in a row-wise manner using the vstack() function.

Example:

import numpy
 
x = numpy.array([ [0, 0], [1, 1] ])
y = numpy.array([ [2, 2], [2,2]])
 
stk = numpy.vstack((x, y))
print(stk)

Output:

As seen below, all the 2-D arrays are merged together (without hampering their configuration) to create a vertically stacked array. We can imagine this array as a stack where we push it in a vertical fashion (bottom-up approach).

[[0 0]
 [1 1]
 [2 2]
 [2 2]]

Scenario 3: Implementing Numpy vstack() on arrays of different shapes

Can we have arrays of different shapes as parameters to the vstack() function?

The straightforward answer for this is NO. We cannot have arrays of different shapes as parameters to vstack() function for vertical concatenation.

Example:

Here, we have created an array with two elements and another array with just a single element. So, as the number of elements in the arrays differs, the shape() method won’t work well as shown below.

import numpy
 
x = numpy.array([1, 2])
y = numpy.array([2])
 
stk = numpy.vstack((x,y))
print("The stacked array")
print(stk)

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-878bb49aea48> in <module>
      4 y = numpy.array([2])
      5 
----> 6 stk = numpy.vstack((x,y))
      7 print("The stacked array")
      8 print(stk)

<__array_function__ internals> in vstack(*args, **kwargs)

c:\users\hp\appdata\local\programs\python\python36\lib\site-packages\numpy\core\shape_base.py in vstack(tup)
    281     if not isinstance(arrs, list):
    282         arrs = [arrs]
--> 283     return _nx.concatenate(arrs, 0)
    284 
    285 

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 2 and the array at index 1 has size 1

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content