AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Machine Learning Foundations: Teaching Computers

📚 Technology⏱️ 20 min read🎓 Grade 8
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 20 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

A Robot That Cannot Tell an Apple from an Orange

Imagine you are asked to program the sorting arm at a fruit-packing unit near Nagpur, which ships oranges and apples to markets across the country. The arm has two sensors: one weighs the fruit in grams, another measures its diameter in centimetres. Your job is simple, or so it seems: write a program that looks at the sensor readings and tells the arm "Apple" or "Orange."

Since you already know how apples and oranges roughly compare — apples in this batch tend to be a little smaller and lighter than the oranges — you write a rule directly:

def is_apple_rule_based(weight, diameter):
    if weight < 175 and diameter < 7.5:
        return "Apple"
    else:
        return "Orange"

This works for the first fifty fruits that pass through. Then an unripe orange arrives — still small because it was picked early, weighing 160 grams with a 7.3 cm diameter. Your rule checks weight < 175 (true, 160 < 175) and diameter < 7.5 (true, 7.3 < 7.5), so it confidently returns "Apple". It is wrong. To fix this one case, you would have to tighten the thresholds, but tightening them might now misclassify a large ripe apple. Every rule you hand-write to fix one mistake risks creating a new one somewhere else, because you are guessing at boundaries instead of actually knowing where they lie in the real data.

This is the situation machine learning was built for: problems where the correct rule is buried inside examples, not inside your head. Instead of you guessing the numbers 175 and 7.5, you let the computer discover better boundaries by examining many already-labelled fruits and working out, mathematically, which values actually separate apples from oranges.

Flipping the Problem: Data In, Rules Out

In ordinary programming, you supply two things and the computer produces the third:

  • Rules (the code you write) + Data (the input) → Output

In machine learning, you supply a different two things, and the computer produces the rules itself:

  • Data (many past examples) + Output (the correct answer for each example) → Rules (a model)

