July 2018
Beginner to intermediate
406 pages
9h 55m
English
Let's assume for a second that the underlying model is a straight line. Then the challenge is how to best put that line into the chart so that it results in the smallest approximation error. SciPy's polyfit() function does exactly that. Given data x and y and the desired order of the polynomial (a straight line has an order of 1), it finds the model function that minimizes the error function defined earlier:
fp1 = np.polyfit(x, y, 1)
The polyfit() function returns the parameters of the fitted Model function, fp1:
>>> print("Model parameters: %s" % fp1)Model parameters: [ 2.59619213 989.02487106]
This means the best straight line fit is the following function:
f(x) = 2.59619213 * x + 989.02487106
We then ...
Read now
Unlock full access