AI Run Lab

All chapters

Which classical model fits the task?

A dataset with rows and columns can support several kinds of prediction. Match the model to the question, then compare it with a simple baseline.

Open this chapter with experiments and audio

A score can become a class probability

Suppose an email filter records a count of suspicious links and a sender-reputation score. Logistic regression assigns a weighted sum to these features. Its sigmoid function maps that sum to a value between zero and one. One toy version has score = −2 + 0.8 × link count. With four links the score is 1.2 and the corresponding probability is about 0.769.

from math import exp
for links in [0, 2, 4, 6]:
    score = -2 + 0.8 * links
    probability = 1 / (1 + exp(-score))
    print(links, round(probability, 3), probability >= 0.5)

The threshold 0.5 turns a score into a decision. You can raise it to send fewer messages to a spam folder without retraining the model. Whether 0.769 really means that roughly 77% of similar messages are spam is a separate calibration question, answered with labelled evaluation data.

Keep four links in the message and raise the threshold to 0.8. The decision changes while the score and probability stay fixed. You have changed the application’s decision rule. Now hold the threshold steady and change the link count. This time the input itself changes, so a different score is computed.

How would a tree use the same information?

A decision tree can first ask whether link count exceeds three. For messages that do, it can ask whether the sender is known. Each final branch stores a prediction based on its training examples. A tree can therefore represent an exception in a small region of feature space. Deep branches containing very few examples are particularly sensitive to accidental patterns.

MessageExample pathDecision in this toy tree
2 links, new senderAt most 3 linksKeep
5 links, known senderMore than 3 → knownKeep
5 links, new senderMore than 3 → newFlag

A random forest trains multiple trees using randomness in examples and feature choices and combines their predictions. Averaging can reduce the effect of one unusual split. It also makes a single path an incomplete explanation of the combined prediction.

What if the groups are unknown?

K-means clustering starts from candidate centres, assigns each example to the nearest centre, and replaces each centre by the average of its assigned examples. It repeats these steps until the assignments stabilize or a limit is reached. The chosen number of centres, initial positions and feature scaling can all influence the result.

For one-dimensional values [1, 2, 9, 10] and initial centres 1 and 10, the first assignments are [1, 2] and [9, 10]. The updated centres are 1.5 and 9.5. Those new centres produce the same split. The algorithm found compact groups here, but only a human or a separate evaluation can determine what those groups represent.

Set both centres to the same position. Under the tie rule shown here every observation goes to the first group. There is no second group mean to compute. Practical algorithms need an explicit policy for this situation, for example choosing a new centre for an empty group.

What should you compare first?

Start with a baseline such as always selecting the most frequent class. Train competing models on the same examples and compare them on the same held-out split. A support vector machine, for example, can seek a separating boundary with a large margin. Its value for your task depends on measured errors, training cost and feature choices. The algorithm name alone is insufficient evidence.

Would the names of the clusters automatically identify spam?

No. Cluster IDs can even be permuted without changing the grouping. Spam labels or careful inspection are needed to assign that meaning.

scikit-learn · Supervised learning

scikit-learn · Clustering

Can a straight boundary detect disagreement?

A machine has two on/off sensors. An inspection flag should turn on when exactly one sensor is on. With the input order [left, right], the labels are [0, 0] → off, [1, 0] → on, [0, 1] → on and [1, 1] → off. Plot the four points at the corners of a square. The two “on” corners sit opposite one another. This pattern is often called exclusive OR, or XOR.

Try a score a × left + b × right + c and switch the flag on for positive scores. To keep [0, 0] off, c must be at most zero. To turn each one-sensor case on, a + c and b + c must both be positive. Adding these gives a + b + 2c greater than zero. Since c is at most zero, a + b + c must be positive too. That forces [1, 1] on. No amount of extra training can correct that contradiction within this family of scores.

Can a tree with two questions represent the rule?

First ask whether the left sensor is on. If it is off, output the right sensor. If it is on, output the opposite of the right sensor. This covers all four cases. Representing a solution and discovering it from data are separate questions. For example, a greedy tree learner that disallows zero-gain splits can stop at the first node when the four cases occur equally often.

Build the same disagreement flag from two rectified units →

Further reading · scikit-learn · Decision trees