NumPy Splitting Array
✂️ NumPy Splitting Arrays (Complete Guide)
Splitting arrays is the opposite of joining arrays — it divides one array into multiple smaller sub-arrays.
NumPy provides these main split methods:
✔ array_split()
✔ split()
✔ hsplit()
✔ vsplit()
✔ dsplit()
✅ 1. np.array_split() (Most Flexible)
You can split an array into equal or unequal pieces.
Output:
💡 If the array can’t be divided equally, it will automatically adjust.
❌ 2. np.split() (Strict Split)
split() only works if the array is split equally.
Output:
But this is ❌ invalid:
✅ 3. Splitting 2D Arrays
Split along Columns (axis=1)
🚀 Horizontal Split — hsplit()
Splits array column-wise.
Output:
🚀 Vertical Split — vsplit()
Splits array row-wise.
Output:
🚀 Depth Split — dsplit()
For 3D arrays.
📌 Summary Table
| Method | Works If Equal? | Supports 1D | 2D | 3D |
|---|---|---|---|---|
array_split() |
❌ No | ✔ | ✔ | ✔ |
split() |
✔ Yes | ✔ | ✔ | ✔ |
hsplit() |
✔ Yes | ✔ | ✔ | ❌ |
vsplit() |
✔ Yes | ❌ | ✔ | ❌ |
dsplit() |
✔ Yes | ❌ | ❌ | ✔ |
🎯 Example Challenge
Split this array into 4 parts:
