Cross-validation and data splitting
- class GroupKFoldShuffle(n_splits=5, *, shuffle=False, random_state=None)[source]
- split(X, y=None, groups=None)[source]
Generate indices to split data into training and test set.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data, where n_samples is the number of samples and n_features is the number of features.
y (array-like of shape (n_samples,), default=None) – The target variable for supervised learning problems.
groups (array-like of shape (n_samples,), default=None) – Group labels for the samples used while splitting the dataset into train/test set.
- Yields:
train (ndarray) – The training set indices for that split.
test (ndarray) – The testing set indices for that split.
- get_scaffold(smi)[source]
Generate the Bemis-Murcko scaffold for a given molecule.
- Parameters:
smi (
Union[str,Mol]) – A SMILES string or an RDKit molecule object representing the molecule for which to generate the scaffold.- Return type:
- Returns:
A SMILES string representing the Bemis-Murcko scaffold of the input molecule. If the scaffold cannot be generated, the input SMILES string is returned.
- get_random_clusters(smiles_list)[source]
Assign every SMILES to its own unique group.
This makes a grouped splitter (e.g. GroupKFoldShuffle) behave like a random (non-grouped) split, since no two molecules are forced into the same fold.
- get_butina_clusters(smiles_list, dist_cutoff=0.65)[source]
Cluster a list of SMILES strings using the Butina clustering algorithm.
- get_bemis_murcko_clusters(smiles_list)[source]
Cluster a list of SMILES strings based on their Bemis-Murcko scaffolds.
- get_kmeans_clusters(smiles_list, n_clusters=10)[source]
Cluster a list of SMILES strings using the KMeans clustering algorithm.
- cross_validate(df, model_list, y_col, group_list, n_outer=5, n_inner=5, random_state=None)[source]
Perform nested cross-validation on a dataset using multiple models and grouping strategies.
For each grouping strategy the data is split into n_outer outer folds. For every outer fold the inner models are cross-validated on the outer TRAINING set only (group assignments are computed there as well, to avoid leaking information from the held-out fold). The models are then refit on the full outer training set and evaluated on the outer test fold.
The resulting
foldcolumn uses the labelouter * (n_inner + 1) + innerfor the inner splits; the outer test evaluation occupies slotouter * (n_inner + 1) + n_innerso that inner and outer metrics never collide.- Parameters:
df (
DataFrame) – The input dataframe containing the data.model_list (
List[Tuple[str,Callable[[str],object]]]) – A list of tuples where each tuple contains a model name and a class that, when called withy_col, returns a model with avalidate(train, test)method.y_col (
str) – The name of the target column.group_list (
List[Tuple[str,Callable[[Series],Series]]]) – A list of tuples where each tuple contains a group name and a callable that assigns groups based on the SMILES column.n_outer (
int) – The number of outer folds for cross-validation. Default is 5.n_inner (
int) – The number of inner folds for cross-validation. Default is 5.random_state (
Optional[int]) – Seed for the shuffled splits. Default is None.
- Return type:
DataFrame- Returns:
A dataframe containing predictions (and model names as columns) for each fold, model, and group.