Back

Machine Learning Courses · Lesson ML.1

What Is Machine Learning?

CODE SCOPE MACHINE LEARNING SERIESLESSON ML.1 OF ML.1–ML.10

Machine learning is not magic — it is learning patterns from examples instead of

Free PDF of this lesson — save it, print it, revise offline. The video is the same lesson on the Code Scope YouTube channel.

Introduction

Imagine you want a program that tells spam emails apart from real ones. The old way is rules: you write hundreds of rules by hand — "if the email says FREE MONEY, mark it spam". But spammers change their words, and your rules break every week. So you keep adding rules forever, and the program still misses new tricks.

Machine learning takes a different road. Instead of writing rules, you show the program many examples: thousands of emails already marked "spam" or "not spam". The program studies those examples and finds the patterns itself — which words, senders and styles usually mean spam. Then, when a new email arrives, it guesses from the patterns it learned. That is the whole idea: learn from examples, not from hand-written rules.

You already meet machine learning every day. When your phone keyboard suggests your next word, when a shopping app shows "you may also like", when your camera finds faces in a photo, when a video app recommends the next video — a model learned those patterns from examples. It is powerful, but it is not magic and it is not thinking like a human. It only repeats patterns it saw in its training examples.

In this lesson you will learn the difference between rules and learning, see one spam-filter example step by step, and learn why a model is only as good as its examples.

Learning also has a simple rhythm that repeats: guess, check, adjust. The model first guesses an answer, then compares its guess with the true label, then adjusts its patterns a little so the next guess is closer. It repeats this thousands of times over the training examples. That loop — guess, check, adjust, repeat — is what "training" really means. It is also why a model improves slowly instead of understanding in one go, like a student who gets better by practising many sums, not by reading the answer once.

A second point beginners often miss: the model only learns what the examples contain. If every spam example in training is in English, the model has no pattern for spam in Urdu, and it will guess badly there. It is not being lazy; it simply never saw that pattern. So when a model fails, the first question is never "is the model clever enough?" It is "did the training examples cover this kind of input?"

(Roman Urdu: Machine learning ka matlab hai misaalon se seekhna — program ko rules likh kar dene ke bajaye examples dikhao, aur woh khud pattern pehchan le.)

Key points

  • Traditional programming = rules written by a human. The programmer writes every "if this, then that". It works until the world changes and the rules go out of date.
  • Machine learning = patterns learned from examples. You give the program data with answers, and it finds the patterns that connect inputs to answers.
  • A model is what the program learns. Think of it as the finished "brain" of the program: the stored patterns it uses to guess on new, unseen inputs.
  • Training is the learning step. Showing examples to the program and letting it adjust its patterns is called training. The examples used for this are the training data.
  • Prediction is the guessing step. After training, you give the model a new input it has never seen, and it predicts an answer — spam or not spam, a price, a category.
  • ML is not magic and not human thinking. A model does not understand email, it does not know what spam means. It only knows that certain patterns appeared with the "spam" label in its examples.
  • Bad examples = bad model. If the training emails were all from one year, or all marked by one careless person, the model learns those flaws too. Data quality decides everything.
  • Training is a loop: guess, check, adjust, repeat. The model improves a little on every pass over the examples; it does not understand in one step.
  • One model type can serve many jobs. The same learning idea — patterns from examples — works for spam, recommendations and face unlock; only the examples and the labels change.

Examples

Example 1 — Spam filter: rules vs learning

Rules approach: you write "if subject contains 'lottery', mark spam". Next week spammers write "l0ttery" and your rule fails. You patch it, they change again. You are always one step behind.

Learning approach: you collect 10,000 emails, each labelled spam or not spam. The model notices that emails with words like "lottery", "free prize" and "click now" are spam in 9 out of 10 cases, while emails from your friends' addresses almost never are. Nobody wrote those rules — the model counted the patterns. When a new email arrives, it weighs all the patterns together and guesses.

Example 2 — ML in your daily life

  • Keyboard suggestions: your phone learned which words you usually type after other words, from your own typing history.
  • Face unlock: your phone learned the pattern of your face from the photos taken when you set it up.
  • Video recommendations: the app learned that people who watched video A usually also watched video B.
  • Voice assistants: they learned to turn sound patterns into words from millions of recorded voice examples.

