Skip to main content

Machine Learning: Regression, Classification & Model Evaluation

1. Machine Learning Overview

Machine Learning (ML) allows a computer to learn patterns from data and make predictions without explicitly programming every rule. Two common supervised learning tasks are:

2. Regression

Regression is used when the target/output is a continuous numerical value.

Example

Predict house price:
Common regression algorithms:
  1. Linear Regression
  2. Ridge Regression
  3. Lasso Regression

3. Linear Regression

Linear Regression finds a relationship between input features and a continuous target. The basic equation is:
Where:
  • y = predicted value
  • x = input
  • m = slope/coefficient
  • b = intercept
For multiple features:

Example

Predict salary based on years of experience.
Output:

Important Properties

  • coef_ → learned slope/weight
  • intercept_ → value of y when all features are 0

When to use

Use Linear Regression when:
  • Target is continuous
  • Relationship is approximately linear
  • You want a simple and interpretable model

4. Ridge Regression

Ridge Regression is Linear Regression with L2 regularization. It adds a penalty for large coefficients. Conceptually:
Where:
  • α = regularization strength
  • Larger α → stronger regularization

Example

Why use Ridge?

Ridge helps when:
  • Features are highly correlated
  • The model is overfitting
  • You have many features

Important

Ridge generally shrinks coefficients toward zero, but usually does not make them exactly zero.

5. Lasso Regression

Lasso Regression uses L1 regularization. Conceptually:
Unlike Ridge, Lasso can make some coefficients exactly zero.

Example

Why use Lasso?

Lasso is useful for:
  • Feature selection
  • Reducing unnecessary features
  • Preventing overfitting

Ridge vs Lasso


6. Classification

Classification predicts a category/class.

Examples

Common classification algorithms:
  1. Logistic Regression
  2. K-Nearest Neighbors
  3. Decision Tree
  4. Random Forest
  5. Support Vector Machine
  6. Naive Bayes

7. Logistic Regression

Despite its name, Logistic Regression is mainly used for classification. It predicts the probability of a class. The sigmoid function converts a value into a probability between 0 and 1.
Example:

Example

Important methods

Returns predicted classes.
Returns class probabilities.

When to use

Use Logistic Regression when:
  • Target is categorical
  • You need probability estimates
  • You want a simple and interpretable classification model

8. K-Nearest Neighbors (KNN)

KNN predicts a data point based on its nearest training examples. The basic idea:
Example:

Example

Choosing K

Small K:
Large K:

Important

KNN is distance-based, so feature scaling is usually important.

9. Decision Tree

A Decision Tree makes predictions using a sequence of questions. Example:
The tree contains:
  • Root node
  • Internal nodes
  • Branches
  • Leaf nodes

Example

Important parameters

Advantages

  • Easy to understand
  • Little preprocessing required
  • Can model non-linear relationships

Disadvantage

A deep tree can easily overfit.

10. Random Forest

Random Forest is an ensemble of multiple Decision Trees. Instead of relying on one tree:
For classification, trees vote. For regression, predictions are generally averaged.

Example

Important parameters

n_estimators controls the number of trees.

Advantages

  • Usually more robust than a single tree
  • Handles non-linear relationships
  • Works with many features
  • Less prone to overfitting than a single unrestricted tree

11. Support Vector Machine (SVM)

SVM finds a decision boundary that separates classes. The best boundary tries to maximize the margin between classes.
The closest points to the boundary are called support vectors.

Example

Common kernels

Example:

Important

SVM is sensitive to feature scale. Scaling is commonly performed before training:

12. Naive Bayes

Naive Bayes is a probabilistic classification algorithm based on Bayes’ theorem. Bayes’ theorem:
The “naive” assumption is that features are conditionally independent given the class.

Example

Common Naive Bayes types


13. Model Evaluation

After training a model, we need to determine:
Different metrics are used for different problems.

14. Confusion Matrix

A confusion matrix summarizes classification predictions. For binary classification: Where:
  • TP = True Positive
  • TN = True Negative
  • FP = False Positive
  • FN = False Negative
Example:
Results:

Python

Output:

15. Accuracy

Accuracy tells us the percentage of predictions that were correct. Formula:

Example

Output:
So:

Problem with Accuracy

Accuracy can be misleading for imbalanced datasets. Example:
A model predicting every transaction as “Not Fraud” gets:
But it detects:
Therefore, other metrics may be more useful.

16. Precision

Precision answers:
Of all the samples predicted as positive, how many were actually positive?
Formula:
Example:
So:

Python

When precision matters

Precision is important when false positives are costly. Example:
You don’t want many legitimate emails to be incorrectly marked as spam.

17. Recall

Recall answers:
Of all the actual positive samples, how many did the model find?
Formula:
Example:
So:

Python

When recall matters

Recall is important when false negatives are costly. Example:
Missing a positive case can be expensive.

18. Precision vs Recall

Remember:

19. F1 Score

F1 Score combines Precision and Recall. It is the harmonic mean of precision and recall. Formula:
Example:

Python

When to use

F1 is useful when:
  • Classes are imbalanced
  • Both precision and recall matter
  • You want one metric balancing both

20. ROC-AUC

ROC-AUC evaluates how well a classification model separates positive and negative classes across different classification thresholds.

ROC

ROC stands for:
It plots:
Where:
and

AUC

AUC means:
General interpretation:

Python

Output:

Important

ROC-AUC generally requires probabilities or decision scores, not just final class labels. For example:

21. MAE

MAE stands for:
It measures the average absolute difference between actual and predicted values. Formula:
Example:

Python

Output:

Interpretation

If MAE is:
the model is off by about 13.33 units on average.

22. MSE

MSE stands for:
Formula:
Example:

Python

Output:

Important

MSE gives more importance to large errors because errors are squared.

23. RMSE

RMSE stands for:
It is the square root of MSE. Formula:
For the previous example:

Python

Output:

Version note

If your scikit-learn version does not provide root_mean_squared_error, use:

24. R² Score

R² is called the coefficient of determination. It measures how well a regression model explains the variation in the target. A simplified interpretation:
Perfect prediction.
Model is roughly as good as predicting the mean.
Model can be worse than simply predicting the mean.

Python

Interpretation

Higher R² is generally better for a regression model, but it should not be used alone to judge model quality.

25. Classification Metrics Summary


26. Regression Metrics Summary


27. Complete Classification Example

A typical ML workflow looks like this:

28. Complete Regression Example


29. Which Algorithm Should I Choose?

Regression

Linear Regression

Use when:

Ridge

Use when:

Lasso

Use when:

Classification

Logistic Regression

KNN

Decision Tree

Random Forest

SVM

Naive Bayes


30. Quick Algorithm Comparison


31. Important Concepts to Remember

Overfitting

Model performs well on training data but poorly on unseen data.
Possible solutions:

Underfitting

Model is too simple to learn the underlying pattern.
Possible solutions:

32. Regularization

Regularization prevents a model from becoming unnecessarily complex.

Ridge

Lasso

Remember:

33. Train/Test Workflow

The standard supervised ML workflow is:
Example:

34. Most Important Formulas

Linear Regression

Logistic Regression

Ridge

Lasso

Accuracy

Precision

Recall

F1

MAE

MSE

RMSE


35. One-Minute Revision


36. Key Mental Model

The easiest way to remember the algorithms:
The most important evaluation distinction is: