Bird
Raised Fist0
SciPydata~20 mins

Polynomial fitting in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Polynomial Fitting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polynomial fit coefficients
What is the output of the following code that fits a polynomial of degree 2 to the data points?
SciPy
import numpy as np
from numpy import polyfit
x = np.array([0, 1, 2, 3])
y = np.array([1, 3, 7, 13])
coeffs = polyfit(x, y, 2)
print(coeffs)
A[1. 1. 1.]
B[2. 1. 1.]
C[1. 2. 1.]
D[1. 1. 2.]
Attempts:
2 left
💡 Hint
Remember that polyfit returns coefficients starting from the highest degree term.
data_output
intermediate
1:30remaining
Number of coefficients from polynomial fit
How many coefficients does polyfit return when fitting a polynomial of degree 4 to 6 data points?
SciPy
import numpy as np
from numpy import polyfit
x = np.arange(6)
y = np.array([1, 4, 9, 16, 25, 36])
coeffs = polyfit(x, y, 4)
print(len(coeffs))
A7
B6
C4
D5
Attempts:
2 left
💡 Hint
The number of coefficients is degree + 1.
visualization
advanced
2:30remaining
Plotting polynomial fit and data points
Which option produces a plot showing the original data points as red dots and the polynomial fit curve as a blue line?
SciPy
import numpy as np
import matplotlib.pyplot as plt
from numpy import polyfit, polyval
x = np.array([0, 1, 2, 3, 4])
y = np.array([1, 3, 7, 13, 21])
coeffs = polyfit(x, y, 2)
x_fit = np.linspace(0, 4, 100)
y_fit = polyval(coeffs, x_fit)
plt.scatter(x, y, color='red')
plt.plot(x_fit, y_fit, color='blue')
plt.show()
AScatter plot with red dots for data and blue line for fit
BScatter plot with blue dots for data and red line for fit
CLine plot with red line for data and blue dots for fit
DLine plot with green line for data and orange dots for fit
Attempts:
2 left
💡 Hint
Check the colors and plot types used for data and fit.
🧠 Conceptual
advanced
2:00remaining
Effect of polynomial degree on overfitting
What happens if you fit a polynomial of very high degree to a small noisy dataset?
AThe polynomial degree does not affect the fit quality
BThe polynomial ignores noise and fits the true trend perfectly
CThe polynomial fits the noise and generalizes poorly to new data
DThe polynomial always underfits the data
Attempts:
2 left
💡 Hint
Think about what happens when a model is too complex for the data.
🔧 Debug
expert
1:30remaining
Identify error in polynomial fitting code
What error does the following code raise?
SciPy
import numpy as np
from numpy import polyfit
x = np.array([1, 2, 3])
y = np.array([2, 4])
coeffs = polyfit(x, y, 2)
ATypeError: polyfit() missing required positional argument
BValueError: x and y must have the same length
CSyntaxError: invalid syntax
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check the lengths of x and y arrays.

Practice

(1/5)
1. What does the scipy.polyfit function do in polynomial fitting?
easy
A. It calculates the coefficients of the polynomial that best fits the data.
B. It plots the data points on a graph.
C. It predicts future data points without fitting.
D. It normalizes the data before fitting.

Solution

  1. Step 1: Understand the purpose of polyfit

    polyfit takes data points and finds polynomial coefficients that best fit those points.
  2. Step 2: Differentiate from other functions

    Plotting or normalization are not done by polyfit; it only calculates coefficients.
  3. Final Answer:

    It calculates the coefficients of the polynomial that best fits the data. -> Option A
  4. Quick Check:

    polyfit = coefficients [OK]
Hint: Remember: polyfit finds coefficients, not plots or predictions [OK]
Common Mistakes:
  • Confusing polyfit with plotting functions
  • Thinking polyfit predicts future points directly
  • Assuming polyfit normalizes data automatically
2. Which of the following is the correct syntax to fit a 3rd degree polynomial to data arrays x and y using SciPy?
easy
A. coeffs = scipy.polyfit(y, x, 3)
B. coeffs = scipy.polyfit(x, y, 3)
C. coeffs = scipy.polyfit(x, y)
D. coeffs = scipy.polyfit(x, y, degree=3)

