Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Indexing & Slicing – Access Array Elements Easily in Python

🐍 NumPy – Indexing & Slicing

In NumPy, indexing and slicing are essential techniques used to access, modify, and extract specific elements or sections of an array.

These operations are widely used in:

  • Data analysis
  • Machine learning
  • Image processing
  • Scientific computing
  • Feature engineering

Understanding indexing and slicing allows you to efficiently work with large datasets without writing complex loops.


What is Indexing in NumPy?

Indexing means accessing a single element from an array using its position.

NumPy uses zero-based indexing, meaning the first element is at index 0.


Example: 1D Indexing

import numpy as np

arr = np.array([10, 20, 30, 40, 50])

print(arr[0])
print(arr[3])

Output:

10
40

Negative Indexing

NumPy supports negative indexing to access elements from the end.

IndexMeaning
-1Last element
-2Second last

Example

arr = np.array([10, 20, 30, 40, 50])

print(arr[-1])
print(arr[-2])

Output:

50
40

What is Slicing?

Slicing means extracting a portion of an array.

Syntax

array[start:stop:step]

Where:

  • start → starting index (included)
  • stop → ending index (excluded)
  • step → interval between elements

Basic Slicing (1D Array)

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]

Omitting Values in Slicing

Start omitted

print(arr[:3])

Output:

[10 20 30]

End omitted

print(arr[2:])

Output:

[30 40 50 60]

Both omitted

print(arr[:])

Output:

[10 20 30 40 50 60]

2D Array Indexing

In a 2D array, elements are accessed using:

array[row, column]

Example

arr = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

print(arr[0, 1])
print(arr[2, 2])

Output:

2
9

2D Array Slicing

Extract rows

print(arr[0:2])

Output:

[[1 2 3]
 [4 5 6]]

Extract columns

print(arr[:, 1])

Output:

[2 5 8]

Extract submatrix

print(arr[0:2, 1:3])

Output:

[[2 3]
 [5 6]]

Negative Indexing in 2D Arrays

print(arr[-1, -1])

Output:

9

3D Array Indexing

arr = np.array([
    [[1, 2], [3, 4]],
    [[5, 6], [7, 8]]
])

print(arr[0, 1, 1])

Output:

4

3D Slicing Example

print(arr[:, :, 0])

Output:

[[1 3]
 [5 7]]

Boolean Indexing

Boolean indexing filters data based on conditions.

arr = np.array([10, 20, 30, 40, 50])

print(arr[arr > 25])

Output:

[30 40 50]

Fancy Indexing

Select multiple specific elements.

arr = np.array([10, 20, 30, 40, 50])

print(arr[[0, 2, 4]])

Output:

[10 30 50]

Modifying Values Using Indexing

arr = np.array([1, 2, 3, 4])

arr[1] = 99

print(arr)

Output:

[ 1 99  3  4]

Modifying Slices

arr[1:3] = 100

print(arr)

Output:

[  1 100 100   4]

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]]

Key Concepts Summary

ConceptDescription
Indexing              Access single element
Slicing              Extract sub-array
Negative Indexing              Access from end
Boolean Indexing              Filter data
Fancy Indexing              Select multiple elements

Best Practices

  • Always understand array shape before slicing
  • Use vectorized slicing instead of loops
  • Prefer boolean indexing for filtering
  • Avoid unnecessary copying of large arrays
  • Use colon (:) for full dimension selection

Common Errors

Index out of range

IndexError: index 5 is out of bounds

✔ Fix by checking array size using .shape


Summary

NumPy indexing and slicing provide powerful tools to access and manipulate array data efficiently. These techniques are essential for:

  • Data preprocessing
  • Machine learning pipelines
  • Image processing
  • Scientific computing

Mastering indexing and slicing will significantly improve your ability to work with NumPy arrays.


Conclusion

Indexing and slicing are core NumPy skills that allow you to efficiently extract and manipulate data from arrays. With these tools, you can handle everything from simple element access to complex multidimensional data operations in a clean and optimized way.




Post a Comment

0 Comments