That flipped arrow is the entire idea. A machine learning model is a program whose internal rules were not typed in by a human but were fitted, through calculation, to a set of examples called training data. Each example is made of two parts:

  • Features — the measurable inputs you know about an example (a fruit's weight and diameter; a student's hours of study; a message's word content).
  • Label — the correct answer for that example, supplied by a human or by a trustworthy record (Apple/Orange; the marks a student actually scored; spam/not-spam).

When every training example comes with a label, the process is called supervised learning — "supervised" because a labelled answer key supervises the fitting process, the same way a teacher's answer key supervises a student checking their own practice paper. Supervised learning splits further by the type of label: when the label is a number (marks, price, rainfall in mm), it is called regression; when the label is a category (Apple/Orange, spam/not-spam), it is called classification. We will build one complete worked example of each, by hand, before writing a single line of a "black box" library.

Worked Example 1: Predicting Marks From Study Hours (Regression)

Suppose five students record their study hours before a unit test and their marks out of 100 afterward:

  • 1 hour → 38 marks
  • 2 hours → 44 marks
  • 3 hours → 58 marks
  • 4 hours → 60 marks
  • 5 hours → 78 marks

The marks generally rise with study hours but not in a perfectly straight line — real data never is. We want a model of the form marks = m × hours + c, where m is the slope (how many extra marks each extra hour of study is worth on average) and c is the intercept (a baseline score). "Training" this model means finding the specific m and c that fit these five points best.

The standard method, called least squares, finds the line that makes the sum of squared errors as small as possible. Here is how to compute it by hand, using only means and basic algebra:

Step 1 — find the mean of each list. Mean hours = (1+2+3+4+5)/5 = 15/5 = 3. Mean marks = (38+44+58+60+78)/5 = 278/5 = 55.6.

Step 2 — find how far each value is from its mean.

  • Hours deviations: −2, −1, 0, 1, 2
  • Marks deviations: −17.6, −11.6, 2.4, 4.4, 22.4

Step 3 — multiply matching deviations and add them up.

(−2)(−17.6) + (−1)(−11.6) + (0)(2.4) + (1)(4.4) + (2)(22.4) = 35.2 + 11.6 + 0 + 4.4 + 44.8 = 96

Step 4 — square each hours-deviation and add them up.

(−2)² + (−1)² + 0² + 1² + 2² = 4 + 1 + 0 + 1 + 4 = 10

Step 5 — divide. slope m = 96 ÷ 10 = 9.6. This means: on average, one extra hour of study is associated with about 9.6 extra marks in this data.

Step 6 — find the intercept using c = mean(marks) − m × mean(hours): c = 55.6 − 9.6 × 3 = 55.6 − 28.8 = 26.8.

Our trained model is marks = 9.6 × hours + 26.8. Notice this line does not pass exactly through any of the five original points — and that is not a flaw. It is the whole point: the model captures the overall trend rather than memorising five specific answers.

From Formula to Program

The six hand-steps above translate directly into code. Nothing here is a library call — every line does exactly the arithmetic we just did, so you can trust what it prints:

def train_linear_model(x_values, y_values):
    n = len(x_values)
    x_mean = sum(x_values) / n
    y_mean = sum(y_values) / n

    numerator = 0
    denominator = 0
    for i in range(n):
        numerator += (x_values[i] - x_mean) * (y_values[i] - y_mean)
        denominator += (x_values[i] - x_mean) ** 2

    slope = numerator / denominator
    intercept = y_mean - slope * x_mean
    return slope, intercept

hours = [1, 2, 3, 4, 5]
marks = [38, 44, 58, 60, 78]

m, c = train_linear_model(hours, marks)
print(m, c)          # 9.6 26.8

def predict(hours_studied, slope, intercept):
    return slope * hours_studied + intercept

print(predict(6, m, c))   # 84.4

Tracing it: x_mean becomes 3.0, y_mean becomes 55.6. The loop accumulates numerator to 96 and denominator to 10, exactly as in our hand calculation, so slope is 9.6 and intercept is 26.8. Calling predict(6, m, c) computes 9.6 × 6 + 26.8 = 57.6 + 26.8 = 84.4 — the model's estimate for a student who studies 6 hours, even though 6 hours never appeared in the training data. This ability to answer for inputs it has never seen, called generalisation, is what separates a trained model from a lookup table.

What "Training" Really Means

Why is the least-squares line better than some other guess? Define the error for one example as (actual − predicted), and a natural way to measure the total error of a whole line as the sum of squared errors (SSE) — squaring makes every error positive so overshoots and undershoots do not cancel out, and it punishes large errors more heavily than small ones.

For our trained line (m = 9.6, c = 26.8), the predictions are 36.4, 46, 55.6, 65.2, 74.8, giving errors of 1.6, −2, 2.4, −5.2, 3.2. Squaring and adding: 2.56 + 4 + 5.76 + 27.04 + 10.24 = 49.6.

Compare that to a plausible but untrained guess, say m = 5, c = 35. Predictions become 40, 45, 50, 55, 60, giving errors of −2, −1, 8, 5, 18, and a squared sum of 4 + 1 + 64 + 25 + 324 = 418. Our least-squares line's SSE of 49.6 is far lower — it fits the real trend much better. This SSE number is an example of a cost function (also called a loss function): a single score that tells you how wrong a model currently is, so that "training" simply means searching for the slope and intercept that make the cost function as small as possible. For a straight line, the least-squares formula finds that minimum directly, in one calculation, as we did above.

Most real machine learning problems, though, involve dozens or millions of features, and there is no neat one-step formula. Those models are trained by an iterative method called gradient descent, which behaves like a "hot-and-cold" search game: start with a random guess for each number the model needs, calculate the cost, nudge one number slightly and recalculate the cost, keep the nudge if the cost went down (getting "warmer"), reverse it if the cost went up (getting "colder"), and repeat this thousands of times until the cost stops improving. It is slower than a direct formula, but it works for models far too complex for algebra to solve in one step, which is why it is the workhorse behind most modern machine learning systems.

Worked Example 2: Sorting Fruit With Nearest Neighbours (Classification)

Return to the fruit-sorting arm from the introduction, but now let's train it instead of hand-writing thresholds. We collect eight already-labelled fruits and record just their diameter in centimetres (their true label is known because a worker weighed and measured each one by hand):

  • Apples: 7.0 cm, 7.5 cm, 6.8 cm, 6.5 cm
  • Oranges: 8.5 cm, 8.0 cm, 8.2 cm, 8.7 cm

A new fruit arrives with diameter 7.2 cm. A simple and surprisingly effective classification method called k-nearest neighbours (k-NN) says: find the k training examples closest to the new one, and let them vote on the label by majority. With k = 3, we compute the distance (here, just the absolute difference in diameter) from 7.2 to every training example:

  • 7.0 → |7.2 − 7.0| = 0.2 (Apple)
  • 7.5 → |7.2 − 7.5| = 0.3 (Apple)
  • 6.8 → |7.2 − 6.8| = 0.4 (Apple)
  • 6.5 → |7.2 − 6.5| = 0.7 (Apple)
  • 8.0 → |7.2 − 8.0| = 0.8 (Orange)
  • 8.2 → |7.2 − 8.2| = 1.0 (Orange)
  • 8.5 → |7.2 − 8.5| = 1.3 (Orange)
  • 8.7 → |7.2 − 8.7| = 1.5 (Orange)

Sorted by distance, the three closest are 0.2, 0.3, and 0.4 — all three labelled Apple. The vote is unanimous: the model classifies the new fruit as Apple. Notice this model never needed a hand-picked threshold like "175 grams" at all; it simply asked "which labelled examples does this new one resemble most closely?"

def knn_classify(new_diameter, training_data, k=3):
    distances = []
    for diameter, label in training_data:
        distance = abs(new_diameter - diameter)
        distances.append((distance, label))
    distances.sort()

    nearest = distances[:k]
    votes = {}
    for distance, label in nearest:
        votes[label] = votes.get(label, 0) + 1

    best_label = max(votes, key=votes.get)
    return best_label

training_data = [
    (7.0, "Apple"), (7.5, "Apple"), (6.8, "Apple"), (6.5, "Apple"),
    (8.5, "Orange"), (8.0, "Orange"), (8.2, "Orange"), (8.7, "Orange"),
]

print(knn_classify(7.2, training_data, k=3))   # Apple

Tracing it: distances fills with the eight (distance, label) pairs computed above; .sort() orders tuples by their first element, so distance, giving 0.2 Apple first; slicing [:3] keeps the three nearest; the voting loop builds votes = {"Apple": 3}; and max(votes, key=votes.get) returns the dictionary key with the highest vote count, which is "Apple". The printed result matches our hand calculation exactly.

Least-Squares Line: marks = 9.6 × hours + 26.8 1 2 3 4 5 Hours Studied 0 20 40 60 80 Marks (out of 100) Actual marks Trained line Error (residual)
Each blue dot is one student's real result; the orange line is the trained model; the dashed green segments are the leftover errors the least-squares method minimises.

Common Misconception: "Zero Errors on Training Data Means the Best Model"

A very natural but incorrect belief is that the best model is whichever one gets every single training example exactly right. Suppose, instead of a straight line, we fit a wildly bendy curve through our five study-hours points so it touches each one precisely — training error becomes exactly zero. This sounds impressive, but it is usually a trap called overfitting: the curve has bent itself around the specific noise in these five particular students' results rather than learning the genuine relationship between studying and scoring. Ask that same overfit curve to predict marks for a sixth student who studied 3.5 hours, and it can swing to an absurd value, because it was never forced to be sensible between the points it memorised — only to be exact at them.

This is exactly like preparing for your CBSE board exam by memorising the model answers to last year's question paper word for word, instead of understanding the underlying concept. You would score perfectly if the exact same paper repeated, but the moment the examiner rephrases a question or changes the numbers in a numerical problem, memorisation collapses while genuine understanding still works. The fix used throughout real machine learning is a train/test split: hold back a portion of the labelled examples (say the last one or two) and never let the model see them while it is being fitted. After training, measure the error only on this held-out test set. A model that does well on training data but poorly on the test set is overfitting; a model that does reasonably well on both has actually learned the pattern, not just the answer key. Our five-point least-squares line, precisely because it does not pass exactly through every point, is behaving exactly as a well-fit model should.

Where Indian Systems Use These Same Ideas

The vocabulary you have just learned — features, labels, training data, minimising a cost function — is not classroom-only language. A bank's transaction-fraud system represents every UPI payment as a feature vector: amount, time of day, typical spending pattern for that account, distance from the user's usual location, and so on. It is trained on millions of past transactions that human investigators have already labelled "genuine" or "fraudulent," and it learns, the same way our tiny fruit example did, which combinations of feature values tend to go with which label — just with far more features and a far larger training set than we used by hand.

Check Your Understanding

1. A different batch of three students studied for 2, 4, and 6 hours and scored 50, 70, and 90 marks. Compute the least-squares slope and intercept by hand, the way we did above.

Answer: mean hours = 4, mean marks = 70. Deviations in hours: −2, 0, 2. Deviations in marks: −20, 0, 20. Products: 40, 0, 40, summing to 80. Squared hour-deviations: 4, 0, 4, summing to 8. Slope = 80 ÷ 8 = 10. Intercept = 70 − 10 × 4 = 30. Model: marks = 10 × hours + 30.

2. Using the fruit training data from Worked Example 2, classify a new fruit with diameter 8.3 cm using k-NN with k = 3.

Answer: distances are 1.3, 0.8, 1.5, 1.8 to the four apples and 0.2, 0.3, 0.1, 0.4 to the four oranges. The three smallest distances are 0.1, 0.2, and 0.3, all belonging to oranges, so the model classifies it as Orange.

3. A classmate says their handwriting-recognition model is excellent because it correctly reads every single sample in its training set. What should you check before agreeing?

Answer: Check its accuracy on a separate test set of handwriting the model never saw during training. Perfect training accuracy alone could just as easily mean overfitting as genuine skill.

4. Why is monthly rainfall a poor candidate for a simple straight-line regression model, even though "month number vs. rainfall" is two columns of numbers just like "hours vs. marks"?

Answer: Rainfall in India rises sharply during the monsoon months and falls afterward — it is cyclical, not a steady increase or decrease. A straight line assumes a constant rate of change in one direction, so it cannot represent a pattern that rises then falls within the same year; a different, non-linear or seasonal model is needed.

Summary

  • Traditional programming supplies rules and data to produce output; machine learning supplies data and known outputs (labels) to let the computer produce the rules, called a model.
  • In supervised learning, each training example has features (measured inputs) and a label (the correct answer). A numeric label makes it regression; a categorical label makes it classification.
  • Least-squares regression finds the line minimising the sum of squared errors directly, using means and deviations — for our example, marks = 9.6 × hours + 26.8.
  • A cost function (like sum of squared errors) measures how wrong a model is; training means searching for the parameters that make it smallest. Gradient descent does this search step by step for models too complex for a direct formula.
  • k-nearest neighbours classifies a new example by letting its closest labelled examples vote — no hand-written thresholds required.
  • Zero error on training data is not proof of a good model; it can signal overfitting. Always check performance on a held-out test set the model never trained on.
← CI/CD Pipelines: Automating Code DeploymentBlockchain →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn