concatenate()
concatenate() joins arrays end to end along an axis you choose.
In this page:
Syntax
np.concatenate((arr1, arr2), axis=0)
concatenate()
np.concatenate takes a tuple of arrays and joins them along an existing axis. The arrays must match in every other dimension. axis=0 stacks rows, axis=1 adds columns.
Note:
axis=None flattens everything before joining.
Example: concatenate()
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))
print(np.concatenate((a, a), axis=1))
# Output:
# [[1 2]
# [3 4]
# [5 6]]
# [[1 2 1 2]
# [3 4 3 4]]
Related Topics
Common Mistakes
- Passing arrays as separate arguments instead of a tuple
- Joining arrays with mismatched shapes
- Confusing axis 0 and axis 1
Chapter Summary
- Pass a tuple of arrays
- Other dimensions must match
- axis picks the join direction
- Returns a new array
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: