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 – finding highest and lowest values


The min() and max() functions are the answer for our requirement. Perform the following steps to find the highest and lowest values:

  1. First, read our file again and store the values for the high and low prices into arrays:

    h,l=np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True)

    The only thing that changed is the usecols parameter, since the high and low prices are situated in different columns.

  2. The following code gets the price range:

    print("highest =", np.max(h))
    print("lowest =", np.min(l))

    These are the values returned:

    highest = 364.9
    lowest = 333.53
    

    Now, it's easy to get a midpoint, so it is left as an exercise for you to attempt.

  3. NumPy allows us to compute the spread of an array with a function called ptp(). The ptp() function returns the difference between the maximum and minimum values of an array. In other words, it is equal to max(array)min(array). Call the ptp() function:

    print("Spread high price", np.ptp(h))
    print("Spread...