Perform unbuffered in place operation on operand for elements specified by 'indices' in Numpy


To perform unbuffered in place operation on operand for elements specified by 'indices, use the numpy.ufunc.at() method in Python Numpy.

The numpy.ufunc has functions that operate element by element on whole arrays. The ufuncs are written in C (for speed) and linked into Python with NumPy’s ufunc facility. A universal function (or ufunc for short) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, type casting, and several other standard features. That is, a ufunc is a "vectorized" wrapper for a function that takes a fixed number of specific inputs and produces a fixed number of specific outputs.

Steps

At first, import the required library −

import numpy as np

Create two 1d arrays −

arr1 = np.array([10, 20, 30, 40, 50])
arr2 = np.array([15, 25, 35, 45, 55])

Display the arrays −

print("Array 1...
", arr1) print("
Array 2...
", arr2)

Get the type of the arrays −

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

Get the dimensions of the Arrays −

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

To perform unbuffered in place operation on operand for elements specified by 'indices, use the numpy.ufunc.at() method in Python Numpy.

Set negative values. The np.negative.at() is used to set specific items to negative values. Here, the 2nd parameter are indices i.e. Array like index object or slice object for indexing into first operand. If first operand has multiple dimensions, indices can be a tuple of array like index objects or slice objects:

np.negative.at(arr1, [0, 1])
print("
Set negative values...
", arr1)

Set positive values. The np.add.at() is used to set specific items to increment values −

np.add.at(arr2, [0, 1], 1)
print("
Set positive values...
", arr2)

Example

import numpy as np

# The numpy.ufunc has functions that operate element by element on whole arrays.
# ufuncs are written in C (for speed) and linked into Python with NumPy’s ufunc facility

# Create two 1d arrays
arr1 = np.array([10, 20, 30, 40, 50])
arr2 = np.array([15, 25, 35, 45, 55])

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # To perform unbuffered in place operation on operand for elements specified by ‘indices, use the numpy.ufunc.at() method in Python Numpy # Set negative values # The np.negative.at() is used to set specific items to negative values # Here, the 2nd parameter are indices i.e. Array like index object or slice object for indexing into # first operand. If first operand has multiple dimensions, indices can be a tuple of array like index objects or slice objects. np.negative.at(arr1, [0, 1]) print("
Set negative values...
", arr1) # Set positive values # The np.add.at() is used to set specific items to increment values np.add.at(arr2, [0, 1], 1) print("
Set positive values...
", arr2)

Output

Array 1...
[10 20 30 40 50]

Array 2...
[15 25 35 45 55]

Our Array 1 type...
int64

Our Array 2 type...
int64

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
1

Set negative values...
[-10 -20 30 40 50]

Set positive values...
[16 26 35 45 55]

Updated on: 07-Feb-2022

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements