☰ Learn Artificial Intelligence Tutorial Menu

How Machines Learn: Data, Patterns and Feedback

Written by CSA mentors · Updated 26 Sept 2026 · 5 min read


What does "the model learns from data" actually mean? Ask that in an interview and half the candidates freeze, because they've used the phrase a hundred times without opening the box. Inside is a loop so simple it's almost disappointing. Guess, measure the mistake, adjust, repeat. We'll open that loop, meet the three learning styles, and get to the habit that separates real ML from wishful thinking.

The training loop

Every supervised model, from a two-line regression to a frontier LLM, is trained with the same four moves.

  1. Take an example with a known answer (an input and its label).
  2. Predict the answer using the model's current parameters (its "weights").
  3. Measure the error between prediction and truth. This number is called the loss.
  4. Adjust the weights slightly in the direction that would have reduced the loss. This is gradient descent.

Do that across the whole dataset, many times over (each full pass is an epoch). You stop when the loss on held-out data stops falling, not when the training loss hits zero. Students mix those up constantly, and the difference is the generalisation section below.

Four-step loop: take example, model predicts, measure error, adjust weights, repeat

Three learning styles

StyleWhat the data looks likeTypical tasksBusiness example
SupervisedInputs with correct answers (labels)Classification, regression, forecastingPredict whether a loan will default
UnsupervisedInputs only, no answersClustering, anomaly detection, dimensionality reductionGroup Daraz customers into segments
ReinforcementAn environment that gives rewards for actionsSequential decisions, control, game playingTune which promotion to show next to maximise long-term sales

A fourth style, self-supervised learning, is how LLMs are pre-trained. The "label" is simply the next word in the sentence, so the internet becomes a labelled dataset for free. Reinforcement learning from human feedback (RLHF) then makes the assistant helpful and safe, and today's reasoning models add more reinforcement learning on problems with checkable answers (maths, code), rewarding the correct multi-step solution.

Three panels comparing supervised, unsupervised and reinforcement learning

Generalisation, which is the whole point

A model that memorises its training data is worthless. We need it to work on new data, and that ability is generalisation. It fails in two directions.

  • Underfitting. The model is too simple for the pattern (a straight line through a curved trend). High error everywhere.
  • Overfitting. The model is so flexible it learns the noise. Near-zero error on training data, bad error on anything new.

The defence is to split the data. Train on one part, tune on a validation part, and report final results on a test part the model never touched. Never judge a model by its score on the data it was trained on. We fail course projects for exactly this, and it's the most common mistake in first submissions.

Two dials control the balance. Model capacity (how many weights, how deep the tree) sets how complex a pattern can be learned, and more capacity needs more data. Regularisation holds the model back, for example by penalising large weights or randomly switching neurons off during training (dropout), so it can't memorise noise. Turning those dials against the validation set is most of what "training a model" means day to day.

Gradient descent by hand

Let's fit a single weight w so that prediction = w × hours_studied predicts exam marks, using nothing but the four-step loop.

hours = [1, 2, 3, 4, 5]
marks = [12, 19, 31, 42, 48]   # roughly 10 marks per hour

w = 0.0            # start with a bad guess
lr = 0.01          # learning rate: how big each adjustment is

for epoch in range(200):
    grad = 0.0
    for h, m in zip(hours, marks):
        pred = w * h                 # 2. predict
        error = pred - m             # 3. measure
        grad += 2 * error * h        # slope of the squared error
    w -= lr * grad / len(hours)      # 4. adjust
    if epoch % 50 == 0:
        loss = sum((w * h - m) ** 2 for h, m in zip(hours, marks)) / len(hours)
        print(f"epoch {epoch:3d}  w={w:.3f}  loss={loss:.1f}")

print("final w:", round(w, 2), "-> 6 hours predicts", round(w * 6), "marks")

Run it and watch w climb from 0 towards roughly 9.9 while the loss drops. That's learning, all of it. A neural network does the same for millions of weights at once, with automatic differentiation working out the gradients.

Measuring success

The loss is for the algorithm. People need business metrics, and picking the wrong one is how good models get binned and bad ones get shipped.

TaskMetricWatch out for
ClassificationAccuracy, precision, recall, F1Accuracy is misleading when classes are unbalanced (99% of transactions are not fraud)
Regression / forecastingMAE, RMSE, MAPEMAPE explodes when true values are near zero
Ranking / recommendationPrecision at k, click-throughOffline metrics may not match live behaviour

The 99% accuracy trap. A mobile-wallet team in Karachi proudly reported a fraud model with 99.2% accuracy. Their data scientist did the sum out loud. Fraud was 0.5% of transactions, so a "model" that said "not fraud" to everything would score 99.5% and catch nothing. They switched to precision and recall. Of the transactions flagged, how many were really fraud, and of all the real fraud, how much was caught? The retrained model caught most of it while flagging roughly one legitimate payment in four hundred. Whenever a student shows us an accuracy number, our first question is "what's the base rate?"

Quick recap

  • Training is a loop. Predict, measure the loss, nudge the weights, repeat.
  • Supervised learning needs labels, unsupervised finds structure, reinforcement learns from rewards, and self-supervised is how LLMs get pre-trained.
  • Generalising to unseen data is the goal, so always hold out validation and test sets.
  • Pick metrics that fit the business problem, especially when one class is rare.

Try this before the next lesson

  1. Change the learning rate in the snippet to 0.1 and then to 0.0001. What happens to the loss in each case, and why?
  2. For a K-Electric outage-prediction model, name the learning style, one suitable metric and one way it could overfit.
  3. Explain to a non-technical manager why a model with 100% accuracy on training data might be a bad sign.

Lesson 5 of 18

Sign in to track your progress and earn learning points for every lesson you finish.