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.
The following steps demonstrate how the at()
method works:
Create an array with
7
random integers from-4
to4
with a seed of44:
np.random.seed(44) a = np.random.random_integers(-4, 4, 7) print(a)
The array appears as follows:
[ 0 -1 -3 -1 -4 0 -1]
Apply the
at()
method of thesign
universal function to the third and fifth array elements:np.sign.at(a, [2, 4]) print(a)
We get the following altered array:
[ 0 -1 -1 -1 -1 0 -1]