In every case, the recipe is the same: lots of examples + a learning method = a model that guesses on new inputs.

Example 3 — What ML cannot do (yet)

Give a spam model an email written in a language it never saw in training, and it will guess badly — not because it is stupid, but because it has no patterns for that language. A model cannot go beyond its examples. If the world changes, or a new kind of spam appears, the model needs new examples and fresh training.

Example 4 — Worked example: counting words to spot spam

Take 100 spam emails and 100 normal emails, and count how many contain the word "free":

| Word | In spam (out of 100) | In normal mail (out of 100) | |---|---|---| | "free" | 90 | 5 | | "prize" | 70 | 2 | | "meeting" | 10 | 60 |

A new email contains "free" and "prize" but not "meeting". The model weighs the evidence: two strong spam signals and no normal-mail signal, so it predicts spam. Change the email to contain only "meeting", and the same counting predicts normal mail. This is the whole trick in miniature: count patterns in labelled examples, then weigh them on a new input. Real models count thousands of such signals at once, but the idea is exactly this table.

Example 5 — Worked example: the same counting, in code (no library needed)

You can do the counting above with plain Python, no machine learning library at all:

spam = ["free prize click now", "free lottery winner", "meeting tomorrow"]
normal = ["meeting tomorrow", "free lunch on Friday", "project meeting notes"]

def count(word, emails):
    return sum(1 for e in emails if word in e)

for word in ["free", "prize", "meeting"]:
    print(word, "spam:", count(word, spam), "normal:", count(word, normal))

The output shows "prize" only in spam, "meeting" mostly in normal mail, and "free" in both — so "free" alone is weak evidence while "prize" is strong. A real spam model does this counting over thousands of words and learns how much weight each word deserves. Try it: add your own example emails to both lists and watch the counts change.

Real-world case — How banks catch card fraud

Banks train fraud models on millions of past card payments, each labelled "fraud" or "genuine". The patterns are things like a payment far from home, a very large amount at 3 a.m., or five purchases in ten minutes. When your card is blocked after an unusual purchase, that is usually such a model flagging a pattern, not a person watching you. And when fraudsters change their tricks, the bank must retrain on the new fraud examples — exactly like the spam filter in Example 1. The model never "understands" crime; it only recognises patterns it has seen before.

Quick recap

  • Rules are written by hand; machine learning learns patterns from examples.
  • Training = learning from labelled examples. Prediction = guessing on new inputs.
  • A model is the stored set of patterns learned during training.
  • You meet ML daily: keyboards, recommendations, face unlock, voice assistants.
  • ML is not magic: it cannot understand, it only repeats patterns from its data.

Common mistakes

  1. Thinking ML "understands" like a human — it does not; it matches patterns. Fix: always ask "what examples did this model learn from?" before trusting its answer.
  2. Believing more data automatically means a better model — bad or biased data teaches bad patterns. Fix: check that your examples are correct, varied and recent.
  3. Expecting a model to handle things it never saw — a model trained only on English emails will fail on other languages. Fix: train on examples that look like the real inputs it will face.
  1. Thinking a model works with no examples — a model with no training data has nothing to learn from, so it can only guess randomly. Fix: always ask what examples it trained on, and how many.
  2. Judging a model from one prediction — one right or wrong answer proves nothing. Fix: test on many new examples and look at the overall pattern of right and wrong answers.

Practice

  1. List three apps you used today and write, in one line each, what examples each one probably learned from.
  2. Pick a daily-life decision — for example, deciding if it will rain — and write down which "rules" a person would use and which "examples" a model would need.
  3. Think of one task where rules are better than learning (for example, a calculator). Write one sentence explaining why.
  1. Collect five emails from your own inbox (do not share them anywhere) and, for each one, write down three patterns a spam model might notice: sender, subject words, and time of day.
  2. Think of a model that recommends videos to you. Write down two examples it probably learned from, and one kind of video it would guess badly about because it never saw that kind in training.
  3. Explain to a friend, in three sentences, why a spam filter trained in 2020 might fail on 2026 spam. Use the words "examples" and "patterns" in your answer.

Practice quiz

Tap an answer to check it — a right answer turns green, a wrong one turns red and shows the right answer. Nothing is scored or saved; this is just for practice.

1. How is machine learning different from normal programming?

2. How does a spam filter learn?

3. Is machine learning magic?