Menu

AI & Machine Learning Topic

Supervised Learning: Predict from Labelled Examples

In supervised learning, the training data includes inputs and known answers. A model learns a relationship that may help with new examples. For a flower, the inputs might be measurements and the answer might be its species.

Two common tasks

Regression predicts a number, such as travel time. Classification predicts a class, such as spam or not spam. A model's name can be confusing: logistic regression is commonly used for classification.

Choose the task from the decision you need to support. A prediction is not automatically a decision. You may need a threshold, a human review step or an option to say the model does not know.

Three model families

Linear regression combines weighted inputs to predict a number. It is a useful baseline when that form is reasonable. It does not make a complex relationship linear just because you fit it.

Logistic regression uses weighted inputs to produce class scores or probabilities. It is often a strong first classifier, especially with suitable features. Check probability calibration before relying on its confidence.

Decision trees split data using questions about features. They can capture non-linear relationships. A deep tree can fit the training data too closely. Limit its complexity and evaluate on separate data.

A complete flower example

Install scikit-learn in your practice environment. Save this as supervised.py. Iris is a small built-in teaching dataset with four flower measurements and three species. It is useful for learning the process, not for proving production performance.

from sklearn.datasets import load_iris
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

def train_demo():
    data = load_iris()
    X_train, X_test, y_train, y_test = train_test_split(
        data.data, data.target, test_size=0.25, stratify=data.target, random_state=42)
    model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=500))
    baseline = DummyClassifier(strategy="most_frequent")
    model.fit(X_train, y_train)
    baseline.fit(X_train, y_train)
    return model, baseline, X_test, y_test, data.target_names

if __name__ == "__main__":
    model, baseline, X_test, y_test, names = train_demo()
    print("Baseline accuracy:", round(accuracy_score(y_test, baseline.predict(X_test)), 3))
    print("Model accuracy:", round(accuracy_score(y_test, model.predict(X_test)), 3))
    print("First predicted flower:", names[model.predict(X_test[:1])[0]])
python supervised.py

The script prints baseline and model accuracy, followed by a predicted flower name. The most-frequent baseline should score about one third. The model should do better on this split. Do not treat one high score on an easy dataset as evidence that a different task will work.

Why use a baseline?

A baseline is a simple comparison, such as always predicting the common class. If your model cannot beat a reasonable baseline, investigate before making it bigger. Sometimes a clear rule or a simple statistical estimate is enough.

The scaler is inside the pipeline, so it learns only from training rows. Stratify keeps class proportions roughly similar in this random split. A fixed random seed helps repeat this exercise. It does not remove uncertainty about other data.

From score to useful behaviour

Inspect which classes are confused. Check the cost of different mistakes. A missed dangerous fault may matter more than an unnecessary manual check. Use a metric and decision policy that reflect those costs, and examine results for relevant groups.

If rows are related or time-ordered, change the split method. Keep a final test set separate from repeated model choices. Read the evaluation guide for that workflow.

Practice

Replace the classifier with a shallow DecisionTreeClassifier using max_depth=3 and random_state=42. Compare the same held-out results and explain the trade-off. Do not keep choosing settings from the test score; use validation or cross-validation for that.

Official references

Reviewed on 25 October 2026. Use documentation for your installed versions.

All AI & Machine Learning tutorials

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?