Yandex

Course IndexCourse Index0

    ProgramGuru

    Boolean and Fancy Indexing in NumPy


    Introduction

    One of the most powerful features of NumPy is its ability to access and manipulate data using Boolean and Fancy Indexing. These techniques let you extract, modify, or filter data in highly flexible ways — all while writing less code.

    Boolean Indexing: Select with Conditions

    Boolean indexing allows you to use True/False conditions to filter data from arrays. Think of it as asking NumPy to show you only the data you're interested in.

    Example: Filter Even Numbers

    import numpy as np
    
    arr = np.array([1, 2, 3, 4, 5, 6])
    even_mask = arr % 2 == 0
    print("Mask:", even_mask)
    print("Filtered:", arr[even_mask])
    Mask: [False  True False  True False  True]
    Filtered: [2 4 6]

    Explanation:

    The expression arr % 2 == 0 checks each element of the array. The result is a Boolean array. When we pass this Boolean array as an index, only the True values get returned.

    Boolean Indexing with Multiple Conditions

    You can use logical operators to stack more complex filters.

    Example: Filter Numbers Between 2 and 5

    arr = np.array([1, 2, 3, 4, 5, 6])
    filtered = arr[(arr > 2) & (arr < 6)]
    print(filtered)
    [3 4 5]

    Note:

    Use & (and), | (or), and wrap each condition in parentheses. Python’s logical operators (and, or) don’t work with NumPy arrays.

    Fancy Indexing: Access Using Array of Indices

    Fancy indexing means using an array (or list) of indices to pick specific elements, even in non-contiguous order.

    Example: Pick Specific Indices

    arr = np.array([10, 20, 30, 40, 50])
    indices = [0, 3, 4]
    print(arr[indices])
    [10 40 50]

    Explanation:

    Instead of slicing, we directly tell NumPy which elements we want: the 0th, 3rd, and 4th elements.

    Fancy Indexing with 2D Arrays

    Fancy indexing becomes even more useful with multidimensional arrays.

    Example: Fetch Multiple Elements from a 2D Array

    arr2d = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
    rows = [0, 1, 2]
    cols = [2, 1, 0]
    print(arr2d[rows, cols])
    [30 50 70]

    Explanation:

    This is element-wise mapping: you get (0,2), (1,1), and (2,0).

    Checkpoints and Best Practices

    • Always verify the shape of the Boolean mask matches the original array.
    • Use .shape or .ndim to debug dimensionality mismatches.
    • For fancy indexing with 2D arrays, ensure row and column index arrays have the same shape.
    • Avoid mixing Boolean and fancy indexing in one expression unless you fully understand the outcome.

    Common Mistakes to Avoid

    • Using and/or instead of &/|.
    • Forgetting parentheses in compound conditions like (arr > 1) & (arr < 5).
    • Passing index arrays of unequal shape in multi-dimensional fancy indexing.

    Wrap-Up

    Boolean and Fancy Indexing empower you to write clear, concise, and powerful data filtering logic in NumPy. These tools are essential for data cleaning, machine learning preprocessing, and scientific computing.

    Try combining both techniques with real-world datasets to get hands-on mastery.



    Welcome to ProgramGuru

    Sign up to start your journey with us

    Support ProgramGuru.org

    You can support this website with a contribution of your choice.

    When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

    PayPal

    UPI

    PhonePe QR

    MALLIKARJUNA M