Simple Linear Regression › Example
Machine Learning from ScratchAll chapters Simple Linear Regression › ExampleOne section · Sections in this chapter5 / 10 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through a concrete example with actual numbers to see how simple linear regression works step by step. Suppose you're studying the relationship between study hours and test scores. You have collected the following data from five students:
| Study Hours (x) | Test Score (y) |
|---|---|
| 1 | 65 |
| 2 | 70 |
| 3 | 80 |
| 4 | 85 |
| 5 | 90 |
Step 1: Calculate the means
First, we need to find the average values of both variables:
Step 2: Calculate the slope ()
Now we'll use the slope formula. Let's break this down into manageable pieces:
First, calculate for each data point:
- Point 1:
- Point 2:
- Point 3:
- Point 4:
- Point 5:
Numerator:
Next, calculate for each data point:
- Point 1:
- Point 2:
- Point 3:
- Point 4:
- Point 5:
Denominator:
Therefore:
Step 3: Calculate the intercept ()
Using the intercept formula:
Our final fitted equation is:
Step 4: Make a prediction
If a student studies for 6 hours, their predicted test score would be:
Step 5: Interpret the results
Our results can be interpreted as follows.
- Intercept (58.5): A student who studies 0 hours would be expected to score 58.5 points (though this might not be realistic in practice)
- Slope (6.5): For each additional hour of study, the test score increases by an average of 6.5 points
- Prediction: A student studying 6 hours would be expected to score about 97.5 points
The reason that we can simply interpret the slope and intercept as the change in the test score for each additional hour of study and the baseline score for no study, respectively, is because the linear model is a linear function of the form . This is why linear regression models are so interpretable.
Let's visualize this example to see how our calculated regression line fits the data and how we can use it to make predictions. The plot will show the original data points, the fitted regression line, and a prediction for a student studying 6 hours. This visualization helps confirm that our manual calculations are correct and demonstrates the practical application of simple linear regression.
