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.
The given input array is reduced by applying the universal function recursively along a specified axis on consecutive elements.
add.reduce() is equivalent to
Syntax: ufunc.reduce(a, axis=0, dtype=None, out=None, keepdims=False, initial=, where=True)
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.
r : ndarray
The array [0 1 2 3 4 5 6 7 8 9] gets reduced to 45
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)
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.
r : ndarray
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.]
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)
A(array_like): First array
B(array_like): Second array
kwargs(any): Arguments to pass on to the ufunc.
r : ndarray
The outer of [0 1 2] & [[0 1] [2 3]] is [[[0 1] [2 3]] [[1 2] [3 4]] [[2 3] [4 5]]]
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.
Reduceat of matrix [0 1 2 3 4 5 6 7 8] is [ 6 4 27 8]
It concerns with indices 1 and 4. This step results in a reduced operation of the array elements between indices 1 and 4.
Result of STEP-I is 6
The second step concerns indices 4 and 2. Since 2 is less than 4, the array element at index 4 is returned:
Result of STEP-II is 4
The third step concerns indices 2 and 8. This step results in a reduce operation of the array elements between indices 2 and 8:
Result of STEP-III is 27
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:
Result of step IV is 8
By going through all this step we get the output of ‘numpy.add.reduceat’.
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Dictionary Methods in Python | Set 2 (update(), has_key(), fromkeys()...)
- List methods in Python
- Python Input Methods for Competitive Programming
- Analysis of Different Methods to find Prime Number in Python
- Accessing Attributes and Methods in Python
- Dunder or magic methods in Python
- Python | Float type and its methods
- Difference between map, applymap and apply methods in Pandas
- Python | Implementing 3D Vectors using dunder methods
- Private Methods in Python
- Bound methods python
- Bound, unbound, and static methods in Python
- Http Request methods - Python requests
- Response Methods - Python requests
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai