Supervised learning splits into two families depending on what kind of value must be predicted.

Regression

In regression the target is a continuous numerical value. The model predicts a quantity: the price of a house, tomorrow's temperature, a student's marks. A simple model is the straight line y = w x + b fitted to the data by minimising the squared error. Regression models are judged by how close their predictions are to the true values, using measures such as mean squared error.

Classification

In classification the target is a discrete class label. The model assigns each instance to one of a fixed set of classes: spam or not spam, disease or healthy, a digit from 0 to 9. With two classes the task is binary classification; with more it is multi-class. Classification models are judged by how often they are right, using accuracy and related measures, which Unit II treats in detail.

Telling Them Apart

Ask what the answer looks like. If the answer is a number on a scale where the distance between values matters, use regression; if the answer is a category, use classification. Some problems can be framed either way: predicting marks is regression, but predicting only pass or fail is classification. Predicting a rating from 1 to 5 stars may be treated either way.

A Worked Comparison

Suppose hours of study are 1, 2, 3, 4, 5 and marks are 35, 45, 50, 62, 70. Fitting a line gives slope 8.7 and intercept 26.3, so 6 hours predicts 78.5 marks (regression). If instead we record only whether the student passed (0, 0, 0, 1, 1), a depth-one decision tree splits at 3.5 hours (classification).

Definition: Regression and classification. Regression predicts a continuous numerical target; classification predicts a discrete class label from a fixed set of classes.

Table 1.4: Regression compared with classification

Aspect Regression Classification
Target Continuous number Discrete class
Example Predict house price Detect spam
Typical model Linear regression Decision tree, KNN
Typical measure Mean squared error Accuracy, F1 score
Output of a model A real value A class label

Example 1.4: Fitting a line by least squares

Problem: Fit y = w x + b to hours x = 1, 2, 3, 4, 5 and marks y = 35, 45, 50, 62, 70, and predict marks for 6 hours.

Solution:

  1. The means are x̄ = 3 and ȳ = 52.4.
  2. Σ(x − x̄)(y − ȳ) = (−2)(−17.4) + (−1)(−7.4) + 0 + (1)(9.6) + (2)(17.6) = 87, and Σ(x − x̄)² = 10.
  3. The slope is w = 87 / 10 = 8.7, and the intercept is b = 52.4 − 8.7 × 3 = 26.3.
  4. For 6 hours, y = 8.7 × 6 + 26.3 = 78.5.

Result: The line is y = 8.7x + 26.3 and the prediction for 6 hours is 78.5 marks.

from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeClassifier
hours = [[1], [2], [3], [4], [5]]
marks = [35, 45, 50, 62, 70]
lr = LinearRegression().fit(hours, marks)
print("slope", round(lr.coef_[0], 2), "intercept",
      round(lr.intercept_, 2))
print("Marks for 6 hours:", round(lr.predict([[6]])[0], 1))
passed = [0, 0, 0, 1, 1]
tree = DecisionTreeClassifier(max_depth=1, random_state=0)
tree.fit(hours, passed)
print("Split at hours =", tree.tree_.threshold[0])
print("Class for 4.5 hours:", tree.predict([[4.5]])[0])

Output

slope 8.7 intercept 26.3
Marks for 6 hours: 78.5
Split at hours = 3.5
Class for 4.5 hours: 1

Common mistakes

  • Calling logistic regression a regression task; it predicts classes.
  • Using accuracy to judge a regression model.
  • Treating class codes 0, 1, 2 as if they had numerical distance.
  • Choosing regression when only a category is needed.

Key takeaways

  • Regression: continuous target. Classification: discrete class.
  • Binary classification has two classes; multi-class has more.
  • Line fit by least squares: w = Sxy / Sxx, b = ȳ − w x̄.
  • Same data can be framed either way depending on the question.

Practice questions

  1. Is predicting tomorrow's temperature regression or classification?
  2. Is detecting spam regression or classification?
  3. What is binary classification?

Answers to the practice questions

  1. Regression, since the target is continuous.
  2. Classification, since the target is a class.
  3. Classification with exactly two classes.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit I