python-scipyHow do I use the numpy vstack function in Python?
np.vstack
is a function in the NumPy library used to stack arrays in sequence vertically (row wise). It takes a sequence of arrays of the same shape as arguments and returns a single array that is a concatenation of all of the input arrays.
Example
import numpy as np
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
c = np.vstack((a,b))
print(c)
Output example
[[1 2 3]
[2 3 4]]
The code above uses the np.vstack
function to stack two arrays a
and b
vertically. The output is a single array c
that is the concatenation of a
and b
.
The parts of the code are as follows:
-
import numpy as np
: This imports the NumPy library asnp
, which provides access to thevstack
function. -
a = np.array([1, 2, 3])
: This creates an arraya
with elements1, 2, 3
. -
b = np.array([2, 3, 4])
: This creates an arrayb
with elements2, 3, 4
. -
c = np.vstack((a,b))
: This uses thenp.vstack
function to stacka
andb
vertically. -
print(c)
: This prints the output of thenp.vstack
function.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I create a numpy array of zeros using Python?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Python Scipy to perform a Z test?
- How can I use Python and Numpy to parse XML data?
- How do I create a zero matrix using Python and Numpy?
- How do I create a numpy array of zeros using Python?
- How to use Python, XML-RPC, and NumPy together?
- How do I calculate a Jacobian matrix using Python and NumPy?
See more codes...