ML Playground
FundamentalsBeginnerCross-cutting

Fundamentals: metrics, overfitting, and data splitting

Available

Learn to split data, interpret metrics, and spot when a model is learning too little or memorizing too much.

Description

Qué haceIntroduces the tools used to tell whether a model works: data splitting, metrics, and controlling complexity.

Para qué sirveDistinguishing a model that generalizes from one that only looks good because it was evaluated with the wrong data or memorized the training set.

The core idea

A model isn’t evaluated by asking how much it remembers, but how well it responds to data it never saw. To check that, you need to split the data, pick an appropriate metric, and watch that complexity doesn’t turn learning into memorization.

Train, validation, and test

Training (train)

The model uses these examples to fit its parameters.

Validation

Lets you pick hyperparameters and complexity without looking at the final result.

Test

Used exactly once, at the end, to estimate how the model will perform on new data.

Data leakage

Happens when information from validation or test influences training. The result looks better than it actually is.

In this demo:Module 3 picks the polynomial degree by looking only at the validation error, never the test error.

How to read a classification

Accuracy

(TP+TN) / total

Overall proportion of correct predictions. Can be misleading if one class is much more frequent.

Precision

TP / (TP+FP)

Of everything the model flagged as positive, what proportion actually was.

Recall

TP / (TP+FN)

Of all the real positives, what proportion the model managed to find.

F1

2 × (precision × recall) / (precision + recall)

Summarizes precision and recall into one value; it will be low if either one is low.

Undefined, not zero:With a zero denominator (e.g. the model never predicted the positive class), the metric is shown as “undefined,” never as 0% or NaN.

Complexity and generalization

Underfitting · too simple

The model can't represent the pattern. It makes errors both in training and in validation.

Good balance

Error is low and similar in training and validation: what was learned transfers to new data.

Overfitting · too complex

The model memorizes details and noise. It performs very well while training but gets worse on validation data.

One split doesn't tell the whole story:The result can change depending on which samples land in each group. Cross-validation repeats this process over several splits to get a more stable estimate.

Playground

01

Split

Data splitting

Splits the dataset into three groups with different responsibilities. Test is reserved for the final evaluation and doesn't take part in any earlier decision.

Split result

90 synthetic samples · seed 7

Training
63
Validation
14
Test
13
Table of synthetic dataset samples with the split (training, validation, or test) assigned to each one.
#xySplit
0-3.911.67Training
11.592.04Test
2-0.27-1.65Training
31.843.09Training
42.112.53Training
5-1.06-3.18Test
6-1.63-2.60Training
73.021.52Training
8-2.76-2.21Training
92.093.49Validation
100.430.83Validation
113.730.37Training
122.622.25Test
13-1.14-1.58Training
14-3.30-0.69Validation
15-0.59-0.74Training
161.632.35Training
17-1.15-1.57Validation
183.300.71Validation
191.063.11Test
201.472.36Test
212.612.06Validation
22-3.06-1.17Training
233.130.75Training
240.751.94Training
250.391.36Training
260.130.19Training
270.401.45Validation
280.460.82Training
291.141.91Training
30-1.50-1.58Training
31-3.810.31Training
321.040.96Training
330.762.28Training
34-1.78-2.62Test
352.442.14Validation
36-2.89-1.15Training
370.540.41Training
38-2.31-1.80Training
391.893.05Test
400.872.44Training
41-0.09-1.11Training
422.372.10Training
430.440.38Training
44-2.56-2.59Test
45-0.180.66Test
46-2.61-0.29Training
473.010.97Training
48-2.60-2.39Training
491.861.98Training
502.332.18Training
510.240.27Training
520.500.32Training
532.212.17Training
542.611.63Training
55-0.56-0.63Training
56-1.99-3.27Training
572.642.32Training
580.410.63Training
590.301.46Test
600.581.47Test
61-0.03-0.34Training
623.45-0.19Training
63-2.70-1.82Training
640.881.75Training
65-1.64-3.14Training
661.862.46Training
67-1.92-2.74Training
683.410.42Training
693.700.90Validation
70-1.74-3.10Training
711.712.27Training
721.802.07Training
73-1.82-2.90Validation
74-2.88-1.57Training
750.481.69Training
763.620.89Validation
77-1.70-3.12Training
783.280.08Validation
793.26-0.52Training
803.410.37Test
81-1.33-2.26Training
822.742.37Training
832.023.51Validation
843.29-0.43Training
85-1.90-2.57Test
86-3.540.71Validation
87-3.34-1.30Training
882.311.98Training
89-0.02-1.31Training
02

Measure

Classification metrics

Change the matrix and see which question each metric answers. The imbalanced preset shows why high accuracy doesn't always mean the model is useful.

Classes of similar size; precision, recall, and F1 stay close to accuracy.

Rows are the model's predictions and columns are the actual values. Diagonal cells are hits; the other two are errors.
Predicted ↓ / Actual →PositiveNegative
Predicted positive
Predicted negative

Shortcut: Ctrl/Cmd+Z undoes the last confirmed edit (if focus isn't in a text field).

Metrics computed from the matrix

Accuracy
89.0%
Precision
88.2%
Recall
90.0%
F1
89.1%
Specificity
88.0%

Change the threshold

ROC and precision-recall curves

What is the threshold?

It's the minimum score to predict “positive.” Lowering it surfaces more positives: recall goes up, but false alarms can increase too.

What does each curve show?

ROC compares recall against false alarms. Precision-recall shows how much recall you get without losing precision and tends to be more informative with imbalanced classes.

ROC curve (false positive rate vs. true positive rate)0.00.30.50.81.00.00.30.50.81.0False positive rateTrue positive rate
ROC curve over 100 synthetic samples derived from the current matrix. AUC = 0.905.
Precision-recall curve0.00.30.50.81.00.00.30.50.81.0Recall (PR curve)Precision (PR curve)
Precision-recall curve over the same samples. Area under the curve (average precision) = 0.870.

Area under the curve

AUC-ROC
0.905

1.0 is perfect separation; 0.5 is equivalent to random ranking.

AUC-PR (average precision)
0.870

More sensitive than AUC-ROC when the positive class is the minority.

Note: the curves use 100 reproducible synthetic scores derived from the preset (45 TP, 6 FP, 5 FN, and 44 TN). At a 0.5 threshold they reproduce the matrix shown above.

03

Diagnose

Underfitting and overfitting

Increase the polynomial degree and compare training with validation. The goal isn't minimizing training error at any cost, but keeping a good result on unseen data.

3

Degree 1 draws a straight line. As the degree increases, the curve can fit more complex patterns — and noise too.

18
Polynomial fit over training and validation-4.7-2.4-0.12.24.5-4.0-1.90.12.14.2xy
63 training samples, 14 validation samples. Degree-3 polynomial curve fitted on the training set.
  • Training
  • Validation
  • Fit (degree 3)

Training vs. validation error

Training MSE
0.3768
Validation MSE
0.7222
Training R²
0.882
Validation R²
0.750

Diagnosis

Reasonable balance

Training and validation error are similar: the model generalizes reasonably to data it didn't see while fitting.

The test set (split off in the partitioning module) doesn't take part here: it's reserved for a single final evaluation after choosing the degree, not for deciding which degree to use.

Related concepts

References