We use np.concatenate() to join two or more arrays into one. It helps combine data from different sources into a single array.
np.concatenate() for joining arrays in NumPy
import numpy as np # Define arrays to join array1 = np.array([...]) array2 = np.array([...]) # Join arrays along an axis joined_array = np.concatenate((array1, array2), axis=0)
The arrays must have the same shape except in the dimension you join on.
The axis parameter decides if you join rows (axis=0) or columns (axis=1).
import numpy as np # Example 1: Join 1D arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5]) result = np.concatenate((array1, array2)) print(result)
import numpy as np # Example 2: Join 2D arrays along rows (axis=0) array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6]]) result = np.concatenate((array1, array2), axis=0) print(result)
import numpy as np # Example 3: Join 2D arrays along columns (axis=1) array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5], [6]]) result = np.concatenate((array1, array2), axis=1) print(result)
import numpy as np # Example 4: Edge case - empty array array1 = np.array([1, 2, 3]) array2 = np.array([]) result = np.concatenate((array1, array2)) print(result)
This program creates two arrays and joins them by adding rows. It prints the arrays before and after joining so you can see the change.
import numpy as np # Create two 2D arrays array1 = np.array([[10, 20], [30, 40]]) array2 = np.array([[50, 60]]) print("Array 1 before joining:") print(array1) print("Array 2 before joining:") print(array2) # Join arrays along rows (axis=0) joined_array = np.concatenate((array1, array2), axis=0) print("\nJoined array after np.concatenate along axis=0:") print(joined_array)
Time complexity: Joining arrays takes time proportional to the total size of the output array.
Space complexity: A new array is created, so space grows with the combined size.
Common mistake: Trying to join arrays with mismatched shapes on non-joining axes causes errors.
Use np.concatenate() when you want to join arrays along an existing axis. For adding new dimensions, consider np.stack().
np.concatenate() joins arrays along a chosen axis.
Arrays must match in shape except on the joining axis.
It creates a new combined array without changing the originals.
Practice
np.concatenate() do in NumPy?Solution
Step 1: Understand the purpose of np.concatenate()
It is used to join arrays along a specified axis, combining them into one larger array.Step 2: Compare with other array functions
Splitting, sorting, or creating zero arrays are different functions like np.split(), np.sort(), and np.zeros().Final Answer:
Joins two or more arrays along an existing axis -> Option BQuick Check:
np.concatenate() = join arrays [OK]
- Confusing concatenate with split or sort functions
- Thinking it modifies original arrays
- Assuming it creates arrays from scratch
a and b using np.concatenate()?Solution
Step 1: Check the function signature
np.concatenate() expects a single argument: a sequence (like a list or tuple) of arrays to join.Step 2: Analyze each option
np.concatenate([a, b]) correctly passes a list of arrays [a, b]. np.concatenate(a, b) passes two separate arguments, which is invalid. np.concatenate(a + b) tries to add arrays before concatenation, which is not the function's purpose. np.concatenate(a, axis=1) passes axis=1 for 1D arrays, which is invalid.Final Answer:
np.concatenate([a, b]) -> Option AQuick Check:
Pass arrays as a list to concatenate() [OK]
- Passing arrays as separate arguments instead of a list
- Using plus (+) operator instead of concatenate
- Setting wrong axis for 1D arrays
import numpy as np x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6]]) result = np.concatenate((x, y), axis=0) print(result)
Solution
Step 1: Check shapes of arrays
x has shape (2, 2), y has shape (1, 2). They match in columns (axis=1) but differ in rows (axis=0).Step 2: Concatenate along axis=0 (rows)
Joining along rows stacks y below x, resulting in a (3, 2) array with rows from x then y.Final Answer:
[[1 2] [3 4] [5 6]] -> Option AQuick Check:
Concatenate rows stacks arrays vertically [OK]
- Confusing axis=0 and axis=1
- Expecting horizontal stacking with axis=0
- Ignoring shape compatibility
import numpy as np x = np.array([[1, 2], [3, 4]]) y = np.array([5, 6, 7]) result = np.concatenate((x, y), axis=0) print(result)
Solution
Step 1: Check shapes of arrays
x shape is (2, 2), y shape is (3,). They differ in both dimensions.Step 2: Understand concatenate axis rules
Arrays must match in all dimensions except the concatenation axis. Here, axis=0 means rows, but y has no matching columns.Final Answer:
Error because arrays have incompatible shapes along axis=0 -> Option DQuick Check:
Shapes must match except on concat axis [OK]
- Ignoring shape mismatch errors
- Assuming 1D array can concatenate with 2D without reshape
- Misunderstanding axis parameter
a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) c = np.array([[7, 8]])
You want to join them into one array with shape (4, 2). Which code correctly does this?
Solution
Step 1: Check shapes and desired output
a shape is (2, 2), b and c are (1, 2). Joining along rows (axis=0) stacks them vertically to get (4, 2).Step 2: Analyze options
np.concatenate((a, b, c), axis=0) correctly concatenates all three arrays along axis=0. np.concatenate([a, b, c], axis=1) tries axis=1 which would require matching rows, which is false. np.concatenate((a, b), axis=0) + c adds arrays after concatenation, which is invalid. np.concatenate((a, b, c), axis=2) uses axis=2 which is invalid for 2D arrays.Final Answer:
np.concatenate((a, b, c), axis=0) -> Option CQuick Check:
Stack arrays vertically with axis=0 [OK]
- Using wrong axis for concatenation
- Trying to add arrays instead of concatenate
- Using invalid axis number for 2D arrays
Ready to prove it?
Pass the test and this pattern moves unknown
