🐍 NumPy – Indexing
Indexing is one of the most important concepts in NumPy. It allows you to access individual elements from arrays quickly and efficiently.
NumPy uses zero-based indexing, meaning the first element starts at index 0.
Indexing is widely used in:
- Data analysis
- Machine learning
- Image processing
- Scientific computing
- Feature extraction
Understanding indexing is essential before learning slicing, filtering, and advanced array operations.
What is Indexing?
Indexing means accessing a specific element in an array using its position.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[0])
print(arr[3])Output:
10
40🔵 Positive Indexing
Positive indexing starts from the beginning (left to right).
| Index | Value |
|---|---|
| 0 | First element |
| 1 | Second element |
| 2 | Third element |
Example
arr = np.array([5, 10, 15, 20])
print(arr[1])
print(arr[2])Output:
10
15🔴 Negative Indexing
Negative indexing starts from the end of the array.
| Index | Value |
| -1 | Last element |
| -2 | Second last |
Example
arr = np.array([10, 20, 30, 40, 50])
print(arr[-1])
print(arr[-2])Output:
50
40🟡 Indexing in 2D Arrays
In a 2D array, indexing is done using:
array[row_index, column_index]Example
arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(arr[0, 1])
print(arr[2, 2])Output:
2
9🟢 Accessing Entire Row
print(arr[1])Output:
[4 5 6]✔ Returns the full row
🟣 Accessing Entire Column
print(arr[:, 1])Output:
[2 5 8]✔ : means all rows
✔ 1 means second column
🔵 Indexing in 3D Arrays
3D arrays represent multiple layers of data.
array[layer, row, column]Example
arr = np.array([
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
])
print(arr[0, 1, 1])Output:
4🟠 Modifying Elements Using Indexing
Indexing is not only for reading values—you can also modify data.
arr = np.array([1, 2, 3, 4])
arr[2] = 99
print(arr)Output:
[ 1 2 99 4]🔶 Indexing Multiple Elements (Fancy Indexing)
You can access multiple elements at once.
arr = np.array([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])Output:
[10 30 50]🟤 Boolean Indexing
Boolean indexing allows filtering based on conditions.
arr = np.array([10, 15, 20, 25, 30])
print(arr[arr > 20])Output:
[25 30]⚡ Real-World Example
Extracting pixel values (Image data)
import numpy as np
image = np.array([
[255, 128, 64],
[100, 150, 200],
[50, 75, 90]
])
print(image[1, 2])Output:
200✔ Common in image processing and computer vision
📊 Indexing Summary Table
| Type | Description |
| Positive Indexing | Access from start |
| Negative Indexing | Access from end |
| 2D Indexing | row, column access |
| 3D Indexing | layer, row, column |
| Fancy Indexing | multiple elements |
| Boolean Indexing | condition-based filtering |
🧠 Best Practices
- Always check array shape using
.shape - Use negative indexing for last elements
- Prefer boolean indexing for filtering data
- Avoid out-of-range index errors
- Combine indexing with slicing for advanced operations
🚫 Common Errors
Index Out of Range
IndexError: index 10 is out of bounds✔ Fix: Ensure index is within array size
🏁 Summary
NumPy indexing allows you to efficiently access and modify elements in arrays. It supports:
- 1D indexing
- 2D matrix indexing
- 3D tensor indexing
- Negative indexing
- Boolean filtering
- Fancy indexing
🚀 Conclusion
Indexing is the foundation of working with NumPy arrays. Mastering it allows you to manipulate data efficiently and is essential for all advanced topics like slicing, filtering, and machine learning preprocessing.


0 Comments