Course 2.1Lesson 3 of 5
NumPy stores a polynomial as an array of coefficients from the highest power down. np.polyval gives its values at many points, np.roots gives its roots, which may be complex numbers when there is no real root, np.poly builds the coefficients from the roots, and np.polyder gives the derivative. np.poly1d wraps the coefficients in an object that can be called, multiplied and differentiated. np.polyfit with a degree finds the polynomial that fits measured data best by the method of least squares, and with degree 1 it gives the slope and the intercept of a straight line. The residuals are the differences between the data and the fitted values, and R squared tells how much of the scatter of the data the fit explains. A fit is only as good as the model behind it.
Try the code and the simulations beside the lesson.
After this lesson you can
np.polyfit and judge the fit.You know that is made of the coefficients . NumPy stores a polynomial in just this way: as an array of the coefficients, from the highest power down. The degree is one less than the length of the array. A missing power must be written with a zero: is [1, 0, 0, -2].
Predict first. What are the values of the polynomial at 1, 2 and 3? Then run it.
To keep the real roots only, we test whether the imaginary part is close to zero: roots[np.isclose(roots.imag, 0)].real. For this polynomial the answer is an empty array, and the list is empty, so there are no real roots. Use np.isclose, and not == 0, because of the tiny errors in the last digits.
Data from an experiment has scatter: no point is exactly on the line. A spring is stretched with five different forces, and the extension is measured. Hooke's law says that the force is proportional to the extension. How can we find the best straight line, and the spring constant?
Predict first. The force of 4.9 N gives an extension of 0.10 m. What is the rough spring constant? Then run it.
np.polyfit(extension, force, 1) finds the straight line of degree 1 that fits the points best, and gives two numbers: the slope first, then the intercept. (The highest power comes first, as always.) The method is called least squares: it chooses the line for which the sum of the squares of the vertical distances from the points is the smallest.With a higher degree, np.polyfit(x, y, 2) fits a parabola, and so on. Do not use a high degree to make the line pass through every point. With 5 points, a polynomial of degree 4 passes through all of them exactly, but it wiggles between them and means nothing. A good model is as simple as the physics allows.
"Best" here has an exact meaning: the smallest sum of squared misses. That is what least squares means.
[1, 0, 0, -2].==. Use np.isclose.polyfit in the wrong order. The slope first, then the intercept.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
np.polyval, np.roots, np.poly, np.polyder and np.poly1d do the work.np.isclose(roots.imag, 0).np.polyfit(x, y, 1) gives the slope and then the intercept of the best line.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
np.roots([1, 0, 1]) give, and why?np.polyfit(x, y, 1) giving [3.0, 2.0], what is the line?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.