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 can I use Python and Numpy to parse XML data?
- How do I download a Python Scipy .whl file?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I use the scipy ttest_ind function in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python and SciPy to read and write WAV files?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use scipy.optimize.curve_fit in Python?
See more codes...