Wine factory production line (14:32–15:53, rapid NIR/FTIR QC) ↗
Today’s task: prepare a series of acetic acid standards of known concentration, measure their NIR spectra, use PLS regression to “translate” spectra into concentration, and learn to choose model complexity by cross-validation and report performance honestly on an independent test set.
Full narration and captions; simulated NB11 spectra. Player and downloads · Interactive lab
All model numbers today come from the demo data in Colab NB11 (Orange OR13 reads the same data, so the numbers match); in class, each group measures its own spectra, then repeats the same workflow.
The fix: don’t solve for 60 b values directly; first condense the spectrum into a few “latent variables” — that is PLS.
| Model | Calibration sample x₁=0.50, x₂=0.50 | New sample x₁=0.52, x₂=0.50 |
|---|---|---|
| ŷ = x₁ + x₂ | 1.00 | 1.02 |
| ŷ = 11x₁ − 9x₂ | 1.00 | 1.22 |
Both models give 1.00 on the calibration sample (equally accurate); x₁ gains only 0.02 of noise, yet the first model gives 1.02 and the second gives 1.22 — the larger the coefficients and the more their opposite signs cancel, the more the model amplifies noise.
| New sample | Straight line | 4th-degree curve |
|---|---|---|
| x = 1.5 (true value 1.5) | 1.56 | 0.75 |
| x = 4.5 (true value 4.5) | 4.47 | 3.64 |
The number of LVs controls how many latent variables the model uses: more LVs fit the calibration set more closely (RMSEC↓), but what they fit may be noise, so new samples do worse (RMSECV, RMSEP↑) — for preprocessed input, PLS is still a linear combination; adding LVs does not turn it into a higher-degree polynomial. The 4th-degree curve here is only an analogy for the overfitting idea that “fitting too tightly sacrifices performance on new samples”; it does not mean PLS itself fits a polynomial. This figure uses illustrative numbers, not NB11 data.
This approach is called leave-one-group-out (grouped by sample_id); it must be run for every number of LVs to draw the RMSECV vs. LV curve.
| Preprocessing (NB11) | Best LV | Minimum RMSECV (% w/v) |
|---|---|---|
| raw (none) | 3 | 0.458 |
| SNV | 2 | 0.400 |
| SG first derivative | 3 | 0.490 |
In the NB11 demo data, SNV gives the smallest RMSECV (LV=2), and all later figures use SNV. With your class’s data the conclusion may differ — so compare every time.
PLSRegression(scale=True) and the Orange PLS widget, Scale is checked by default. NB11 and OR13 both keep the default, so the numbers on both sides match (RMSECV 0.400).scale=False in sklearn) and see whether RMSECV gets larger or smaller — decide by RMSECV again, don’t guess.| Metric | Formula | In plain words |
|---|---|---|
| RMSE | √( Σe² ÷ n ) | On average, how far off (same unit as concentration) |
| bias | Σe ÷ n | How much too high (+) or too low (−) overall |
| R² | 1 − Σe² ÷ Σ(y − ȳ)² | Proportion of the reference-value variance the model explains (W07 ↗) |
| RPD | SD(ytest) ÷ RMSEP | How many times larger the samples’ own spread is than the error |
e = residual; n = number of spectra; y = reference value (W4 titration / prepared concentration); ȳ = mean of the reference values; SD = standard deviation. Computed on the calibration set it is called RMSEC, by cross-validation RMSECV, and on the test set RMSEP.
bias = 0 does not mean there is no error: positives and negatives cancel out, so look at RMSE and bias together.
| Metric | Meaning | NB11 value |
|---|---|---|
| RMSEC | The calibration set testing itself (optimistic) | 0.269% w/v |
| RMSECV | Grouped cross-validation, used to choose LVs | 0.400% w/v |
| RMSEP | Independent test set; use this in the final report | 0.427% w/v |
| R²(test) | Proportion of the reference-value variance the model explains | 0.929 |
| bias(test) | Mean over- / under-prediction on the test set | −0.054% w/v |
| RPD | SD(ytest) 1.67 ÷ RMSEP | 3.91 |
| Test sample (reference value) | Residuals e of the 3 cuvette reloads |
|---|---|
| STD-06 (4.62) | +0.72, +0.16, −0.26 |
| STD-08 (6.15) | +0.64, −0.12, −0.15 |
| STD-11 (8.46) | −0.25, −0.02, −0.17 |
| White vinegar (W4 titration) (4.50) | −0.62, +0.19, −0.78 |
NB11 demo data. Blue dots = cross-validation predictions of the calibration set; orange triangles = test set (final model). The closer the points are to the diagonal, the more accurate; a consistent over- or under-prediction is bias. The 12 residuals in the table at right are the source of RMSEP 0.427.
Exercise: change PLS Components to 1–6 and see at which number the RMSE in ① is lowest.
pls = PLSRegression(n_components=2) # scale=True is the default
y_cv = cross_val_predict(pls, X_cal, y_cal,
groups=sample_id, cv=LeaveOneGroupOut())
pls.fit(X_cal, y_cal)
y_pred = pls.predict(X_test) # → RMSEP| Group | Instrument | Band | Dispersion / detector | Excluded band | Preprocessing / LV | RMSEP (separate simulation, % w/v) |
|---|---|---|---|---|---|---|
| G1 | InnoSpectra NIR-S-G1 | 900–1700 nm | DLP + single InGaAs | — | SNV / 2 | 0.122 |
| G2 | InnoSpectra R14 | 1600–2400 nm | DLP + extended InGaAs | Excludes 1885–1994 nm (water absorption saturated) | First derivative / 4 | 0.106 |
| G3 | Avantes fiber-optic spectrometer | 900–1650 nm | Grating + InGaAs array | — | SNV / 2 | 0.132 |
| G4 | Avantes fiber-optic spectrometer (short-wave, bought on Taobao, awaiting delivery) | 600–1100 nm | Grating + Si array | — | SNV / 2 | 0.325 |
| G5 | NeoSpectra Micro Dev Kit | 1350–2500 nm | MEMS FT-NIR + single InGaAs | Excludes 1889–1987 and 2495–2500 nm (water absorption saturated) | SNV / 2 | 0.129 |
| Time | Contents |
|---|---|
| 0:00–0:55 | Lecture: calibration set design, interpolation/extrapolation, collinearity and LVs, choosing LVs by cross-validation, error metrics |
| 0:55–1:35 | Prepare the standard series (dilute concentrated vinegar / glacial acetic acid by weighing, in the fume hood); prepare the W4 commercial vinegar samples |
| 1:35–2:35 | NIR measurement (cuvette, 3 reloads per sample, each saved separately; first confirm your group’s instrument supports cuvette transmission mode) |
| 2:35–3:30 | Orange OR13: SNV → PLS → Test and Score (cross-validation grouped by sample_id) to choose LVs, then compute RMSEP on the test set |
| 3:30–4:00 | Whole-class roundup: each group’s model report card; compare RMSEP and RPD across instruments |
Colab NB11: Open NB11 in Colab ↗ · Beginner (no coding): Orange workflow OR13 ↗. The report must include: model report card (preprocessing, number of LVs, RMSEC/RMSECV/RMSEP, R², bias, RPD, applicable concentration range), and a discussion comparing with the model of another instrument.
Tap to tick — make sure you really can do it.