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

Analyzing intra-year daily average temperatures


We are going to have a look at the temperature variation within a year by converting dates to the corresponding day of the year in numbers. This number is between 1 and 366, where 1 corresponds to January 1st and 365 (or 366) corresponds to December 31st. Perform the following steps to analyze the intra-year daily average temperature:

  1. Initialize arrays for the range 1-366 with averages initialized to zeros:

    rng = np.arange(1, 366)
    avgs = np.zeros(365)
    avgs2 = np.zeros(365)
  2. Calculate averages by the day of the year before and after a cutoff point:

    for i in rng: 
       indices = np.where(days[:cutoff] == i)
       avgs[i-1] = temp[indices].mean()
       indices = np.where(days[cutoff+1:] == i)
       avgs2[i-1] = temp[indices].mean()
  3. Fit the averages before the cutoff point to a quadratic polynomial (just a first-order approximation):

    poly = np.polyfit(rng, avgs, 2)
    print poly

    The following polynomial coefficients in descending power are printed:

    [ -4.91329859e-04...