Artificial intelligence can now predict welfare risk in dairy cows from ordinary milk-recording data, and a new study shows it can also explain its own reasoning. Researchers trained machine learning models on monthly records from 798 dairy cows to reconstruct five welfare indicators used by the Italian Breeders Association, covering mastitis, subclinical acidosis, subclinical ketosis, longevity, and reproduction. The work, published as a proof of concept in Discover Artificial Intelligence, then applied explainable artificial intelligence techniques to reveal how the models reached their predictions, and where they might mislead.
The study was designed explicitly as a proof of concept, not as a ready-to-deploy farm tool. The team, led by Pablo Augusto de Souza Fonseca with colleagues at several institutions, drew on data from the Italian Breeders Association Livestock Environment Opendata platform. Their goal was to test whether explainable AI, or XAI, can make the inner workings of complex machine learning models transparent enough to support real welfare decisions, rather than leaving farmers to trust a black box.
For each cow and month, the researchers used six routinely available test-day traits as predictors: milk acetone, fat, lactose, protein, urea, and electrical conductivity. After quality control that removed records more than five standard deviations from the feature mean, the final dataset contained 125,285 monthly cow records, split into a training set of 87,699 observations and a test set of 37,586. Random forest regression models were trained separately for each of the five welfare indicators, which are scored from 0 to 30, with values up to 10 indicating good welfare, 10 to 20 intermediate, and above 20 indicating risk.
The predictive performance was strikingly high. In the test set, the models achieved Pearson correlations of 0.982 for longevity, 0.987 for mastitis, 0.971 for subclinical ketosis, 0.979 for subclinical acidosis, and 0.978 for reproduction, with corresponding coefficients of determination between 0.934 and 0.966. The random forest models also outperformed linear regression models fitted with the same features, which the authors say justified the choice of a more flexible, if less intrinsically interpretable, model class.
Those correlations, however, come with an important caveat that the authors themselves stress. The welfare indicators being predicted are themselves calculated from structured dairy herd improvement records using threshold-based standardization procedures, in some cases involving the same features that fed the models. The prediction task is therefore best understood as the reconstruction of standardized welfare-risk scores, not the diagnosis of independent clinical disorders. The authors caution that the high accuracy should not be extrapolated to independent disease diagnosis, and that external validation using independent herds, temporal splits, animal-level splits, and clinical records will be necessary before any practical deployment.
When the predicted individual indicators were combined to reconstruct the overall welfare class, labelled good, intermediate, or risk, the models achieved a balanced accuracy of 0.841, with weighted precision of 0.938, recall of 0.930, and an F1-score of 0.930. Performance varied considerably by class. For the good class, precision was perfect at 1, meaning no false positives, but recall was only 0.665, so roughly a third of truly good cows were classified as intermediate. The authors note this was a deliberate conservative choice to avoid false positives, and that rounding the predicted scores raised the true positive rate for the good class to about 80.5 percent, at the cost of 70 false positives. For the risk class, precision reached 0.997 and recall 0.859, and crucially, no risk-classified cow was ever misclassified as good; all errors fell between the risk and intermediate classes.
To open the black box, the team used SHAP values, from SHapley Additive exPlanations, which quantify how much each feature contributed to each prediction. The resulting patterns were largely biologically plausible. In the mastitis model, lower lactose, protein, and fat were associated with higher predicted welfare-risk scores, and higher milk conductivity raised risk predictions in the risk and intermediate classes, consistent with the well-established role of milk electrical conductivity as a mastitis biomarker, since intramammary infection drives sodium and chloride into milk and lactose out. In the ketosis model, higher acetone values contributed to higher predicted risk scores, echoing the known link between milk acetone and subclinical ketosis during negative energy balance after calving.
Not every pattern was intuitive. For the acidosis model, records with lower fat and higher protein received higher predicted risk scores, in line with the known association between a low fat-to-protein ratio and subacute ruminal acidosis. But for the longevity and reproduction indicators, the SHAP analysis revealed U-shaped patterns in which both very high and very low feature values increased predicted risk, and these patterns differed between the good and risk classes for the same features. The authors argue that such class-specific differences show why feature importance must be interpreted group by group rather than through one-size-fits-all rules, and they note that some patterns may reflect sample size or measurement biases within narrow intervals.
The second XAI technique, counterfactual explanations, identified the minimal changes in feature values that would shift a risk prediction back toward good welfare. For a cow with three individual indicators in the risk class, the suggested changes generally fell between the observed values recorded in months when the same animal was classified as good and those in risk months, and most aligned with the animal’s own observed good-welfare profile. Across the whole test set, mean counterfactual values typically lay between the true risk and good class means. Yet there were exceptions that demand expert scrutiny: for mastitis, the counterfactuals suggested an increase in milk conductivity, which is biologically illogical given that higher conductivity accompanies mastitis. The authors conclude that counterfactuals should be treated as tools for exploring feasible management scenarios, not as rigid prescriptions.
The broader message is about trust and oversight in automated livestock decision-making. The authors emphasize that SHAP values do not provide causal evidence; they explain how a fitted model used its features, not that a milk trait causes a change in welfare. Used properly, they argue, these tools can improve model auditing, flag implausible model behaviour, and support expert interpretation in precision livestock farming, while occasional non-intuitive outputs highlight the continuing need for expert oversight. The study’s datasets are openly available, and the authors call for systematic evaluation of on-farm applicability, economic impact, and usability before such systems reach real production environments.
Subject of Research: Explainable AI applied to predicting dairy cow welfare-risk indicators from routine milk-recording data
Article Title: Improving interpretability and applicability of welfare management decisions in dairy cows through explainable artificial intelligence
Article References: de Souza Fonseca, P. A., Suárez-Vega, A., Gutiérrez-Gil, B., Dadousis, C., Geifman, N., Melilli, C., Pascarella, L., Whetton, A. D., & Arranz, J. J. (2026). Improving interpretability and applicability of welfare management decisions in dairy cows through explainable artificial intelligence. Discover Artificial Intelligence, 6(1), Article 1026. https://doi.org/10.1007/s44163-026-01938-1
Image Credits: AI Generated
DOI: 10.1007/s44163-026-01938-1
Keywords: explainable AI, dairy cows, animal welfare, machine learning, random forest, SHAP values, counterfactual explanations, milk composition, welfare indicators, precision livestock farming, mastitis, subclinical ketosis
News Source: William Thompson. (October 29, 2026). Explainable AI decodes dairy cow welfare risk from routine milk records. Scienmag.



