Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning NumPy Array

You're reading from  Learning NumPy Array

Product type Book
Published in Jun 2014
Publisher
ISBN-13 9781783983902
Pages 164 pages
Edition 1st Edition
Languages
Author (1):
Ivan Idris Ivan Idris
Profile icon Ivan Idris

Table of Contents (14) Chapters

Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with NumPy 2. NumPy Basics 3. Basic Data Analysis with NumPy 4. Simple Predictive Analytics with NumPy 5. Signal Processing Techniques 6. Profiling, Debugging, and Testing 7. The Scientific Python Ecosystem 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}