Gavin Wiggins

AboutNotesPhotos


Boolean indexing

Written on November 7, 2022

Use boolean indexing in NumPy arrays to assign values. The example below assigns a zero to array items that are less than zero.

>>> import numpy as np
>>> nums = np.array([-1, 3, 6, -0.2, -4])
>>> nums[nums < 0] = 0
>>> nums
array([0., 3., 6., 0., 0.])

Gavin Wiggins © 2024.
Made on a Mac with Genja. Hosted on GitHub Pages.