Solution

  1. Step 1: Check the order of arguments in polyfit

    The correct order is polyfit(x, y, degree).
  2. Step 2: Confirm the degree argument is positional, not keyword

    polyfit expects degree as the third positional argument, not as a keyword.
  3. Final Answer:

    coeffs = scipy.polyfit(x, y, 3) -> Option B
  4. Quick Check:

    Correct syntax = coeffs = scipy.polyfit(x, y, 3) [OK]
Hint: Remember: polyfit(x, y, degree) with degree as positional [OK]
Common Mistakes:
  • Swapping x and y arguments
  • Omitting the degree argument
  • Using degree as a keyword argument
3. Given the code:
import numpy as np
from scipy import polyfit, polyval
x = np.array([0, 1, 2, 3])
y = np.array([1, 3, 7, 13])
coeffs = polyfit(x, y, 2)
fitted = polyval(coeffs, x)
print(fitted)

What is the output printed?
medium
A. [ 1. 4. 9. 16.]
B. [ 1. 2. 4. 8.]
C. [ 0. 1. 4. 9.]
D. [ 1. 3. 7. 13.]

Solution

  1. Step 1: Fit a 2nd degree polynomial to points

    The points (x, y) fit exactly to y = 1 + 2x + x^2, so polyfit finds coefficients close to [1, 2, 1].
  2. Step 2: Use polyval to compute fitted values at x

    Evaluating the polynomial at x gives the original y values: [1, 3, 7, 13].
  3. Final Answer:

    [ 1. 3. 7. 13.] -> Option D
  4. Quick Check:

    polyval(coeffs, x) = original y [OK]
Hint: polyval with polyfit coeffs returns fitted y values [OK]
Common Mistakes:
  • Confusing input arrays order
  • Expecting different output than original y
  • Misunderstanding polynomial degree effect
4. What is wrong with this code snippet for polynomial fitting?
import numpy as np
from scipy import polyfit, polyval
x = np.array([1, 2, 3])
y = np.array([2, 4, 6])
coeffs = polyfit(x, y, 2)
fitted = polyval(coeffs, x)
print(fitted)
medium
A. The code is correct and will run without errors.
B. The arrays x and y must be lists, not numpy arrays.
C. The degree 2 polynomial is too high for 3 points; use degree 1 instead.
D. polyval cannot be used with coefficients from polyfit.

Solution

  1. Step 1: Check polynomial degree vs data points

    Fitting a degree 2 polynomial to 3 points is mathematically valid and will produce a polynomial that fits all points exactly.
  2. Step 2: Validate data types and function usage

    Using numpy arrays is correct; polyval works with polyfit coefficients; no syntax errors present.
  3. Final Answer:

    The code is correct and will run without errors. -> Option A
  4. Quick Check:

    Degree 2 polynomial with 3 points = code runs fine [OK]
Hint: Degree equal to number of points minus one fits exactly [OK]
Common Mistakes:
  • Using too high polynomial degree for few points
  • Thinking numpy arrays are invalid input
  • Believing polyval can't use polyfit output
5. You have noisy data points and want to fit a polynomial that smooths the noise but avoids overfitting. Which approach is best?
hard
A. Use polyfit with degree zero to get a constant fit.
B. Fit a high-degree polynomial to capture all fluctuations.
C. Fit a low-degree polynomial and check the fit visually.
D. Fit multiple polynomials of different degrees and average coefficients.

Solution

  1. Step 1: Understand overfitting and noise smoothing

    High-degree polynomials fit noise too closely, causing overfitting; low-degree polynomials smooth data better.
  2. Step 2: Use visual check to confirm fit quality

    Plotting fitted curve helps decide if degree is appropriate and avoids overfitting.
  3. Final Answer:

    Fit a low-degree polynomial and check the fit visually. -> Option C
  4. Quick Check:

    Low degree + visual check = smooth fit [OK]
Hint: Low degree + visual check avoids overfitting [OK]
Common Mistakes:
  • Choosing too high degree polynomial
  • Using degree zero which ignores trends
  • Averaging coefficients from different fits