Bird
0
0

Given the code:

medium📝 Predict Output Q13 of 15
SciPy - Curve Fitting and Regression
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?
A[ 1. 4. 9. 16.]
B[ 1. 2. 4. 8.]
C[ 0. 1. 4. 9.]
D[ 1. 3. 7. 13.]
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes