-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learning NumPy Array
By :

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:
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)
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]
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...
Change the font size
Change margin width
Change background colour