Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Joining NumPy Array
Next article icon

numpy.vstack() is a function in NumPy used to stack arrays vertically (row-wise). It takes a sequence of arrays as input and returns a single array by stacking them along the vertical axis (axis 0).

Example: Vertical Stacking of 1D Arrays Using numpy.vstack()

Python
importnumpyasgeeka=geek.array([1,2,3])print("1st Input array :\n",a)b=geek.array([4,5,6])print("2nd Input array :\n",b)res=geek.vstack((a,b))print("Output vertically stacked array:\n ",res)

Output
1st Input array :  [1 2 3]2nd Input array :  [4 5 6]Output vertically stacked array:  [[1 2 3] [4 5 6]]

The two 1D arrays a and b are stacked vertically usingnp.vstack(), combining them into a 2D array where each input array forms a row.

Syntax

numpy.vstack(tup)

Parameters:

  • tup:[sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the first axis.

Return:[stacked ndarray] The stacked array of the input arrays.

Vertical Stacking of 2D Arrays Using numpy.vstack()

This code shows how to vertically stack two 2D arrays using numpy.vstack() resulting in a combined 2D array.

Python
importnumpyasgeeka=geek.array([[1,2,3],[-1,-2,-3]])print("1st Input array :\n",a)b=geek.array([[4,5,6],[-4,-5,-6]])print("2nd Input array :\n",b)res=geek.vstack((a,b))print("Output stacked array :\n ",res)

Output
1st Input array :  [[ 1  2  3] [-1 -2 -3]]2nd Input array :  [[ 4  5  6] [-4 -5 -6]]Output stacked array :  [[ 1  2  3] [-1 -2 -3] [ 4  5  6] [-4 -5 -6]]

Two 2D arrays a and b are vertically stacked, creating a new 2D array where each original array becomes a set of rows in the resulting array.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp