machine learning30 min

Supervised Learning

Learning from labeled examples to make predictions on new data

0/9Not Started

Why This Matters

Whenever you unlock your phone with face recognition, get a spam email filtered out, or see a product recommendation, a supervised learning model is working behind the scenes. It is the most widely used form of machine learning and the foundation you need before studying any other ML technique.

Supervised learning works the way a student learns from a textbook with an answer key. You show the model thousands of examples with known answers, and it learns patterns so it can predict answers for examples it has never seen before.

Define Terms

Visual Model

Training DataLabeled examples
ModelLearns patterns
Predictions
True Labels
LossCompare error
Update Model
repeat

The full process at a glance. Click Start tour to walk through each step.

Supervised learning: labeled data flows through training, prediction, comparison with labels, and parameter updates in a cycle.

Code Example

Code
// Supervised learning concept: k-Nearest Neighbors
// Classification: predict a label from labeled examples

function euclideanDistance(a, b) {
  let sum = 0;
  for (let i = 0; i < a.length; i++) {
    sum += (a[i] - b[i]) ** 2;
  }
  return Math.sqrt(sum);
}

// Training data: [feature1, feature2] => label
const trainingData = [
  { features: [1, 2], label: "A" },
  { features: [2, 3], label: "A" },
  { features: [5, 6], label: "B" },
  { features: [6, 7], label: "B" },
];

// Predict: find the nearest neighbor
function predict(point) {
  let nearest = trainingData[0];
  let minDist = Infinity;
  for (const example of trainingData) {
    const dist = euclideanDistance(point, example.features);
    if (dist < minDist) {
      minDist = dist;
      nearest = example;
    }
  }
  return nearest.label;
}

console.log(predict([1.5, 2.5])); // "A"
console.log(predict([5.5, 6.5])); // "B"

Interactive Experiment

Try modifying the code above:

  • Add more training data points and see how predictions change.
  • What happens if you add a point right between the two clusters, like [3.5, 4.5]?
  • Modify the code to implement k=3 nearest neighbors instead of just 1. How does the prediction change for borderline cases?
  • Change the features from 2D to 3D and observe how distance works in higher dimensions.

Quick Quiz

Coding Challenge

Simple Linear Regression

Write a function called `linearPredict` that takes an array of training points (each with an `x` and `y` value) and a new `x` value, then predicts `y` using the slope and intercept from a simple linear regression. Use the formulas: slope = sum((xi - xMean)(yi - yMean)) / sum((xi - xMean)^2), intercept = yMean - slope * xMean.

Loading editor...

Real-World Usage

Supervised learning powers countless applications you use daily:

  • Email filtering: Classifying messages as spam or legitimate based on text content, sender reputation, and link analysis.
  • Medical diagnosis: Predicting disease presence from patient symptoms, lab results, and imaging data.
  • Voice assistants: Converting speech to text by classifying audio signals into words and phonemes.
  • Credit scoring: Predicting loan default risk from financial history, income, and employment data.
  • Recommendation engines: Predicting which products or content a user will engage with based on past behavior.

Connections