🐍 NumPy – Slicing
Slicing in NumPy is a powerful technique used to extract a portion of an array without copying or rewriting data manually.
It allows you to work with large datasets efficiently and is widely used in:
- Data analysis
- Machine learning
- Image processing
- Signal processing
- Scientific computing
Slicing is one of the most important skills in NumPy after indexing.
What is Slicing?
Slicing means selecting a range of elements from an array.
Instead of accessing one element at a time, slicing lets you extract multiple elements at once.
Syntax
array[start:stop:step]Meaning:
- start → starting index (included)
- stop → ending index (excluded)
- step → interval between elements
🔵 Basic Slicing in 1D Arrays
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60])
print(arr[1:4])Output:
[20 30 40]🟢 Slicing with Step Value
print(arr[0:6:2])Output:
[10 30 50]✔ Every 2nd element is selected
🟡 Omitting Start and Stop
Start omitted
print(arr[:3])Output:
[10 20 30]Stop omitted
print(arr[2:])Output:
[30 40 50 60]Both omitted
print(arr[:])Output:
[10 20 30 40 50 60]🔴 Negative Slicing
Negative slicing works from the end of the array.
print(arr[-4:-1])Output:
[30 40 50]🟣 Slicing in 2D Arrays
In 2D arrays, slicing works on rows and columns.
Syntax
array[row_start:row_stop, col_start:col_stop]Example
arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(arr[0:2, 1:3])Output:
[[2 3]
[5 6]]🟠 Extracting Rows
print(arr[1:3])Output:
[[4 5 6]
[7 8 9]]🔵 Extracting Columns
print(arr[:, 1])Output:
[2 5 8]✔ : means all rows
🟢 Step Slicing in 2D
print(arr[::2, ::2])Output:
[[1 3]
[7 9]]🔶 Slicing in 3D Arrays
3D slicing is used in image and tensor processing.
Example
arr = np.array([
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
])
print(arr[:, :, 0])Output:
[[1 3]
[5 7]]🧠 Understanding Slice Behavior
Slicing in NumPy:
✔ Does NOT copy data (usually returns view)
✔ Is memory efficient
✔ Works across all dimensions
✔ Is faster than loops
⚡ Real-World Example
Image Cropping using slicing
import numpy as np
image = np.array([
[10, 20, 30, 40],
[50, 60, 70, 80],
[90, 100, 110, 120]
])
crop = image[0:2, 1:3]
print(crop)Output:
[[20 30]
[60 70]]✔ Common in computer vision tasks
📊 Slicing Summary Table
| Type | Example | Description |
|---|---|---|
| Basic slice | arr[1:4] | Range selection |
| Step slice | arr[::2] | Skip elements |
| Negative slice | arr[-3:] | From end |
| 2D slice | arr[0:2,1:3] | Matrix section |
| Column slice | arr[:,1] | Column access |
| Row slice | arr[1,:] | Row access |
🧠 Best Practices
- Always understand array shape before slicing
- Use
:for full dimension access - Prefer slicing over loops
- Use step slicing for performance optimization
- Combine slicing with indexing for flexibility
🚫 Common Mistakes
Out of range slice
IndexError: index out of bounds✔ Fix: check .shape
Confusing row/column order
Remember:
arr[row, column]🏁 Summary
NumPy slicing allows you to extract subarrays efficiently using:
- start:stop syntax
- 1D, 2D, and 3D slicing
- row and column extraction
- negative and step slicing
🚀 Conclusion
Slicing is one of the most powerful features in NumPy. It enables fast, memory-efficient access to data without copying arrays unnecessarily.
Mastering slicing is essential for:
- Data science
- Machine learning
- Image processing
- Scientific computing


0 Comments