Showing posts with label vstack. Show all posts
Showing posts with label vstack. Show all posts

2/18/2023

How to Vertically Stack Multiple Arrays Using numpy.vstack in Python

 refer to code:


.

import numpy as np

# Example arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([7, 8, 9])

# Create a list of arrays
array_list = [a, b, c]

# Vertically stack all arrays in the list
result = np.empty((0, a.shape[0]))
for arr in array_list:
result = np.vstack((result, arr))

# Print the vertically stacked array
print(result)

..



Thank you.

πŸ™‡πŸ»‍♂️

www.marearts.com

3/19/2019

Python hstack, vstack example code

import numpy as np

#hstack #1
a = np.array((1,2,3))
b = np.array((2,3,4))
print(np.hstack((a,b)))
>
 [1 2 3 2 3 4]

#hstack #2
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
print(np.hstack((a,b)))
>
 [[1 2]
 [2 3]
 [3 4]]

#vstack #1
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
print(np.vstack((a,b)))
>
 [[1 2 3]
 [2 3 4]]


#vstack #2
a = np.array([[1], [2], [3]])
b = np.array([[2], [3], [4]])
print(np.vstack((a,b)))
>
[[1]
 [2]
 [3]
 [2]
 [3]
 [4]]