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

Modeling temperature with the SciPy leastsq function


So, now we have two ideas: either the temperature today depends on the temperature yesterday and the day before yesterday, and we assume that some kind of linear combination is formed, or the temperature depends on a day of the year (between 1 and 366). We can combine these ideas, but then the question is how. It seems that we could have a multiplicative model or an additive model.

Let's choose the additive model since it seems simpler. This means that we assume that temperature is the sum of the autoregressive component and a cyclical component. It's easy to write this down into one equation. We will use the SciPy leastsq function to minimize the square of the error of this equation. The procedure for this model is illustrated as follows:

  1. Define a function that computes the error of our model. The code is as follows:

    def error(p, d, t, lag2, lag1):
       l2, l1, d2, d1, d0 = p
     
       return t - l2 * lag2 + l1 * lag1 + d2 * d ** 2 + d1 * d + d0...