Time for action – applying the ufunc methods on add
Let's call the four methods on add
function.
The input array is reduced by applying the universal function recursively along a specified axis on consecutive elements. For the
add
function, the result of reducing is similar to calculating the sum of an array. Call thereduce
method:a = np.arange(9) print "Reduce", np.add.reduce(a)
The reduced array should be as follows:
Reduce 36
The
accumulate
method also recursively goes through the input array. But, contrary to thereduce
method, it stores the intermediate results in an array and returns that. The result, in the case of theadd
function, is equivalent to calling thecumsum
function. Call theaccumulate
method on theadd
function:print "Accumulate", np.add.accumulate(a)
The accumulated array:
Accumulate [ 0 1 3 6 10 15 21 28 36]
The
reduceat
method is a bit complicated to explain, so let's call it and go through its algorithm, step-by-step. Thereduceat
method requires as arguments, an...