Skip to main content
ANVISoftware Solutions
Lesson 2 of 22Beginner14 min

Machine Learning

By the end of this lesson

Explain learning from data versus writing explicit rules.

In ordinary programming you supply the rules and the computer applies them. In machine learning you supply examples and the computer derives something that behaves like rules. That is the whole difference, and everything else in this course follows from it.

The output of that process is a model: a large collection of numbers, tuned so that feeding an input through them produces a useful output. Nobody writes those numbers. Nobody can read them back as sentences either, which is why explaining a model's decision is harder than explaining an if statement.

Training and inference

Two separate activities get muddled constantly because both are called "using AI". Training is where the model is built from examples. Inference is where a finished model is given one input and produces one output. They have different costs, different failure modes, and in most projects they are done by different people at different times.

A small classifier for expense claim categories
Python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline

# Examples a human has already categorised
descriptions = [
    "Taxi from the airport to the client office",
    "Return rail ticket to Manchester",
    "Team lunch with three colleagues",
    "Sandwiches for the sprint review",
    "Annual licence for design software",
    "Replacement laptop charger",
]
categories = ["travel", "travel", "meals", "meals", "software", "equipment"]

model = make_pipeline(TfidfVectorizer(), LogisticRegression())
model.fit(descriptions, categories)          # training

print(model.predict(["Coach fare to the Leeds office"]))   # inference
  • TfidfVectorizer turns each description into numbers by counting words and weighting the ones that are distinctive across the set. Models work on numbers, so text has to be converted before anything can be learned from it.
  • LogisticRegression is the part that learns. It finds a weight for each word that best separates the categories in the examples it was given.
  • make_pipeline wires the two together so the same conversion is applied during training and during prediction. Applying different conversions in the two places is a classic source of results that look fine in a notebook and fail in production.
  • fit is training. predict is inference. After fit returns, the learned weights sit in memory and predict does arithmetic on them — no learning happens at prediction time.
  • Six examples is nowhere near enough for anything real. A usable classifier here would need thousands of past claims, and it would still be wrong sometimes. The sample is this small so you can see the whole mechanism at once.

The two activities, side by side:

 TrainingInference
What happensExamples go in and the model's internal numbers are adjustedOne input goes in and one prediction comes out
How oftenOccasionally — a batch job taking minutes, hours or longerOn every request, potentially thousands of times a day
Cost shapeA large cost paid in one goA small cost paid over and over
Does the model change?Yes — that is the point of itNo. The model is read-only here
With a hosted modelDone by the provider long before you call itThis is what your API call does

Two broad styles of learning, plus where language models sit:

Supervised learning
You supply inputs together with the correct answers. The expense example above is supervised: each description came with the category a human chose. Most machine learning used in business is this, and the labelling is usually the expensive part.
Unsupervised learning
You supply inputs with no answers and ask for structure. Grouping ten thousand support tickets into clusters nobody named in advance is unsupervised. It tells you what your data looks like; it does not tell you what to do about it.
Where language models come from
A language model is trained largely by predicting the next piece of text in an enormous amount of text. The text is its own answer key, which is why no labelling team was needed at that scale. Providers then tune the result further using human-rated responses so it follows instructions rather than continuing your sentence.

Summary

  • Ordinary code applies rules you wrote; machine learning derives behaviour from examples you supplied
  • Training builds the model and is a one-off cost; inference runs it and is a per-request cost
  • Supervised learning needs labelled examples, unsupervised learning finds structure without them
  • Language models are trained mostly by predicting the next piece of text, then tuned with human feedback
  • A model reproduces the habits and inconsistencies of its data, so examine the data before the algorithm

Practice

Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.

Think about it

Rules, or examples?

Your finance team wants three things automated: flagging claims over the 75 pound receipt threshold, deciding which of twelve budget codes a free-text claim belongs to, and detecting claims that look unusual for that employee.

For each, decide whether you would write ordinary code, use supervised learning, or use unsupervised learning. Say what you would need before you could start.

Show solution

The threshold is ordinary code. It is one comparison, it must be exact, and it changes when finance changes the policy — a number in configuration, not a model.

Budget codes are supervised learning, because the mapping from free text to a code is fuzzy and examples exist. Before starting you need a decent number of past claims with codes you trust, and you need to know how inconsistent the historic coding is.

Unusual claims lean unsupervised, because you cannot label something you have not seen. You would look for claims that sit far from that employee's normal pattern. Expect it to surface plenty of legitimate oddities — the useful output is a review queue, not a rejection.

The pattern worth carrying forward: exact rules belong in code, fuzzy mappings with labelled history suit supervised learning, and "show me what stands out" suits unsupervised learning.

Knowledge check

Nothing is recorded and there is no score. The explanation appears either way.

A team trains an expense classifier on 200 labelled claims and reports 99% accuracy. What is the most likely explanation?
Which statement about training and inference is accurate?

Saved in this browser only.