Fit curves, calculate roots, and evaluate polynomials with polyfit() and poly1d().
NumPy provides polynomial utilities to evaluate mathematical curves ($ax^2 + bx + c$), compute polynomial roots, and perform polynomial regression fitting (`np.polyfit`).
Polynomial fitting is drawing the smoothest curve that passes through a cloud of noisy data points.
`np.polyfit(x, y, deg=1)` fits a linear regression line. `deg=2` fits a quadratic parabola.
coeffs = np.polyfit(x, y, deg=1)np.roots([1, 2]) # Roots takes polynomial coefficients listnp.roots([1, -4, 4]) # x^2 - 4x + 4 = 0 -> roots [2, 2]Coefficients must be ordered from highest degree power to constant term.
Solve the objective below using NumPy vectorized syntax
Create `p = np.poly1d([1, 0, -4])` (which represents $x^2 - 4$) and print `p(3)`.
5