NumPy vstack Function: A Step-by-Step Guide with Examples

 

NumPy vstack step by step examples

NumPy, short for Numerical Python, is a powerful library in the Python ecosystem that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these elements. One of the essential functions in NumPy is vstack(), which is used to vertically stack arrays. In this blog post, we'll take a closer look at the vstack() function and walk through step-by-step examples to illustrate its usage.


$ads={1}

 

Understanding NumPy vstack()



The vstack() function in NumPy is designed to vertically stack arrays in a sequence. It takes a sequence of arrays as input and returns a single array by vertically stacking them. This can be particularly useful when you want to combine datasets vertically, adding rows to an existing array.


The basic syntax of numpy vstack() is as follows:



numpy.vstack(tup)


Here, tup is the sequence of arrays to be stacked. The arrays must have the same number of columns, as the function vertically concatenates them.


Read also:
 

 

Step-by-Step Examples



Example 1: Vertical Stack of Two Arrays



Let's start with a simple example of stacking two arrays vertically:



import numpy as np

# Creating two arrays
array1 = np.array([[1, 2, 3],
 [4, 5, 6]])

array2 = np.array([[7, 8, 9],
 [10, 11, 12]])

# Using vstack to vertically stack the arrays
result = np.vstack((array1, array2))

print("Original Array 1:")
print(array1)

print("\nOriginal Array 2:")
print(array2)

print("\nStacked Result:")
print(result)  
  


In this example, array1 and array2 are vertically stacked using numpy vstack(), resulting in a new array with twice the number of rows.


Output:




Original Array 1:
[[1 2 3]
 [4 5 6]]

Original Array 2:
[[ 7  8  9]
 [10 11 12]]

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


Example 2: Vertical Stack of Multiple Arrays



You can use numpy vstack() to combine more than two arrays. Let's see an example with three arrays:



import numpy as np

# Creating three arrays
array1 = np.array([[1, 2, 3],
 [4, 5, 6]])

array2 = np.array([[7, 8, 9],
 [10, 11, 12]])

array3 = np.array([[13, 14, 15],
 [16, 17, 18]])

# Using vstack to vertically stack the arrays
result = np.vstack((array1, array2, array3))

print("Original Array 1:")
print(array1)

print("\nOriginal Array 2:")
print(array2)

print("\nOriginal Array 3:")
print(array3)

print("\nStacked Result:")
print(result)


In this example, array1, array2, and array3 are stacked vertically to create a new array with three times the number of rows. 
 
$ads={2}


Output:




Original Array 1:
[[1 2 3]
 [4 5 6]]

Original Array 2:
[[ 7  8  9]
 [10 11 12]]

Original Array 3:
[[13 14 15]
 [16 17 18]]

Stacked Result:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]
 [13 14 15]
 [16 17 18]]



Example 3: Handling Arrays with Different Column Numbers



It's crucial to note that the arrays must have the same number of columns for vstack() to work. If the arrays have different column numbers, you'll encounter a ValueError.



import numpy as np

# Creating arrays with different column numbers
array1 = np.array([[1, 2, 3],
 [4, 5, 6]])

array2 = np.array([[7, 8],
 [10, 11]])

try:
 # Attempting to vertically stack arrays with different column numbers
 result = np.vstack((array1, array2))
except ValueError as e:
 print(f"ValueError: {e}")
  
  


In this example, array1 has three columns, while array2 has only two. The attempt to vertically stack them results in a ValueError.



ERROR!
ValueError: all the input array dimensions except 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


Conclusion



NumPy's vstack() function is a powerful tool for vertically stacking arrays in a seamless manner. Whether you need to concatenate two arrays or combine multiple arrays vertically, vstack() simplifies the process. Understanding its usage and limitations is crucial for effectively leveraging this function in your data manipulation tasks. Armed with this knowledge, you can confidently integrate vstack() into your NumPy workflow for efficient array manipulation.


Previous Post Next Post