MolBasis
cross-validationgroupkfoldleakageinchikey5 min read

Grouped cross-validation keeps one molecule on one side

Grouped cross-validation keeps all rows of one molecule in the same fold. Without it, repeated measurements and salt forms leak across folds and inflate scores.

Updated November 28, 2026

Grouped cross-validation assigns every row that belongs to one molecule to the same fold, so the model is never asked to predict a measurement while a sibling measurement of the same molecule sits in its training set. Plain K-fold and especially plain leave-one-out do the opposite. They separate duplicates most of the time and let the model score on repeatability rather than prediction. The MolBasis record's rigor gate uses a grouped leave-one-out by molecule, with the InChIKey first block as the group key. This guide shows why, with the arithmetic of how often ungrouped folds leak.

Rows are not molecules

A property dataset row is one measurement. A molecule often has several. The same compound measured in two laboratories, a free base and its hydrochloride, two tautomer drawings normalised differently, or one solute in several solvents all produce multiple rows. For a solvation databank such as the one described in the guide on the Minnesota Solvation Database, one solute in many solvents is the normal case rather than the exception.

The descriptor file does not help here. Descriptors from one DFT calculation per molecule are identical for every row of that molecule, so two rows of one molecule have the same input and nearly the same target. A model that has seen one row can reproduce the other without learning anything about chemistry.

How often ungrouped folds separate a pair

Place two rows of one molecule into K folds at random. The first row lands somewhere. The second lands in a different fold with probability close to K minus 1 over K. For 5-fold cross-validation that is 0.8, for 10-fold it is 0.9, and for leave-one-out with n rows it is n minus 1 over n, which is essentially 1.

So in 5-fold cross-validation four pairs out of five leak, and in leave-one-out every pair leaks in every fold. Leave-one-out has a reputation for being conservative because it trains on almost all the data. For a dataset with duplicates it is the least conservative scheme available.

Here is the effect on one molecule. Suppose it was measured twice, at log S −2.1 and −2.3. Under plain leave-one-out, a flexible model predicts the −2.3 row at roughly −2.1, the value of its twin, and the error is 0.2 log units. That error is the repeatability of the measurement. Under grouped leave-one-out both rows are held out together and the model predicts them from other molecules. The error is then whatever the chemistry allows, which may be 0.8 log units. Only the second number describes how the model will do on a new compound.

Building the groups

Use the first block of the InChIKey, 14 characters that encode the connectivity. Ethanol has the InChIKey LFQSCWFLJHTTHZ-UHFFFAOYSA-N and its group key is LFQSCWFLJHTTHZ. The second block carries stereochemistry, isotopes and protonation, so enantiomers and salts that share a skeleton share the first block. Strip counter-ions before computing the key, otherwise a sodium salt gets a different key from its acid.

from rdkit import Chem
import numpy as np
from sklearn.model_selection import GroupKFold, LeaveOneGroupOut

def group_key(smiles):
    mol = Chem.MolFromSmiles(smiles)
    return Chem.MolToInchiKey(mol)[:14]

groups = np.array([group_key(s) for s in smiles_list])
for train, test in GroupKFold(n_splits=5).split(X, y, groups):
    assert not set(groups[train]) & set(groups[test])
logo = LeaveOneGroupOut()        # grouped leave-one-out, one molecule out per fold

GroupKFold balances the number of rows per fold while keeping each group whole. LeaveOneGroupOut holds out one molecule at a time with all its rows. The assertion is cheap and worth keeping. It fails loudly if a key was computed inconsistently.

Grouping by molecule versus grouping by scaffold

Grouping by InChIKey block removes the duplicate leak. It does not remove the analogue leak, where a close analogue with a different connectivity sits in the training fold. Grouping by Bemis-Murcko scaffold removes the analogue leak and the duplicate leak together, at the cost of larger and fewer groups. The guide on the scaffold split shows the construction.

The two groupings answer different questions. Molecule-level grouping asks how well the model predicts a compound it has not measured. Scaffold-level grouping asks how well it predicts a chemotype it has not seen. The MolBasis methodology uses both. The rigor gate runs a grouped leave-one-out by molecule during selection, and the final held-out figures come from a 25 percent scaffold-blind split. Each kernel page states the number of held-out molecules behind its figures.

Grouping inside model selection

Grouping only the outer evaluation is not enough. If a search over descriptors and exponents uses an ungrouped inner cross-validation to score candidates, it will prefer equations that exploit duplicates, and the outer grouped score will then be low for the wrong reason. Pass the same groups to every split in the pipeline, inner and outer. The guide on nested cross-validation shows how the two levels fit together, and the guide on random splits and leakage covers what leaks when they do not.

A last practical point. Deduplicate before grouping as well as after. Two rows with the same key and conflicting values need a decision, average them, keep one or drop both, and that decision belongs in the record. The guide on duplicates and conflicting measurements covers the options.

Key points

  • Rows are measurements, not molecules. One molecule often has several rows with identical descriptors.
  • Ungrouped K-fold separates a duplicate pair with probability about K minus 1 over K. Leave-one-out separates every pair.
  • Group by the first 14 characters of the InChIKey after stripping counter-ions. Assert that folds share no group.
  • Molecule grouping removes the duplicate leak. Scaffold grouping removes the analogue leak as well.
  • Pass the groups to the inner search and the outer evaluation alike, or the search will learn to exploit duplicates.

Questions people ask

What is grouped cross-validation?
It is cross-validation in which rows are first assigned to groups, and each group is placed entirely in one fold. In property prediction the group is usually one molecule, identified by the first block of its InChIKey, or one scaffold. The model never predicts a row from another row of the same group.
Why is plain leave-one-out the worst case for duplicates?
Leave-one-out holds out a single row and trains on everything else. If a molecule has two rows, the held-out row is always predicted with its duplicate in the training set. Every duplicate leaks in every fold, so the score is the measurement repeatability, not the model's predictive power.
How do I identify one molecule across salts and tautomers?
Use the first 14 characters of the InChIKey, the connectivity block. It is the same for different charge states, protonation states and drawn tautomers of the same skeleton, while the second block carries stereochemistry and isotopes. Standardise salts to the parent molecule first.

Further reading

In practice: The methodology page describes the checks applied to every kernel, scaffold-blind split included, and the limits of those checks. Every kernel page quotes the held-out figure first and the in-sample figure second.

See the kernels themselves

Every kernel is shown with its held-out result, the number of molecules it rests on and what it cannot be trusted for.