Linear Regression
Machine Learning Fundamentals
Chapter 3 · Linear Regression
ds1-9's own used-car dataset returns. Its Step 7 hypothesis — "does mileage predict price more strongly than the car's year does?" — was raised from a heatmap and explicitly left untested. This chapter tests it for real.
What Linear Regression Actually Does
Linear regression fits a straight line (or, with more than one feature, a flat hyperplane) through data to predict a continuous numeric value — a price, not a category. With one feature: price = w × mileage + b. With several features at once (this chapter's own case): price = w₁ × mileage + w₂ × year + b. Each w is a learned coefficient; b is the intercept — what the model would predict if every feature were zero.
How "Fitting" Actually Works
ds1-6 already defined variance as the average squared distance between each value and the mean. Linear regression's own fitting process — least squares — minimizes a close cousin of that same idea: the total squared distance between each actual price and the price the line would have predicted for it. "Squared" matters for the same reason it did in ds1-6: it penalizes large errors disproportionately more than small ones, and keeps positive and negative errors from canceling out.
Coefficients Are Interpretable — With One Real Caveat
Each coefficient answers a direct question: how much does the predicted price change per one-unit change in that feature, holding the other features constant? This is the actual mechanism for testing this chapter's own hypothesis.
Fitting the Model — ds1-9's Own Dataset, For Real
from sklearn.linear_model import LinearRegression from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split X = df[["mileage", "year"]] y = df["price"] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) model = LinearRegression() model.fit(X_train_scaled, y_train) model.coef_ # [coefficient for mileage, coefficient for year] model.intercept_ # b
mileage's own coefficient comes back substantially larger in magnitude (and negative — more mileage, lower price) than year's own coefficient, which is positive but noticeably smaller once both are on the same standardized scale. ds1-9's own hypothesis is answered: mileage is the stronger predictor of the two, holding year constant — a real, direct, testable conclusion, not a description of the data's own general shape.
Why a Fitted Coefficient Is Stronger Evidence Than a Raw Correlation
ds1-9's own heatmap already showed mileage and price correlating. ds1-6's own confounding-variable warning applies at full strength to that pairwise number alone — a raw correlation between two variables says nothing about a third, unmeasured factor possibly driving both. Multiple linear regression is a genuine, practical (if partial) step beyond that: fitting mileage and year together means each coefficient already accounts for the other — "holding year constant" is quite literally controlling for one of the two candidate confounders directly in the model itself, rather than leaving it unaccounted for.
ds1-9's own table). This is honest progress, not a claim of full causal proof.
What This Chapter Doesn't Yet Tell You
Knowing mileage matters more than year is a real finding — but it says nothing about how good this model actually is at predicting price overall. A model could correctly rank mileage as the stronger factor while still being wildly inaccurate in its actual dollar predictions. ml1-4 covers exactly that next.
Hands-On Exercises
Explain, using this chapter's own warn-box, why comparing mileage's raw coefficient directly against year's raw coefficient would be misleading, and explain what standardization actually fixes.
📄 View solutionExplain, using ds1-6's own confounding-variable material and this chapter's own tip-box, why fitting mileage and year together is real progress over ds1-9's own single pairwise correlation, and why it still isn't full proof of causation.
📄 View solutionExplain why this chapter's own finding (mileage matters more than year) doesn't yet tell you whether the model's actual price predictions are any good, and identify what would be needed to answer that second question.
📄 View solutionChapter 3 Quick Reference
- Linear regression predicts a continuous value; coefficients + an intercept define the fitted line/hyperplane
- Least squares fitting — minimizing squared prediction error, the same "squared distance" idea as ds1-6's own variance
- Coefficient size is only comparable across features after standardization — raw units differ (mileage vs. year)
- ds1-9's hypothesis, closed: mileage predicts price more strongly than year, holding the other constant
- Fitting features together partially controls for confounding (ds1-6) — real progress, not full causal proof
- Next chapter: Evaluation Metrics for Regression