The ufunc expect a set of scalars as input and produce a set of scalars as output. Universal
functions can be related to mathematical aspects such as, add, subtract, divide, multiply, and likewise.
Methods on numpy.add
Actually, universal functions are not functions instead, these are objects representing functions. Here we are taking the function – add, they have two input parameters and return one output parameter (signature mismatch of ufunc will results in a ValueError. Hence this will work only for binary universal functions).
The four methods on add are:
In this tutorial we are going through each of the above function in detail.
numpy.ufunc.reduce()
The given input array is reduced by applying the universal function recursively along a specified axis on consecutive elements.
Note: add.reduce()
is equivalent to sum()
Syntax: ufunc.reduce(a, axis=0, dtype=None, out=None, keepdims=False, initial=, where=True)
Parameters:
a(array_like): The array upon which processing occurs
axis(None or int or tuple of ints, optional): Axis or axes along which a reduction is performed. The default is (axis = 0). Axis may be negative, whenever it counts backwards.None, a reduction is performed over all the axes.Tuple of ints, a reduction is performed on multiple axes.
dtype(data-type code, optional): The type used to represent the intermediate results.
out(ndarray, None, or tuple of ndarray and None, optional): Location into which the result is stored. If not provided or None, a freshly-allocated array is returned.
keepdims(bool, optional): If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
initial(scalar, optional): The value with which to start the reduction.
where(array_like of bool, optional): A boolean array which is broadcasted to match the dimensions of a, and selects elements to include in the reduction.
Returns:
r : ndarray
Example:
import numpy as np
a = np.arange( 10 )
b = np.add. reduce (a, dtype = int , axis = 0 )
print ( "The array {0} gets reduced to {1}" . format (a, b))
|
OUTPUT
The array [0 1 2 3 4 5 6 7 8 9] gets reduced to 45
numpy.ufunc.accumulate()
It stores the intermediate results in an array and returns that. The result, in the case of the add function, is equivalent to calling the cumsum function.
Syntax: ufunc.accumulate(array, axis=0, dtype=None, out=None)
Parameters:
array(array_like): The array to act on.
axis(int, optional): The axis along which to apply the accumulation; default is zero.
dtype(data-type code, optional): The data-type used to represent the intermediate results. Defaults to the data-type of the output array if such is provided, or the data-type of the input array if no output array is provided.
out(ndarray, optional): A location into which the result is stored. If not provided a freshly-allocated array is returned.
Returns:
r : ndarray
Example:
import numpy as np
a = np.arange( 10 )
c = np.add.accumulate(a, axis = 0 , dtype = float )
print ( "The array {0} gets added cumulatively to {1}" . format (a, c))
|
OUTPUT
The array [0 1 2 3 4 5 6 7 8 9] gets added cumulatively to [ 0. 1. 3. 6. 10. 15. 21. 28. 36. 45.]
numpy.ufunc.outer()
The ‘outer’ method returns an array that has a rank, which is the sum of the ranks of its two input arrays. The method is applied to all possible pairs of the input array elements.
Syntax: ufunc.outer(A, B, **kwargs)
Parameters:
A(array_like): First array
B(array_like): Second array
kwargs(any): Arguments to pass on to the ufunc.
Returns:
r : ndarray
Example:
import numpy as np
a = np.arange( 4 ).reshape( 2 , 2 )
b = np.arange( 3 )
z = np.add.outer(b, a)
print ( "The outer of {0} & {1} is {2}" . format (b,a,z))
|
OUTPUT
The outer of [0 1 2] & [[0 1]
[2 3]] is [[[0 1]
[2 3]]
[[1 2]
[3 4]]
[[2 3]
[4 5]]]
numpy.ufunc.reduceat()
The ‘reduceat()
‘ method requires as arguments, an input array, and a list of indices. The reduceat()
method goes through step-by-step procedures to perform its operation. We will look up its action by four steps.
Example:
import numpy as np
a = np.arange( 9 )
z = np.add.reduceat(a, [ 1 , 4 , 2 , 8 ])
print ( "Reduceat of matrix {} is {}" . format (a,z))
|
OUTPUT
Reduceat of matrix [0 1 2 3 4 5 6 7 8] is [ 6 4 27 8]
STEP-1
It concerns with indices 1 and 4. This step results in a reduced operation of the array elements between indices 1 and 4.
import numpy as np
a = np.arange( 9 )
print ( "Result of STEP-I is" , np.add. reduce (a[ 0 : 4 ]))
|
OUTPUT
Result of STEP-I is 6
STEP-2
The second step concerns indices 4 and 2. Since 2 is less than 4, the array element at index 4 is returned:
import numpy as np
a = np.arange( 9 )
print ( "Result of STEP-II is" , a[ 4 ])
|
OUTPUT
Result of STEP-II is 4
STEP-3
The third step concerns indices 2 and 8. This step results in a reduce operation of the array elements between indices 2 and 8:
import numpy as np
a = np.arange( 9 )
print ( "Result of STEP-III is" , np.add. reduce (a[ 2 : 8 ]))
|
OUTPUT
Result of STEP-III is 27
STEP-4
The fourth step concerns index 8. This step results in a reduce operation of the array elements from index 8 to the end of the array:
import numpy as np
a = np.arange( 9 )
print ( "Result of step IV is" , np.add. reduce (a[ 8 :]))
|
OUTPUT
Result of step IV is 8
By going through all this step we get the output of ‘numpy.add.reduceat’.
Similar Reads
Python | Numpy np.legadd() method
np.legadd() method is used to add one Legendre series to another.It returns the sum of two Legendre series c1 + c2. Syntax : np.legadd(c1, c2) Parameters: c1, c2 :[ array_like ] 1-D arrays of Legendre series coefficients ordered from low to high. Return : [ndarray] Array representing the Legendre se
1 min read
Python | Numpy np.lagadd() method
np.lagadd() method is used to add one Laguerre series to another.It returns the sum of two Laguerre series c1 + c2. Syntax : np.lagadd(c1, c2) Parameters: c1, c2 :[ array_like ] 1-D arrays of Laguerre series coefficients ordered from low to high. Return : [ndarray] Array representing the Laguerre se
1 min read
Python | Numpy np.hermadd() method
With the help of np.hermadd() method, we can add the one hermite series to another by using np.hermadd() method. Syntax : np.hermadd(series1, series2) Return : Return the coefficients after addition of series. Example #1 : In this example we can see that by using np.hermadd() method, we are able to
1 min read
Python | Numpy np.chebadd() method
With the help of np.chebadd() method, we can get the addition of two Chebyshev series by using np.chebadd() method. Syntax : np.chebadd(s1, s2) Return : Return an array after addition. Example #1 : In this example we can see that by using np.chebadd() method, we are able to get the addition of two c
1 min read
Python | Numpy np.hermeadd() method
With the help of np.hermeadd() method, we can get the addition of two hermiteE series by using np.hermeadd() method. Syntax : np.hermeadd(series1, series2) Return : Return the addition of two hermiteE series. Example #1 : In this example we can see that by using np.hermeadd() method, we are able to
1 min read
Python __add__() magic method
Python __add__() function is one of the magic methods in Python that returns a new object(third) i.e. the addition of the other two objects. It implements the addition operator "+" in Python. Python __add__() Syntax Syntax: obj1.__add__(self, obj2) obj1: First object to add in the second object.obj2
1 min read
Python | sympy.Add() method
With the help of sympy.Add() method, we can add two variables and can form a mathematical expression by using sympy.Add() method. Syntax : sympy.Add() Return : Return the addition of two variables. Example #1 : In this example we can see that by using sympy.Add() method, we are able to add the two v
1 min read
Python | sympy.Add() method
With the help of sympy.Add() method, we can add the two variables and can form a mathematical expression by using sympy.Add() method. Syntax : sympy.Add() Return : Return the addition of two variables. Example #1 : In this example we can see that by using sympy.Add() method, we are able to add the n
1 min read
Set add() Method in Python
The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot
5 min read
numpy.add() in Python
NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays. numpy.add() SyntaxSyntax : numpy.add(arr1, arr2, /, out=
4 min read