Bird
Raised Fist0
NumPydata~10 mins

Linear regression with np.polyfit() in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fit a linear model to x and y using np.polyfit.

NumPy
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
coefficients = np.polyfit(x, y, [1])
Drag options to blanks, or click blank then click option'
A1
B3
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using degree 2 or higher for a simple linear fit.
Using degree 0 which fits a constant only.
2fill in blank
medium

Complete the code to predict y values using the fitted coefficients.

NumPy
import numpy as np
x = np.array([1, 2, 3, 4, 5])
coefficients = np.polyfit(x, np.array([2, 4, 5, 4, 5]), 1)
predicted_y = coefficients[[1]] * x + coefficients[1]
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using coefficients[1] as slope.
Using negative indices incorrectly.
3fill in blank
hard

Fix the error in the code to correctly fit a linear model and predict y.

NumPy
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
coefficients = np.polyfit(x, y, [1])
predicted_y = coefficients[0] * x + coefficients[[2]]
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using degree 0 or 2.
Using coefficients[0] as intercept.
4fill in blank
hard

Fill both blanks to create a dictionary of x values and their predicted y values using np.polyfit.

NumPy
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
coeffs = np.polyfit(x, y, [1])
predictions = {val: coeffs[0] * val + coeffs[[2]] for val in x}
Drag options to blanks, or click blank then click option'
A1
B0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong degree.
Mixing up slope and intercept indices.
5fill in blank
hard

Fill all three blanks to create a list of predicted y values for x using np.polyfit and a list comprehension.

NumPy
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
coeffs = np.polyfit(x, y, [1])
predicted = [coeffs[[2]] * val + coeffs[[3]] for val in x]
Drag options to blanks, or click blank then click option'
A1
B0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping slope and intercept indices.
Using wrong degree.

Practice

(1/5)
1. What does np.polyfit(x, y, 1) return when used for linear regression?
easy
A. The residual errors of the fit
B. The predicted y values for each x
C. An array with slope and intercept of the best fit line
D. A plot of the data points and the fit line

Solution

  1. Step 1: Understand np.polyfit parameters

    np.polyfit(x, y, 1) fits a polynomial of degree 1 (a line) to data points x and y.
  2. Step 2: Identify the output

    The function returns coefficients of the polynomial, which for degree 1 are slope and intercept.
  3. Final Answer:

    An array with slope and intercept of the best fit line -> Option C
  4. Quick Check:

    np.polyfit(x, y, 1) = [slope, intercept] [OK]
Hint: Degree 1 means line: output is slope and intercept [OK]
Common Mistakes:
  • Thinking it returns predicted values
  • Confusing output with residuals
  • Expecting a plot instead of coefficients
2. Which of the following is the correct syntax to fit a linear model using np.polyfit?
easy
A. np.polyfit(x, y, 1)
B. np.polyfit(1, x, y)
C. np.polyfit(x, 1, y)
D. np.polyfit(y, x, 1)

Solution

  1. Step 1: Recall np.polyfit argument order

    The function signature is np.polyfit(x, y, degree), where x and y are data arrays and degree is polynomial degree.
  2. Step 2: Match correct argument order

    Only np.polyfit(x, y, 1) follows this order correctly.
  3. Final Answer:

    np.polyfit(x, y, 1) -> Option A
  4. Quick Check:

    np.polyfit(x, y, 1) correct syntax [OK]
Hint: Remember order: x, y, degree [OK]
Common Mistakes:
  • Swapping x and y
  • Putting degree in the middle
  • Using wrong argument order
3. What will be the output of this code?
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 6, 8])
coeffs = np.polyfit(x, y, 1)
print(coeffs)
medium
A. [2.0, 0.0]
B. [0.0, 2.0]
C. [1.0, 2.0]
D. [2.0, 1.0]

Solution

  1. Step 1: Analyze data points

    y is exactly 2 times x, so the line is y = 2*x + 0.
  2. Step 2: Understand np.polyfit output

    It returns [slope, intercept], so here [2.0, 0.0].
  3. Final Answer:

    [2.0, 0.0] -> Option A
  4. Quick Check:

    Line y=2x+0 means coeffs = [2.0, 0.0] [OK]
Hint: Check if y is multiple of x for slope, intercept zero [OK]
Common Mistakes:
  • Confusing slope and intercept order
  • Swapping x and y arrays
  • Misreading output as predicted values
4. Identify the error in this code snippet:
import numpy as np
x = np.array([1, 2, 3])
y = np.array([2, 3, 5])
coeffs = np.polyfit(y, x, 1)
print(coeffs)
medium
A. Degree argument is missing
B. x and y are swapped in np.polyfit arguments
C. np.polyfit cannot handle arrays
D. No error, code is correct

Solution

  1. Step 1: Check argument order in np.polyfit

    np.polyfit expects x first, then y, then degree.
  2. Step 2: Identify mistake in code

    Here, y and x are swapped, so the fit will be incorrect or misleading.
  3. Final Answer:

    x and y are swapped in np.polyfit arguments -> Option B
  4. Quick Check:

    Correct order is np.polyfit(x, y, 1) [OK]
Hint: Remember: first x, then y in np.polyfit [OK]
Common Mistakes:
  • Swapping x and y arrays
  • Forgetting degree argument
  • Assuming np.polyfit can't handle arrays
5. You have data points x = [0, 1, 2, 3] and y = [1, 3, 7, 13]. Using np.polyfit(x, y, 1), what is the best fit line equation?
hard
A. y = 3x + 0
B. y = 3x + 1
C. y = 4x + 0
D. y = 4x + 1

Solution

  1. Step 1: Calculate slope and intercept manually

    Calculate slope m = (n∑xy - ∑x∑y)/(n∑x² - (∑x)²). Here, n=4, ∑x=6, ∑y=24, ∑xy=56, ∑x²=14. So, m = (4*56 - 6*24)/(4*14 - 6²) = (224 - 144)/(56 - 36) = 80/20 = 4.
  2. Step 2: Find intercept

    Intercept b = (∑y - m∑x)/n = (24 - 4*6)/4 = (24 - 24)/4 = 0.
  3. Step 3: Check residuals

    Check predicted y values: y_pred = 4x + 0 = [0,4,8,12], actual y = [1,3,7,13]. Residuals are not zero, so intercept might not be exactly zero.
  4. Step 4: Use np.polyfit to confirm

    Using np.polyfit(x, y, 1) gives approximately [4.0, 1.0], so intercept is about 1.
  5. Final Answer:

    y = 4x + 1 -> Option D
  6. Quick Check:

    np.polyfit(x, y, 1) ≈ [4.0, 1.0] [OK]
Hint: Slope = (last y - first y) / (last x - first x), intercept adjusted by average [OK]
Common Mistakes:
  • Ignoring intercept value
  • Calculating slope incorrectly
  • Assuming intercept is zero
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard