Zero Labs
Industry News / AI

What Shap Revealed About Molecular Properties: Four Models, Four Different Answers

Source · Towards AI By Shehan Makani

From the publisher’s feed, collected in the ZeroLabs newsroom.

Solubility, boiling point, LogP and mutagenicity from SMILES, and why the top feature changes every timeSame descriptors, four different top drivers. Source: Image by the author.Disclosure: I drafted this article with AI assistance from my own notebooks and results. I reviewed and edited it, and checked every number against my code.Most chemistry notebooks on Kaggle treat molecules as just another tabular dataset. Compute some descriptors, fit XGBoost, report R², done. I wanted to know something else: when a model predicts a molecular property well, is it learning chemistry, or a shortcut?So I built four models from the same family of RDKit descriptors, for four different properties, and asked SHAP what drove each one. The answer was different every time, and each time it matched the physical chemistry. Two of the four also ended with a result I didn’t expect: the fancy model lost.All notebooks and data are on GitHub (cheminformatics-ml).The setupEvery notebook follows the same recipe, so the differences come from the chemistry, not the pipeline.Data. Small, curated sets with measured values: 99 compounds for solubility, 181 for boiling point (11 chemical classes), 119 for LogP (Hansch & Leo, NIST) and 122 for Ames mutagenicity (Hansen et al., Kazius et al.).Features. Descriptors computed from SMILES with RDKit: molecular weight, Crippen LogP, H-bond donors and acceptors, polar surface area, ring counts, molar refractivity (MolMR), BertzCT complexity and more. The mutagenicity model adds structural alerts such as nitro and nitroso groups.Models. Ridge and ElasticNet as linear baselines, then random forest, gradient boosting, XGBoost and LightGBM. Every model is compared to a domain baseline, not just to the others.Explanation. SHAP values on the best tree model [3], to see which descriptor actually drives the prediction.The baselines matter most. A model that beats LightGBM but loses to a 20-year-old formula hasn’t learned anything useful.1. Solubility: LogP decidesXGBoost predicted aqueous solubility (logS) with RMSE 0.52 log units and R² 0.89 on held-out compounds. The classic Delaney (2004) formula [1], applied across the dataset, scored RMSE 1.15, more than twice the error.SHAP put Crippen LogP far ahead, at 63% of XGBoost’s feature importance. That is the thermodynamics of water exclusion: hydrophobic molecules don’t dissolve. Polycyclic aromatics such as pyrene (logS −5.18) set the insolubility floor, with high LogP and no heteroatoms to bond with water. Tree models beat linear ones because the effect isn’t additive: LogP and ring count interact.Test error by model vs. the Delaney formula. Source: Image by the author.2. Boiling point: complexity, not LogPXGBoost reached RMSE 28.5 °C and R² 0.84 across compounds boiling from −162 to +365 °C. A molecular-weight-only baseline managed RMSE 47.7 °C.The top SHAP driver flipped. It was BertzCT, a measure of structural complexity, not LogP. More complex molecules have more surface for van der Waals contact, so they hold together longer as liquids. H-bond donors push boiling points well above non-polar molecules of the same weight.SHAP feature importance for boiling point. Source: Image by the author.3. LogP: the purpose-built formula winsHere I removed RDKit’s own Crippen LogP from the features and asked whether ML could rebuild it from structure. The best model, ElasticNet, reached RMSE 0.75 and R² 0.84. Crippen’s atom-contribution method [2] still won, at RMSE 0.65 and R² 0.88.That isn’t a failure; it’s the point. Crippen encodes a measured contribution for each atom type, which 119 molecules can’t relearn. SHAP’s top driver was molar refractivity (MolMR), a proxy for electron polarizability, which is the physical reason a molecule prefers octanol to water. ML was competitive in the LogP 1–3 range, where most drug molecules sit.SHAP feature importance for LogP. Source: Image by the author.4. Mutagenicity: a few structures say it allFor Ames mutagenicity, a binary yes/no, logistic regression ranked first on the held-out test set: AUC 0.987 and recall 0.909. In 5-fold cross-validation, though, SVM and XGBoost scored higher (AUC 0.98 vs. 0.90). With 122 compounds, that ranking is fragile.The structural alerts were not fragile. Every compound with a nitro or nitroso group in the set was mutagenic [4, 5], because these groups become electrophiles that attack DNA. Aromatic amines were mutagenic 30% of the time, and alkyl halides 43%. I optimized for recall, because in toxicology a missed mutagen costs far more than a false alarm.ROC curves for the mutagenicity models. Source: Image by the author.The core pipelineThe whole loop fits on one screen. This is the boiling-point version, trimmed from the notebook:import numpy as np, pandas as pd, shap, xgboost as xgbfrom rdkit import Chemfrom rdkit.Chem import Descriptors, Crippen, rdMolDescriptorsfrom sklearn.model_selection import train_test_splitdef featurize(smiles): mol = Chem.MolFromSmiles(smiles) return { "MW": Descriptors.MolWt(mol), "LogP": Crippen.MolLogP(mol), "HBD": rdMolDescriptors.CalcNumHBD(mol), "TPSA": Descriptors.TPSA(mol), "MolMR": Crippen.MolMR(mol), "BertzCT": Descriptors.BertzCT(mol), # ...the full notebooks use more descriptors }X = pd.DataFrame([featurize(s) for s in df["smiles"]])y = df["bp_celsius"]# 70/15/15 train/validation/test splitX_train, X_tmp, y_train, y_tmp = train_test_split(X, y, test_size=0.30, random_state=42)X_val, X_test, y_val, y_test = train_test_split(X_tmp, y_tmp, test_size=0.50, random_state=42)model = xgb.XGBRegressor(n_estimators=300, learning_rate=0.05, max_depth=5, subsample=0.8, colsample_bytree=0.8, random_state=42)model.fit(X_train, y_train)# Which descriptor drives the prediction?shap_vals = shap.TreeExplainer(model).shap_values(X_test)importance = pd.Series(np.abs(shap_vals).mean(axis=0), index=X.columns)print(importance.sort_values(ascending=False).head())Swap the target column and the same code answers a different chemistry question. That is what makes the comparison fair.What the four models taught meThe top SHAP driver for each property:Solubility → LogP. Hydrophobicity: water excludes the molecule.Boiling point → BertzCT. Complexity: more van der Waals contact.LogP → MolMR. Polarizability: preference for octanol.Mutagenicity → nitrogen count and structural alerts. Electrophiles that attack DNA.The best feature depends on the question. The same descriptor set gave a different top driver for each property. A model that explains itself with the right chemistry is one you can trust beyond the test set.Always benchmark against the domain baseline. Beating Ridge is easy. Beating Delaney’s formula, or losing honestly to Crippen’s, tells you something real.Small data changes which model wins. With 100–180 molecules, a 20-compound test set can reorder the leaderboard, as cross-validation showed for mutagenicity. Report both, and treat the gap as a warning. Compare baselines on the same split, too.Pick the metric for the cost of being wrong. In toxicology that is recall, not accuracy.ClosingI run a specialty chemical formulation company, and these are the questions I ask before choosing a surfactant or a solvent. ML earned its place in that work only once I could check that it was reasoning like a chemist. SHAP is how I check.The four notebooks, data and figures are on GitHub and Kaggle.Resourcescheminformatics-ml notebooks on GitHubKaggle notebooksRDKitSHAPXGBoostReferences[1] J. S. Delaney, “ESOL: Estimating aqueous solubility directly from molecular structure,” J. Chem. Inf. Comput. Sci., vol. 44, no. 3, pp. 1000–1005, 2004.[2] S. A. Wildman and G. M. Crippen, “Prediction of physicochemical parameters by atomic contributions,” J. Chem. Inf. Comput. Sci., vol. 39, no. 5, pp. 868–873, 1999.[3] S. M. Lundberg and S.-I. Lee, “A unified approach to interpreting model predictions,” Advances in Neural Information Processing Systems 30, 2017.[4] K. Hansen et al., “Benchmark data set for in silico prediction of Ames mutagenicity,” J. Chem. Inf. Model., vol. 49, no. 9, pp. 2077–2081, 2009.[5] J. Kazius, R. McGuire and R. Bursi, “Derivation and validation of toxicophores for mutagenicity prediction,” J. Med. Chem., vol. 48, no. 1, pp. 312–320, 2005.What Shap Revealed About Molecular Properties: Four Models, Four Different Answers was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.