Build a row-wise data matrix
Combine equal-length feature or measurement vectors into a matrix whose rows represent observations.
NumPy · Function
Stack arrays vertically by concatenating along axis 0. One-dimensional inputs are first treated as single rows, producing an output with at least two dimensions.
np.vstack combines arrays along the first axis. Before combining, each one-dimensional input is treated as a single row, so the result is at least two-dimensional; higher-dimensional inputs must agree in every non-first dimension.
numpy.vstack(tup, *, dtype=None, casting='same_kind')| Name | Type | Description |
|---|---|---|
tup | sequence of ndarrays | Sequence of ndarrays to stack; inputs must have compatible shapes. |
dtype | dtype, optional | Output data type, if specified. |
casting | {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional | Casting rule to apply when converting inputs to the requested output type. |
stacked: ndarray — the array formed by stacking the inputs; it is at least two-dimensional.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.vstack((a, b))
print(result)
This converts two one-dimensional arrays into rows and combines them into one two-dimensional array.
Combine equal-length feature or measurement vectors into a matrix whose rows represent observations.
Append compatible batches along their leading dimension while retaining the remaining dimensions.
Turn several one-dimensional sequences into a two-dimensional array for downstream ndarray operations.
It stacks arrays along the first axis, treating each one-dimensional input as shape (1, N) first. Consequently, one-dimensional inputs become rows and the output has at least two dimensions.
Follow-up: How does it differ from np.stack? np.vstack concatenates along the first axis after normalizing one-dimensional inputs, whereas np.stack creates a new axis.
The input is a sequence of ndarrays. Arrays must match in all dimensions except the first; one-dimensional arrays must have equal lengths.
Follow-up: What happens if the non-first dimensions do not match? The operation cannot form a valid concatenation and raises a shape-related error.
The function accepts optional dtype and casting keyword arguments in the current stable signature. dtype requests the result type, while casting controls permitted conversions between input types.
Follow-up: When would casting='same_kind' reject an operation? It can reject conversions considered unsafe under the same-kind casting rule.
numpy.concatenatenumpy.stacknumpy.block