๐ Introduction
When working with datasets, you often need to:
- Combine multiple arrays
- Split arrays into parts
With NumPy, this becomes very easy using stacking and splitting functions.
๐ Why Use Stacking & Splitting?
They help you:
- Merge datasets
- Divide data for training/testing
- Organize data efficiently
๐งช Example Arrays
import numpy as npa = np.array([1, 2, 3])
b = np.array([4, 5, 6])
โก๏ธ 1. Horizontal Stack (hstack())
print(np.hstack((a, b)))
๐ Output:
[1 2 3 4 5 6]
โ Combines arrays side by side
โฌ๏ธ 2. Vertical Stack (vstack())
print(np.vstack((a, b)))
๐ Output:
[[1 2 3]
[4 5 6]]
โ Combines arrays row-wise
๐ 3. Column Stack (column_stack())
print(np.column_stack((a, b)))
๐ Output:
[[1 4]
[2 5]
[3 6]]
โ๏ธ 4. Splitting Arrays (split())
arr = np.array([1, 2, 3, 4, 5, 6])print(np.split(arr, 3))
๐ Output:
[array([1, 2]), array([3, 4]), array([5, 6])]
โ Splits array into equal parts
โ ๏ธ Important Rule
Array must be evenly divisible when using split().
๐ Otherwise โ error โ
๐ 5. Split 2D Arrays
arr = np.array([[1,2],[3,4],[5,6]])print(np.vsplit(arr, 3))
โก Why Use NumPy?
Using NumPy:
- Fast operations
- Easy data manipulation
- Supports large datasets
๐ฆ Real-World Use Case
# Combine features
feature1 = np.array([1,2,3])
feature2 = np.array([4,5,6])data = np.column_stack((feature1, feature2))
print(data)
โ Used in:
- Machine learning datasets
- Data preprocessing
๐ Works with Other Libraries
Stacking & splitting are used in:
- Pandas
- Scikit-learn
- TensorFlow
๐ Summary Table
| Function | Description |
|---|---|
| hstack() | Horizontal combine |
| vstack() | Vertical combine |
| column_stack() | Column-wise combine |
| split() | Split array |
๐ง Pro Tips
- Use
column_stack()for features - Use
split()carefully (equal parts only) - Combine stacking with reshaping
๐ Conclusion
Stacking and splitting in NumPy make data handling flexible and efficient.