Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 01

What Is Machine Learning

Understand the three types of machine learning, the fit-predict workflow, and when to choose ML over rule-based code.

Intermediate25 min
Arrow from raw data through ML model to prediction

Traditional programs follow explicit rules you write. Machine learning programs instead learn rules from examples. You show the algorithm many inputs and their correct outputs, and it finds patterns on its own.

Three types of ML

Supervised learning trains on labelled examples, pairs of input and correct output. Predicting a salary from experience is regression; classifying an employee as senior or not is classification.

Unsupervised learning finds structure without labels. Grouping employees into clusters by behaviour is an example.

Reinforcement learning trains an agent by rewarding good actions and penalising bad ones. It is used in game-playing and robotics.

The fit and predict pattern

Every supervised estimator in scikit-learn has the same interface. fit learns from labelled training rows. predict applies what it learned to new rows. score measures how well the predictions match the true labels.

python
import numpy as np
import pandas as pd
from numpy.random import default_rng
rng = default_rng(42)
n = 300
experience = rng.integers(1, 21, n)
certifications = rng.integers(0, 7, n)
overtime = rng.integers(0, 45, n)
education = rng.choice([10, 12, 14, 16, 18], n, p=[0.05, 0.25, 0.35, 0.25, 0.10])
noise = rng.normal(0, 4500, n)
salary = (20000 + experience * 3200 + (education - 10) * 1500 +
          certifications * 1200 + overtime * 80 + noise).round(-2)
salary = np.clip(salary, 18000, 160000).astype(int)
senior = (salary >= 65650).astype(int)
top = ((experience >= 15) & (certifications >= 5)).astype(int)
employees = pd.DataFrame(dict(
    experience=experience, certifications=certifications,
    overtime=overtime, education=education, salary=salary,
    senior=senior, top=top,
))
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
X = employees[["experience", "certifications", "overtime", "education"]]
y = employees["senior"]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)
tree = DecisionTreeClassifier(max_depth=4, random_state=0)
tree.fit(X_train, y_train)
print(f"train accuracy: {tree.score(X_train, y_train):.3f}")
print(f"test  accuracy: {tree.score(X_test,  y_test):.3f}")

Output:

text
train accuracy: 0.950
test  accuracy: 0.883

ML vs rule-based code

Hand-written rules are fast to write but brittle. An ML model adapts when you retrain it on new data, and it can discover thresholds and combinations that a human might miss.

python
import numpy as np
import pandas as pd
from numpy.random import default_rng
rng = default_rng(42)
n = 300
experience = rng.integers(1, 21, n)
certifications = rng.integers(0, 7, n)
overtime = rng.integers(0, 45, n)
education = rng.choice([10, 12, 14, 16, 18], n, p=[0.05, 0.25, 0.35, 0.25, 0.10])
noise = rng.normal(0, 4500, n)
salary = (20000 + experience * 3200 + (education - 10) * 1500 +
          certifications * 1200 + overtime * 80 + noise).round(-2)
salary = np.clip(salary, 18000, 160000).astype(int)
senior = (salary >= 65650).astype(int)
top = ((experience >= 15) & (certifications >= 5)).astype(int)
employees = pd.DataFrame(dict(
    experience=experience, certifications=certifications,
    overtime=overtime, education=education, salary=salary,
    senior=senior, top=top,
))
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
X = employees[["experience", "certifications", "overtime", "education"]]
y = employees["senior"]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)
# Hand-written rule: more than 10 years AND more than 2 certs → senior
rules = ((X_test["experience"] > 10) & (X_test["certifications"] > 2)).astype(int)
rule_acc = (rules == y_test).mean()
# Learned model
tree = DecisionTreeClassifier(max_depth=4, random_state=0).fit(X_train, y_train)
print(f"rules:   {rule_acc:.3f}")
print(f"tree:    {tree.score(X_test, y_test):.3f}")

Output:

text
rules:   0.733
tree:    0.883

The tree finds better thresholds by itself. With more data or more features the advantage of the learned model grows.

When to use ML

Use ML when:

  • The patterns are too complex or numerous to write by hand.
  • The rules change as data changes.
  • You have enough labelled examples to train on.

Skip ML when the rules are simple and stable, or when you have fewer than a few hundred examples.

Key takeaways

  • ML learns rules from data instead of following rules you write

  • Supervised, unsupervised, and reinforcement are the three main types

  • fit learns from training rows; predict applies what was learned

  • ML wins when patterns are complex, numerous, or change over time

  • Previous: Saving and Loading Models

Loading playground…

Check your understanding

What Is Machine Learning Quiz

What does supervised learning require that unsupervised learning does not?

1 / 10