☰ Machine Learning Tutorial Menu

What is Machine Learning?

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


Ask a Daraz seller how many phone covers to stock before Eid and he'll give you a number on the spot. Ask him how he got it and he'll shrug and say "experience". A Karachi bank's fraud desk does the same with suspicious transactions, and K-Electric's planners do it with next month's load. Every one of those guesses is a rule living in somebody's head. Machine learning is the practice of letting a computer work those rules out from past data instead of a person writing them down. By the end of this lesson you'll have fitted a model with nothing more than a calculator, and you'll know the six words we use in every later lesson.

Two ways to get a rule

In ordinary programming a developer writes the rule. A fraud rule might say if the amount is above PKR 50,000 and the card is new, flag it. Easy to read, easy to audit. But a person had to think of it, and it never gets better on its own.

Machine learning flips the picture. You hand the algorithm thousands of past transactions with the known answer attached (fraud or not), and it works out which mixes of amount, time, merchant and history go with fraud. What comes out is a model, a set of learned numbers you can apply to tomorrow's transactions. Students often expect something more mysterious than that. There isn't anything more.

Traditional programming takes rules and data and gives answers; machine learning takes data and answers and gives rules

The vocabulary you need

TermMeaningExample (online shop)
Row / sampleOne example the model learns fromOne order
Feature (X)An input column used to predictdistance in km, order weight, hour of day
Label / target (y)The answer we want to predictdelivery time in minutes
TrainingFinding the model's numbers from rows where the label is knownfit on last month's 8,000 orders
Prediction / inferenceApplying the model to a new row"this order will take 42 minutes"
Error / lossHow far predictions are from the truthpredicted 42, actual 50, error 8

So "learning" just means nudging the model's numbers until the error on the training rows gets smaller. Interviewers in Karachi like to ask "what does training actually do?" and that one sentence is the answer they want.

What a model looks like once it's live

A model isn't a report you email once. It sits inside a loop. Old data trains it, it scores new cases, someone acts on the score, the real outcome gets recorded, and that outcome becomes next month's training data. If any link is missing (usually the "record the outcome" one), the model quietly goes stale and nobody notices for months.

Historical data trains a model that makes predictions used for decisions, and outcomes feed back as new data

A model you can fit on paper

A rider service in Lahore wants to predict delivery minutes from distance. Five past deliveries:

OrderDistance (km)MinutesMinutes per km
12147.0
24266.5
35346.8
48506.25
510666.6

The simplest model is "minutes = rate x km", and the only thing to learn is the rate. Average the last column: (7.0 + 6.5 + 6.8 + 6.25 + 6.6) / 5 = 6.63 minutes per km. That one number is your trained model. A new 6 km order gets 6 x 6.63 = 39.8 minutes.

Now check it against the rows it learned from. Order 4 is predicted at 8 x 6.63 = 53.0 but took 50, so the error is +3.0. Later lessons add an intercept for pickup time, a proper least-squares fit and a held-out test set, but the idea is already complete. Data in, parameter out, prediction from parameter.

The same five rows in scikit-learn

scikit-learn is the library you'll use for classical ML in almost every job. Run this locally after pip install scikit-learn.

from sklearn.linear_model import LinearRegression

X = [[2], [4], [5], [8], [10]]      # features: distance in km
y = [14, 26, 34, 50, 66]            # labels: minutes

model = LinearRegression(fit_intercept=False).fit(X, y)
print(round(model.coef_[0], 2))     # learned minutes per km (about 6.6)
print(model.predict([[6]]))         # prediction for a 6 km order

When we tell clients not to use ML

Use ML whenDo not bother when
The rule is too complex to write by hand (fraud, demand across 5,000 products)A clear business rule exists ("orders above PKR 100,000 need manager approval")
You have hundreds or thousands of past examples with known outcomesYou have 20 rows or no recorded outcomes
The pattern is stable enough that the past predicts the futureThe situation is brand new (a product launched yesterday)
A slightly wrong prediction is acceptableEvery mistake is catastrophic and unexplainable answers are unacceptable

How a Lahore accessories seller stopped guessing: A Daraz seller we worked with sells around 300 mobile accessories. He ordered stock by gut feel and lost money twice a year, dead stock after Eid and stock-outs during the 11.11 sale. His analyst exported two years of daily sales and built a plain model that predicts next week's units per product from the last four weeks, the price and a "sale event" flag. Stock-outs fell by about a third. Nothing fancy happened. A guess was replaced by a learned pattern that got checked against real outcomes every week.

If you remember one thing

ML learns rules from examples, where traditional code has rules written by people. Features go in, the label is the answer, and training finds the parameters that shrink the error. Even a single averaged number counts as a model; you add complexity only when it earns its place. In production it's a loop (train, predict, act, record, retrain), which is why you need enough labelled history and a pattern that stays stable. If you don't have those, write a rule and move on. If you want the full classroom version of this track, our course list shows where it fits.

Try this before the next lesson

  1. A Rawalpindi school wants to predict which students will fail the matric exam. List three features, the label, and where the training rows would come from.
  2. Add a sixth delivery to the table (12 km, 82 minutes). Recompute the average rate and the 6 km prediction. Did it move much?
  3. Name one decision in a business you know that should stay a hand-written rule, and one that would gain from a learned model. Two sentences each.

Lesson 1 of 18

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