Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Time for action – applying the ufunc methods to the add function


Let's call the first four methods on the add() function:

  1. The universal function reduces the input array 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 the reduce() method:

    a = np.arange(9)
    print("Reduce", np.add.reduce(a))

    The reduced array should be as follows:

    Reduce 36
    
  2. The accumulate() method also recursively goes through the input array. But, contrary to the reduce() method, 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. Call the accumulate() method on the add() function:

    print("Accumulate", np.add.accumulate(a))

    The accumulated array is as follows:

    Accumulate [ 0  1  3  6 10 15 21 28 36]
    
  3. The reduceat() method is a bit complicated to explain, so let's call it and go through its algorithm, step by step. The...