Polynomials: polyfit & poly1d

np.polyfit finds the least-squares polynomial that best fits a set of data points, and np.poly1d turns those coefficients into a reusable polynomial you can evaluate, plot, or solve.

Learn Polynomials: polyfit & poly1d in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll fit a trend line to noisy data, evaluate polynomials with poly1d and polyval, and find where a polynomial crosses zero with np.roots.

np.polyfit(x, y, deg) finds the polynomial of degree deg that minimizes the squared error against your data. It returns the coefficients ordered from the highest power down to the constant. A degree-1 fit gives [slope, intercept] — the classic trend line.

Once you have coefficients, np.poly1d(coeffs) builds a callable polynomial object. Call it like a function, p(x) , to evaluate at any point or array of points, and printing it shows the equation. For a one-off evaluation without keeping an object around, np.polyval(coeffs, x) does the same calculation directly.

np.roots(coeffs) finds where a polynomial equals zero. And the real power of polyfit shows up on noisy data: even when points scatter around a curve, polyfit recovers the underlying trend by minimizing squared error. A degree-2 fit, for instance, can capture a gentle bend that a straight line would miss.

Fit a straight line (degree 1) to these points by filling in the degree argument.

Answer: 1 . A degree of 1 fits a straight line, returning [slope, intercept] = [2., 5.] .

polyfit returns highest power first, so treating coeffs[0] as the intercept is wrong:

✅ Fix: for a line, slope is first, intercept is last:

A high-degree fit hugs the noise and oscillates wildly between points.

✅ Fix: start with degree 1 or 2 and only increase if the data clearly needs it.

❌ Forgetting poly1d coefficients are powers, not values

np.poly1d([2, 1]) means 2x + 1 , not the points 2 and 1.

✅ Fix: print the object — print(np.poly1d([2, 1])) shows the equation it represents.

Fit a trend line to monthly sales, then use it to predict a future month.

Lesson complete — you can fit curves to data!

You can fit polynomials with np.polyfit , evaluate them with np.poly1d and np.polyval , find zeros with np.roots , and recover a trend from noisy measurements.

🚀 Up next: Vectorizing Python Functions — apply a plain Python function across an array with np.vectorize .

Practice quiz

What does np.polyfit(x, y, deg) return?

  • The fitted y values
  • The least-squares polynomial coefficients
  • A plot
  • The roots

Answer: The least-squares polynomial coefficients. polyfit returns the coefficients of the best-fit polynomial of the requested degree.

In what order does np.polyfit return coefficients?

  • Lowest power first
  • Random order
  • Highest power first down to the constant
  • Alphabetical

Answer: Highest power first down to the constant. Coefficients are ordered from the highest power down to the constant term.

For a degree-1 fit, what are the two coefficients?

A degree-1 fit returns [slope, intercept], highest power (slope) first.

What does np.poly1d(coeffs) create?

  • A list of roots
  • A NumPy array
  • A plot object
  • A callable polynomial object

Answer: A callable polynomial object. poly1d builds a callable polynomial you can evaluate, print, or find roots of.

Which evaluates a polynomial in one call without building an object?

  • np.polyval(coeffs, x)
  • np.poly1d(coeffs)
  • np.polyfit(x, y)
  • np.roots(coeffs)

Answer: np.polyval(coeffs, x). np.polyval(coeffs, x) computes the value directly without a reusable object.

What does np.roots([1, -5, 6]) return?

np.roots finds where the polynomial is zero; x^2 - 5x + 6 has roots 2 and 3.

What does np.poly1d([2, 1]) represent?

  • The points 2 and 1
  • The fraction 2/1
  • The polynomial 2x + 1
  • An error

Answer: The polynomial 2x + 1. The coefficients are powers: [2, 1] means 2x + 1, not the data points 2 and 1.

For a degree-2 fit, what does the result [a, b, c] mean?

  • a*x**2 + b*x + c
  • a + b + c
  • c*x**2 + b*x + a
  • Three separate roots

Answer: a*x**2 + b*x + c. Highest power first: [a, b, c] means a*x**2 + b*x + c.

Why avoid a very high polynomial degree on noisy data?

  • It runs too slowly
  • polyfit rejects it
  • It overfits, wiggling through noise and generalizing badly
  • It returns no coefficients

Answer: It overfits, wiggling through noise and generalizing badly. A high degree hugs the noise and oscillates; pick the lowest degree that captures the real shape.

Which degree fits a straight trend line?

  • 3
  • 1
  • 0
  • 2

Answer: 1. Degree 1 fits a straight line, returning [slope, intercept].