O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Fancy indexing in place for ufuncs with the at() method

The at() method was added to the NumPy universal function class in NumPy 1.8. This method allows fancy indexing in-place. Fancy indexing is indexing that does not involve integers or slices, which is normal indexing. "In-place" means that the data of the input array will be altered.

The signature for the at() method is ufunc.at(a, indices[, b]). The indices array corresponds to the elements to operate on. We must specify the b array only for universal functions with two operands.

How to do it...

The following steps demonstrate how the at() method works:

  1. Create an array with 7 random integers from -4 to 4 with a seed of 44:
    np.random.seed(44)
    a = np.random.random_integers(-4, 4, 7)
    print(a)

    The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required