Machine Learning Fundamentals with scikit-learn

Lesson 4 of 8

Regression

Classification predicts a category, regression predicts a continuous number, tomorrow's temperature, a house's price, a patient's blood sugar level. The core workflow, fit/predict/evaluate, stays the same, only the kind of output and the evaluation metrics change.

Predicting a number

Pythonshares state with other Python blocks

load_diabetes() is another small bundled dataset, this one maps patient measurements to a numeric disease progression score, a continuous value, not a category, so classification metrics like accuracy don't apply. LinearRegression learns a straight-line (technically, a weighted sum) relationship between the input features and that number.

Regression metrics

Pythonshares state with other Python blocks
  • MSE (mean squared error) averages the squared difference between each prediction and the true value, squaring both penalizes big misses more than small ones and keeps the result positive. Lower is better, but MSE alone is hard to interpret since its units are "target units squared".
  • (R-squared) is easier to read: it's the fraction of the target's variance your model explains, 1.0 means perfect predictions, 0.0 means your model is no better than always guessing the average, and it can even go negative for a model worse than that baseline.

NOTE

Predicting a category and predicting a number look almost identical in scikit-learn code, swap DecisionTreeClassifier for DecisionTreeRegressor, swap accuracy_score for mean_squared_error, everything else follows the same fit/predict shape from the first lesson.

📝 Regression Quiz

Passing score: 70%
  1. 1.What does an R² score of 1.0 mean?

  2. 2.Regression predicts a continuous number, while classification predicts a category.

  3. 3.Mean squared error penalizes large mistakes more than small ones because the differences are ____ before averaging.