Skip to main content

concatenate

Join a sequence of arrays along an existing axis.
numpy.concatenate(arrays, axis=0, out=None, *, dtype=None, casting="same_kind")
arrays
sequence of array_like
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
axis
int
default:"0"
The axis along which the arrays will be joined. If axis is None, arrays are flattened before use.
out
ndarray
default:"None"
If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
dtype
str or dtype
default:"None"
If provided, the destination array will have this dtype. Cannot be provided together with out.
casting
{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}
default:"'same_kind'"
Controls what kind of data casting may occur.
Returns: ndarray - The concatenated array.

Examples

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])

# Concatenate along axis 0 (rows)
print(np.concatenate((a, b), axis=0))
# array([[1, 2],
#        [3, 4],
#        [5, 6]])

# Concatenate along axis 1 (columns)
b = np.array([[5], [6]])
print(np.concatenate((a, b), axis=1))
# array([[1, 2, 5],
#        [3, 4, 6]])

# Multiple arrays
a = np.array([1, 2])
b = np.array([3, 4])
c = np.array([5, 6])
print(np.concatenate((a, b, c)))
# array([1, 2, 3, 4, 5, 6])

See Also

  • stack - Join arrays along a new axis
  • vstack - Stack arrays vertically (row-wise)
  • hstack - Stack arrays horizontally (column-wise)

stack

Join a sequence of arrays along a new axis.
numpy.stack(arrays, axis=0, out=None, *, dtype=None, casting="same_kind")
arrays
sequence of array_like
Each array must have the same shape.
axis
int
default:"0"
The axis in the result array along which the input arrays are stacked.
out
ndarray
default:"None"
If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned.
dtype
str or dtype
default:"None"
If provided, the destination array will have this dtype. Cannot be provided together with out.
casting
{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}
default:"'same_kind'"
Controls what kind of data casting may occur.
Returns: ndarray - The stacked array has one more dimension than the input arrays.

Examples

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Stack along new first axis
print(np.stack((a, b)))
# array([[1, 2, 3],
#        [4, 5, 6]])

# Stack along new last axis
print(np.stack((a, b), axis=-1))
# array([[1, 4],
#        [2, 5],
#        [3, 6]])

# Stack 2D arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.stack((a, b), axis=0).shape)
# (2, 2, 2)

print(np.stack((a, b), axis=2).shape)
# (2, 2, 2)

See Also

  • concatenate - Join arrays along an existing axis
  • vstack, hstack, dstack - Stack arrays in specific directions

vstack

Stack arrays in sequence vertically (row wise).
numpy.vstack(tup, *, dtype=None, casting="same_kind")
tup
sequence of ndarrays
The arrays must have the same shape along all but the first axis. 1-D arrays must have the same length.
dtype
str or dtype
default:"None"
If provided, the destination array will have this dtype.
casting
{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}
default:"'same_kind'"
Controls what kind of data casting may occur.
Returns: ndarray - The array formed by stacking the given arrays, will be at least 2-D.

Examples

import numpy as np

# Stack 1-D arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
# array([[1, 2, 3],
#        [4, 5, 6]])

# Stack 2-D arrays
a = np.array([[1], [2], [3]])
b = np.array([[4], [5], [6]])
print(np.vstack((a, b)))
# array([[1],
#        [2],
#        [3],
#        [4],
#        [5],
#        [6]])

See Also

  • concatenate - Join arrays along an existing axis
  • stack - Join arrays along a new axis
  • hstack - Stack arrays horizontally
  • dstack - Stack arrays depth-wise

hstack

Stack arrays in sequence horizontally (column wise).
numpy.hstack(tup, *, dtype=None, casting="same_kind")
tup
sequence of ndarrays
The arrays must have the same shape along all but the second axis, except 1-D arrays which can be any length.
dtype
str or dtype
default:"None"
If provided, the destination array will have this dtype.
casting
{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}
default:"'same_kind'"
Controls what kind of data casting may occur.
Returns: ndarray - The array formed by stacking the given arrays.

Examples

import numpy as np

# Stack 1-D arrays
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
print(np.hstack((a, b)))
# array([1, 2, 3, 4, 5, 6])

# Stack 2-D arrays
a = np.array([[1], [2], [3]])
b = np.array([[4], [5], [6]])
print(np.hstack((a, b)))
# array([[1, 4],
#        [2, 5],
#        [3, 6]])

See Also

  • concatenate - Join arrays along an existing axis
  • stack - Join arrays along a new axis
  • vstack - Stack arrays vertically
  • dstack - Stack arrays depth-wise

dstack

Stack arrays in sequence depth wise (along third axis).
numpy.dstack(tup)
tup
sequence of arrays
The arrays must have the same shape along all but the third axis. 1-D or 2-D arrays must have the same shape.
Returns: ndarray - The array formed by stacking the given arrays, will be at least 3-D.

Examples

import numpy as np

# Stack 1-D arrays
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
print(np.dstack((a, b)))
# array([[[1, 4],
#         [2, 5],
#         [3, 6]]])

# Stack 2-D arrays
a = np.array([[1], [2], [3]])
b = np.array([[4], [5], [6]])
print(np.dstack((a, b)))
# array([[[1, 4]],
#        [[2, 5]],
#        [[3, 6]]])

See Also

  • concatenate - Join arrays along an existing axis
  • stack - Join arrays along a new axis
  • vstack - Stack arrays vertically
  • hstack - Stack arrays horizontally

column_stack

Stack 1-D arrays as columns into a 2-D array.
numpy.column_stack(tup)
tup
sequence of 1-D or 2-D arrays
Arrays to stack. All of them must have the same first dimension.
Returns: ndarray - The 2-D array formed by stacking the given arrays.

Examples

import numpy as np

a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
print(np.column_stack((a, b)))
# array([[1, 4],
#        [2, 5],
#        [3, 6]])

# 2-D arrays are stacked as-is
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.column_stack((a, b)))
# array([[1, 2, 5, 6],
#        [3, 4, 7, 8]])

See Also

  • hstack - Stack arrays horizontally
  • vstack - Stack arrays vertically
  • concatenate - Join arrays along an existing axis

row_stack

Stack arrays in sequence vertically (row wise).
numpy.row_stack(tup, *, dtype=None, casting="same_kind")
This is an alias for vstack. It is provided for consistency with column_stack.

See Also

  • vstack - Equivalent function
  • column_stack - Stack 1-D arrays as columns

Build docs developers (and LLMs) love