2024-05-28 10:45:12 +00:00
# Concatenation of Arrays
Concatenation of arrays in NumPy refers to combining multiple arrays into a single array, either along existing axes or by adding new axes. NumPy provides several functions for this purpose.
# Functions of Concatenation
## np.concatenate
Joins two or more arrays along an existing axis.
### Syntax
```python
numpy.concatenate((arr1, arr2, ...), axis)
```
Args:
- arr1, arr2, ...: Sequence of arrays to concatenate.
- axis: Axis along which the arrays will be joined. Default is 0.
### Example
2024-05-31 00:37:45 +00:00
#### Concatenate along axis 0
2024-05-28 10:45:12 +00:00
```python
import numpy as np
#creating 2 arrays
arr1 = np.array([1 2 3],[7 8 9])
arr2 = np.array([4 5 6],[10 11 12])
result_1 = np.concatenate((arr1, arr2), axis=0)
print(result_1)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[ 1 2 3]
[ 7 8 9]
[ 4 5 6]
2024-05-31 00:37:45 +00:00
[10 11 12]]
```
2024-05-28 10:45:12 +00:00
2024-05-31 00:37:45 +00:00
#### Concatenate along axis 1
```python
2024-05-28 10:45:12 +00:00
result_2 = np.concatenate((arr1, arr2), axis=1)
print(result_2)
2024-05-31 00:37:45 +00:00
```
2024-05-28 10:45:12 +00:00
2024-05-31 00:37:45 +00:00
#### Output
2024-05-28 10:45:12 +00:00
```
2024-05-31 00:37:45 +00:00
[[ 1 2 3 4 5 6 ]
[ 7 8 9 10 11 12]]
```
2024-05-28 10:45:12 +00:00
## np.vstack
Vertical stacking of arrays (row-wise).
### Syntax
```python
numpy.vstack(arrays)
```
Args:
- arrays: Sequence of arrays to stack.
### Example
2024-05-31 00:37:45 +00:00
2024-05-28 10:45:12 +00:00
```python
import numpy as np
#create arrays
arr1= np.array([1 2 3], [7 8 9])
arr2 = np.array([4 5 6],[10 11 12])
result = np.vstack((arr1, arr2))
print(result)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[ 1 2 3]
[ 7 8 9]
[ 4 5 6]
2024-05-31 00:37:45 +00:00
[10 11 12]]
2024-05-28 10:45:12 +00:00
```
2024-05-31 00:37:45 +00:00
2024-05-28 10:45:12 +00:00
## 3. np.hstack
Stacks arrays horizontally (column-wise).
### Syntax
```python
numpy.hstack(arrays)
```
Args:
- arrays: Sequence of arrays to stack.
### Example
```python
import numpy as np
#create arrays
arr1= np.array([1 2 3], [7 8 9])
arr2 = np.array([4 5 6],[10 11 12])
result = np.hstack((arr1, arr2))
print(result)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[ 1 2 3] [ 4 5 6]
2024-05-31 00:37:45 +00:00
[ 7 8 9] [10 11 12]]
2024-05-28 10:45:12 +00:00
```
## np.dstack
Stacks arrays along the third axis (depth-wise).
### Syntax
```python
numpy.dstack(arrays)
```
- arrays: Sequence of arrays to stack.
### Example
```python
import numpy as np
#create arrays
arr1= np.array([1 2 3], [7 8 9])
arr2 = np.array([4 5 6],[10 11 12])
result = np.dstack((arr1, arr2))
print(result)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[ 7 10]
[ 8 11]
2024-05-31 00:37:45 +00:00
[ 9 12]]]
2024-05-28 10:45:12 +00:00
```
## np.stack
Joins a sequence of arrays along a new axis.
```python
numpy.stack(arrays, axis)
```
Args:
- arrays: Sequence of arrays to stack.
### Example
```python
import numpy as np
#create arrays
arr1= np.array([1 2 3], [7 8 9])
arr2 = np.array([4 5 6],[10 11 12])
result = np.stack((arr1, arr2), axis=0)
print(result)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[[ 1 2 3]
[ 7 8 9]]
[[ 4 5 6]
2024-05-31 00:37:45 +00:00
[10 11 12]]]
2024-05-28 10:45:12 +00:00
```
# Concatenation with Mixed Dimensions
When concatenating arrays with different shapes, it's often necessary to reshape them to have compatible dimensions.
## Example
2024-05-31 00:37:45 +00:00
#### Concatenate along axis 0
2024-05-28 10:45:12 +00:00
```python
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([7, 8, 9])
result_0= np.concatenate((arr1, arr2[np.newaxis, :]), axis=0)
print(result_0)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[1 2 3]
[4 5 6]
2024-05-31 00:37:45 +00:00
[7 8 9]]
```
#### Concatenate along axis 1
2024-05-28 10:45:12 +00:00
2024-05-31 00:37:45 +00:00
```python
2024-05-28 10:45:12 +00:00
result_1 = np.concatenate((arr1, arr2[:, np.newaxis]), axis=1)
print(result_1)
2024-05-31 00:37:45 +00:00
```
#### Output
```
2024-05-28 10:45:12 +00:00
[[1 2 3 7]
2024-05-31 00:37:45 +00:00
[4 5 6 8]]
2024-05-28 10:45:12 +00:00
```