Warning: session_start(): open(/tmp/sess_0f1e169e5f1880c888c10d455381d4a2, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Use The NumPy vstack Function - LearnShareIT

How To Use The NumPy vstack Function

The NumPy vstack function can help you stack multiple arrays vertically. However, it isn’t compatible with every sequence of arrays.

Learn when you can use it and the result you can expect with the explanation and examples below.

Table of Contents

NumPy vstack Function

The numpy.vstack() function is used to stack arrays vertically, meaning that it will take a sequence of 1D or 2D arrays and combine them into a single 2D array.

This function doesn’t need any other argument other than a tuple containing the sequence of NumPy arrays (ndarrays) you want to stack.

Let’s check out its capability when it comes to 1D arrays first. In this case, numpy.vstack() treats them as if they are 2D arrays with just one element in the first dimension. This means a 1D array of the shape (n, ) will be reshaped to a (1, N) array for the concatenation operation.

Here is a simple example:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
c = np.vstack([a, b])
c.shape # (2, 3)
print(c)
[[ 1 2 3]
 [10 20 30]]

As you can see, the output is a 2D ndarray, which is the result of vertical stacking. It has the same number of columns as the input arrays and has as many rows as how many input arrays there are.

Keep in mind a few things to use the numpy.vstack() function:

  • You must install and load the NumPy library first in order to use this function, or your Python program will produce errors like “name ‘array’ is not defined”.
  • You must put your input arrays in a tuple (between a pair of square brackets).
  • The arrays must have the same number of columns in order for the stacking to be successful.

If you feed the numpy.vstack() function 1D arrays of different lengths, it will produce an error like this:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20])
np.vstack([a, b])
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 2

In the code above, we try to stack a and b vertically, but a has one more column than b, so the stacking operation will fail.

For 2D arrays, the function works in a similar way. The provided arrays may have different numbers of rows, but they must have the same elements in the second dimension (columns).

Here are some examples to illustrate the behavior of numpy.vstack() with 2D arrays:

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
np.vstack([a, b])
array([[ 1, 2, 3],
       [ 4, 5, 6],
       [10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])

This function can stack higher-dimensional arrays too, even though it is rare to see it with 4D arrays or above. In these cases, make sure the arrays share the same shape except for the first dimension (the number of rows).

Summary

In NumPy vstack is one of the built-in stacking functions. The operation takes place row-wise, which is why you must make sure the input arrays have the same number of columns. Keep this important requirement in mind when using this function.

Leave a Reply

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