The Blade It Never Saw Crack

Learning to CompressMedium+20 XP+20

The Blade It Never Saw Crack

A wind farm fits a row of strain gauges along every turbine blade. Cracks are rare and nobody has ever labelled one, so the team trained an autoencoder on months of gauge readings from blades in good condition. Each reading is a short list of strain values, and the autoencoder returns its rebuilt version of it.

The plan is the one from the lesson: a reading the model cannot rebuild well is something it has never seen, so it earns a visit from an inspector. The only open question is how badly rebuilt counts as badly, and the team answers it with a held-out set of healthy readings the model was not trained on.

A reading's error is its reconstruction loss: subtract the rebuilt reading from the original position by position, square, and average over the positions. Compute the error of every healthy reading; if mmm is the mean of those errors and σ\sigmaσ their standard deviation, the alarm threshold is

threshold=m+k σ\text{threshold} = m + k\,\sigmathreshold=m+kσ

Task: write flag_anomalies(healthy, healthy_rebuilt, incoming, incoming_rebuilt, k).

  • healthy holds the held-out normal readings and healthy_rebuilt their reconstructions. The threshold is built from these alone.
  • σ\sigmaσ divides by the number of healthy readings, not by one fewer.
  • incoming holds new readings and incoming_rebuilt their reconstructions. An incoming reading is flagged when its error is strictly greater than the threshold.
  • Return [threshold, flagged]: the threshold rounded to 4 decimal places, then a list of the 0-based positions of the flagged incoming readings in increasing order (empty if none). Compare errors against the unrounded threshold.
  • Every reading in one call has the same length, and neither set is empty.

Nothing in this procedure has ever seen a cracked blade. The monitor works because the autoencoder only learned to rebuild healthy strain patterns well; it has no idea what a crack looks like, and it does not need one.

reconstruction error
0.04
looks normal

Train only on normal data. Anything the model rebuilds badly is something it has never seen — no anomaly labels required.

Examples

healthy = [[1.2, 0.8, 1.5, 0.9], [1.0, 1.1, 0.7, 1.3], [0.9, 1.4, 1.2, 1.0], [1.3, 0.6, 1.1, 1.2]]
healthy_rebuilt = [[1.1, 0.9, 1.5, 0.7], [0.8, 1.1, 0.9, 1.3], [0.9, 1.3, 1.1, 1.1], [1.6, 0.5, 1.1, 1.2]]
incoming = [[1.1, 0.9, 1.4, 2.6], [1.6, 0.5, 1.7, 0.4], [1.0, 1.2, 0.8, 1.1]]
incoming_rebuilt = [[1.1, 0.9, 1.4, 2.1], [1.45, 0.65, 1.55, 0.55], [0.9, 1.2, 0.7, 1.1]]
k = 3
returns [0.0363, [0]]
healthy = [[2.0, 1.5, 3.0], [2.2, 1.4, 2.8], [1.9, 1.6, 3.1], [2.1, 1.5, 2.9], [2.0, 1.7, 3.0]]
healthy_rebuilt = [[1.9, 1.5, 3.1], [2.2, 1.2, 2.8], [2.0, 1.5, 3.0], [1.9, 1.6, 2.9], [2.0, 1.7, 2.7]]
incoming = [[2.0, 1.5, 3.0], [3.5, 0.2, 4.4], [2.1, 1.6, 3.0], [2.0, 1.5, 4.1]]
incoming_rebuilt = [[1.9, 1.4, 3.0], [2.9, 0.8, 3.8], [1.9, 1.5, 3.2], [2.0, 1.5, 3.0]]
k = 2
returns [0.0314, [1, 3]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run