Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Day-of-year temperature take two


The quadratic polynomial approximation for the day-of-the-year temperature fit can be improved upon. We haven't used any of the NumPy trigonometric functions until now. Those should be a good fit for this problem. So, let's try a trigonometric function and fit again using a function from the scipy.optimize module (leastsq to be precise) as follows:

  1. Set up a simple model function and an error function to be minimized, as shown in the following code snippet:

    def model(p, d):
       a, b, w, c = p
       return a + b * np.cos(w * d + c)
     
    def error(p, d, t):
       return t - model(p, d)
  2. Give the initial guess and fit the data:

    p0 = [.1, 1, .01, .01]
    params = leastsq(error, p0, args=(days, temp))[0]
    print params

    We get the following parameters:

    [ 9.6848106  -7.59870042 -0.01766333 -5.83349705]
    

Note

Here, -2 pi over 365 is equal to the third parameter. I believe that the first parameter is equal to the average of all the temperatures, and we can come up with similar explanations...