📖 Scenario: You are a data analyst at a small store. You want to understand how the number of hours spent advertising affects the number of sales. You have collected data for 7 days.
🎯 Goal: Build a simple linear regression model using np.polyfit() to find the relationship between advertising hours and sales. Then, display the slope and intercept of the line.
📋 What You'll Learn
Create two lists: hours and sales with given values
Create a variable degree to set the polynomial degree
Use np.polyfit() with hours, sales, and degree to find the slope and intercept
Print the slope and intercept values
💡 Why This Matters
🌍 Real World
Linear regression helps businesses understand relationships between variables, like advertising time and sales, to make better decisions.
💼 Career
Data analysts and scientists use linear regression to build predictive models and explain trends in data.
Progress0 / 4 steps
1
Create the data lists
Create a list called hours with these exact values: [1, 2, 3, 4, 5, 6, 7]. Create another list called sales with these exact values: [3, 4, 2, 5, 6, 7, 8].
NumPy
Hint
Use square brackets to create lists. Separate numbers with commas.
2
Set the polynomial degree
Create a variable called degree and set it to 1 to indicate a linear fit.
NumPy
Hint
Set degree to 1 because linear regression fits a line.
3
Apply np.polyfit() to find slope and intercept
Import numpy as np. Use np.polyfit() with hours, sales, and degree to create a variable called coefficients. This will hold the slope and intercept.
NumPy
Hint
Use import numpy as np first. Then call np.polyfit(hours, sales, degree).
4
Print the slope and intercept
Print the slope and intercept from coefficients. The slope is the first element, and the intercept is the second element. Use print() to show them with labels.
NumPy
Hint
Use coefficients[0] for slope and coefficients[1] for intercept. Use print() with f-strings.
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
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.
Step 2: Identify the output
The function returns coefficients of the polynomial, which for degree 1 are slope and intercept.
Final Answer:
An array with slope and intercept of the best fit line -> Option C
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
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.
Step 2: Match correct argument order
Only np.polyfit(x, y, 1) follows this order correctly.
Final Answer:
np.polyfit(x, y, 1) -> Option A
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
Step 1: Analyze data points
y is exactly 2 times x, so the line is y = 2*x + 0.
Step 2: Understand np.polyfit output
It returns [slope, intercept], so here [2.0, 0.0].
Final Answer:
[2.0, 0.0] -> Option A
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
Step 1: Check argument order in np.polyfit
np.polyfit expects x first, then y, then degree.
Step 2: Identify mistake in code
Here, y and x are swapped, so the fit will be incorrect or misleading.
Final Answer:
x and y are swapped in np.polyfit arguments -> Option B
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?