Machine learning models to predict the recurrence of common bile duct stones after ERCP Authors: Xu Han, Mujie Ye, Ye Tian, Qin Long, Qiyun Tang, Hong Zhu Published: 2026 ABSTRACT ObjectiveTo construct predictive models for the recurrence of common bile duct stones (CBDS) following endoscopic retrograde cholangiopancreatography (ERCP).MethodsThis retrospective study analyzed data from 1,130 patients who were randomly divided into a training set (70%) and a test set (30%). Feature selection was performed using the boruta algorithm and multivariable logistic regression (LR), followed by addressing data imbalance through the Synthetic Minority Over-sampling Technique (SMOTE). Predictive models were developed utilizing random forest (RF), extreme gradient boosting (XGBoost), and LR. We optimized these models through random search and ten-fold cross-validation to identify the best parameters. After model development, we compared their area under the curve (AUC), accuracy, recall, precision, F1-score, and decision curve analysis (DCA) to select the most optimal model. Ultimately, the optimal model was interpreted using shapley additive explanations (SHAP).ResultsEight risk factors were identified and used to construct the predictive model, including clinical course, stone diameter, presence of multiple stones, use of biliary stents, alcohol consumption, history of biliary tract operations, presence of CBD stenosis, and endoscopic papillary balloon dilation. The RF model outperformed XGBoost and LR in terms of AUC, accuracy, recall, precision, F1-score, and DCA. The SHAP summary plot, waterfall plot, and force plot provided both overall and local explanations of the RF model.ConclusionThis study successfully identifies high-risk individuals for recurrent CBDS post-ERCP and offers valuable insights for machine learning-assisted clinical decision-making. Introduction Cholelithiasis, a leading source of digestive disease-related socioeconomic burden, exhibits a global prevalence of 10% to 20%. Among these cases, common bile duct stones (CBDS) account for 5% to 30% [1]. CBDS is a pathologic condition with a foundation in cholesterol, bilirubin, and bile acid metabolism irregularities [2, 3]. Currently, CBDS treatment primarily involves stone extraction via endoscopic retrograde cholangiopancreatography (ERCP), noted for its minimal invasiveness and positive outcomes [4]. However, the recurrence rate is concerning (4%-25%)3. Recurrences entail persistent abdominal discomfort and may precipitate serious complications like pancreatitis or biliary infections, imposing a risk to human life [5]. Although many scholars have studied the factors of CBDS recurrence after ERCP in recent years, there is still no consensus. For example, there is still considerable controversy regarding whether factors such as age, cholecystectomy, and stone size may contribute to CBDS recurrence [3]. In most studies on CBDS recurrence, the most commonly used method for constructing predictive models is traditional regression analysis, which is limited by its simplicity and the high demand for structured data, making it difficult to fully explore the hidden meanings behind the data [6]. Machine learning comprises a set of powerful algorithms capable of effectively analyzing high-dimensional data and building complex models [7]. Machine learning is considered the future of biomedical research, personalized medicine, and computer-aided diagnosis [8]. Therefore, this study utilized three machine learning algorithms: Random Forest (RF), Extreme Gradient Boosting (XGBoost), and Logistic Regression (LR) to construct the predictive model. For the optimal model, we also employed Shapley additive explanations (SHAP) for model interpretation. SHAP provides intuitive explanations for how each feature in the model contributes to the final prediction result [9]. The main goal of this study is to build a high-performance predictive model for CBDS recurrence after ERCP, providing a basis for clinical decision-making. Materials and methods Object of study Patients who underwent ERCP treatment for CBDS at the Digestive Endoscopy Center of the First Affiliated Hospital of Nanjing Medical University from January 1, 2017, to March 1, 2022, were collected. Inclusion criteria: (1) Age of at least 18 years. (2) Undergoing ERCP treatment for CBDS for the first time. (3) Postoperative cholangiography or nasobiliary drainage imaging or other imaging studies confirms no residual stones. (4) Complete clinical data. Exclusion criteria are detailed in Fig. 1. Eligible patients were required to have at least one postoperative imaging study performed at least 6 months apart to determine the presence of recurrence. CBDS recurrence is defined as: a patient undergoing ERCP treatment for CBDS for the first time with successful stone clearance, and then at least 6 months post-surgery, being diagnosed with CBDS formation again through ultrasound, MRI, CT, MRCP, ERCP or other imaging studies [3]. The study was approved by the Ethics Committee of the First Affiliated Hospital of Nanjing Medical University (2023-SR-657). Fig. 1Research flowchart Research flowchart Data collection The following data were collected for each patient: (1) Medical history information, including Gender, Age, time of first symptoms related to CBDS (Course), Smoke, Alcohol consumption, Hepatitis, Hypertension, Diabetes, Coronary heart disease (CHD), Cerebral infarction, Biliary tract operation, and whether the gallbladder was removed (Cholecystectomy). (2) ERCP report, which includes periampullary diverticulum (PAD), Multiple stones, sludge-like stones (Silt), the diameter of the largest stone (Diameter), Endoscopic sphincterotomy (EST), Endoscopic papillary balloon dilation (EPBD), Suppurative cholangitis, whether only a retrieval basket was used for stone extraction (Basket), Biliary stent, Lithotripsy, and CBD stenosis. (3) Preoperative biochemical indicators, including Alkaline phosphatase (ALP), L-γ-Glutamyltranspeptidase (GGT), Total bilirubin (TBIL), Direct bilirubin (DBIL), Total Cholesterol (TC), Triglycerides (TG), Low density lipoprotein cholesterol (LDL-C), creatinine (Cr), uric acid (UA), blood calcium (Ca), and Fasting blood glucose (FBG). Data analysis Baseline data were analyzed and organized using SPSS 26.0 statistical software and R version 4.3.0. Quantitative data conforming to normal distribution were expressed as mean ± standard deviation, and comparison between two groups was performed using independent sample t-test. Skewed distribution quantitative data were expressed as median (25th percentile, 75th percentile) [M (Q1, Q3)], and a Mann-Whitney U test was used for intergroup comparisons. Categorical data were presented as rates or proportions (%), and the χ2 test or Fisher’s exact test was used for comparisons between groups. Differences were deemed to have statistical significance at p < 0.05. Data analysis and model construction were conducted in Python (3.11.5) within Jupyter Notebook. Model construction and evaluation The main workflow of this study is shown in Fig. 1. The data were randomly split into training and test sets at a 7:3 ratio. The Boruta algorithm was employed for initial feature selection on the training set. The Boruta algorithm can account for interactions between features, providing more comprehensive and precise results in feature selection [10]. Further refinement of the remaining features was achieved using univariate LR. Features with a p-value < 0.05 in univariate LR were included in the subsequent multivariate analysis. After feature selection, the imbalanced training set was processed with the Synthetic Minority Over-sampling Technique (SMOTE) to achieve the final training set. SMOTE generates new minority class instances by interpolating neighboring samples. The new data differs from the original but retains a very similar distribution, avoiding sample repetition [11]. Predictive models were constructed using three machine learning algorithms: RF, XGBoost, and LR on the preprocessed training set. The optimal parameters for the models were determined using the method of random search for hyperparameter tuning. Ten-fold cross-validation was used to enhance the reliability of the models. The models were validated on the test set to assess their generalizability. Evaluation metrics included the AUC value, accuracy, recall, precision, and F1-score. The net benefits of the three models were calculated, and decision curve analysis (DCA) were plotted to select the optimal model. SHAP was used to interpret the most optimal model. Results Patient characteristics After rigorous screening, a total of 1130 patients were included in the study, comprising 952 in the non-recurrent group and 178 in the recurrent group (Table 1). In this study, all patients were followed up for at least 6 months after ERCP. The median follow-up duration was 28.5 months (interquartile range: 16.0–41.0 months), and the maximum follow-up duration was 58 months. The recurrent and non-recurrent groups showed statistical differences in Diameter, Course, Smoke, Drinking, Hypertension, Diabetes, Cerebral infarction, Biliary tract operation, Silt, Multiple stones, EPBD, Suppurative cholangitis, Lithotripsy, Cholecystectomy, Biliary stent, CBD stenosis. The data was randomly divided into a training set (n = 791) and a test set (n = 339) at a ratio of 7:3, with no statistical differences in distribution between the two datasets (p > 0.05) (Table 2). Table 1Baseline characteristics of the datasetVariablesTotal (n = 1130)Non-recurrent (n = 952)Recurrent (n = 178)Statistic P Diameter(mm), M (Q₁, Q₃)8.00 (5.00, 11.00)8.00 (5.00, 10.00)10.00 (8.00, 14.00)Z=-5.84< 0.001Age(year), M (Q₁, Q₃)64.00 (53.00, 74.00)64.00 (53.00, 74.00)63.00 (53.00, 72.00)Z=-0.750.455Course(day), M (Q₁, Q₃)14.00 (6.00, 30.00)10.00 (5.00, 30.00)31.50 (10.00, 60.00)Z=-7.60< 0.001ALP(U/L), M (Q₁, Q₃)168.00 (105.90, 285.60)168.00 (106.57, 286.60)168.90 (99.90, 265.68)Z=-0.790.427GGT(U/L), M (Q₁, Q₃)244.30 (80.62, 483.50)251.50 (85.70, 484.25)202.10 (55.95, 446.98)Z=-1.190.233TBIL(µmol/L), M (Q₁, Q₃)20.25 (12.80, 44.22)20.05 (12.80, 44.30)20.95 (12.85, 43.80)Z=-0.450.651DBIL(µmol/L), M (Q₁, Q₃)9.80 (5.10, 28.28)9.60 (5.10, 28.72)10.10 (5.05, 25.48)Z=-0.090.931TC(mmol/L), M (Q₁, Q₃)4.19 (3.52, 4.91)4.24 (3.55, 4.93)4.07 (3.41, 4.89)Z=-1.650.098TG(mmol/L), M (Q₁, Q₃)1.32 (0.93, 1.82)1.33 (0.93, 1.83)1.30 (0.93, 1.77)Z=-0.480.634LDL-C(mmol/L), M (Q₁, Q₃)2.65 (2.16, 3.19)2.68 (2.16, 3.21)2.55 (2.09, 3.16)Z=-1.680.093FBG(mmol/L), M (Q₁, Q₃)5.09 (4.53, 6.26)5.09 (4.54, 6.20)5.12 (4.52, 6.62)Z=-0.440.658Cr(µmol/L), M (Q₁, Q₃)65.60 (55.80, 77.10)65.80 (55.80, 77.20)65.10 (56.52, 75.65)Z=-0.320.745UA(µmol/L), M (Q₁, Q₃)266.00 (206.25, 327.00)263.45 (206.00, 324.00)282.00 (208.40, 344.75)Z=-1.650.099Ca(mmol/L), M (Q₁, Q₃)2.20 (2.11, 2.29)2.20 (2.11, 2.29)2.19 (2.10, 2.29)Z=-0.600.545Gender, n(%)χ²=0.000.949 Male626 (55.40)527 (55.36)99 (55.62) Female504 (44.60)425 (44.64)79 (44.38)Smoke, n(%)χ²=19.69< 0.001 NO916 (81.06)793 (83.30)123 (69.10) YES214 (18.94)159 (16.70)55 (30.90)Drinking, n(%)χ²=60.25< 0.001 NO959 (84.87)842 (88.45)117 (65.73) YES171 (15.13)110 (11.55)61 (34.27)Hepatitis, n(%)χ²=1.350.245 NO1057 (93.54)894 (93.91)163 (91.57) YES73 (6.46)58 (6.09)15 (8.43)Hypertension, n(%)χ²=4.000.045 NO710 (62.83)610 (64.08)100 (56.18) YES420 (37.17)342 (35.92)78 (43.82)Diabetes, n(%)χ²=4.730.030 NO945 (83.63)806 (84.66)139 (78.09) YES185 (16.37)146 (15.34)39 (21.91)CHD, n(%)χ²=2.690.101 NO1054 (93.27)893 (93.80)161 (90.45) YES76 (6.73)59 (6.20)17 (9.55)Cerebral infarction, n(%)χ²=8.410.004 NO1039 (91.95)885 (92.96)154 (86.52) YES91 (8.05)67 (7.04)24 (13.48)Biliary tract operation, n(%)χ²=78.19< 0.001 NO1018 (90.09)890 (93.49)128 (71.91) YES112 (9.91)62 (6.51)50 (28.09)PAD, n(%)χ²=0.140.713 NO819 (72.48)692 (72.69)127 (71.35) YES311 (27.52)260 (27.31)51 (28.65)Silt, n(%)χ²=4.760.029 NO854 (75.58)708 (74.37)146 (82.02) YES276 (24.42)244 (25.63)32 (17.98)Multiple stones, n(%)χ²=48.58< 0.001 NO701 (62.04)632 (66.39)69 (38.76) YES429 (37.96)320 (33.61)109 (61.24)EST, n(%)χ²=0.700.403 NO357 (31.59)296 (31.09)61 (34.27) YES773 (68.41)656 (68.91)117 (65.73)EPBD, n(%)χ²=29.19< 0.001 NO168 (14.87)118 (12.39)50 (28.09) YES962 (85.13)834 (87.61)128 (71.91)Suppurative cholangitis, n(%)χ²=4.370.037 NO1066 (94.34)904 (94.96)162 (91.01) YES64 (5.66)48 (5.04)16 (8.99)Lithotripsy, n(%)χ²=20.85< 0.001 NO1058 (93.63)905 (95.06)153 (85.96) YES72 (6.37)47 (4.94)25 (14.04)Cholecystectomy, n(%)χ²=8.150.004 NO587 (51.95)512 (53.78)75 (42.13) YES543 (48.05)440 (46.22)103 (57.87)Basket, n(%)χ²=0.060.814 NO517 (45.75)437 (45.90)80 (44.94) YES613 (54.25)515 (54.10)98 (55.06)Biliary stent, n(%)χ²=67.39< 0.001 NO975 (86.28)856 (89.92)119 (66.85) YES155 (13.72)96 (10.08)59 (33.15)CBD stenosis, n(%)χ²=35.24< 0.001 NO1062 (93.98)912 (95.80)150 (84.27) YES68 (6.02)40 (4.20)28 (15.73) Table 1Baseline characteristics of the datasetVariablesTotal (n = 1130)Non-recurrent (n = 952)Recurrent (n = 178)Statistic P Diameter(mm), M (Q₁, Q₃)8.00 (5.00, 11.00)8.00 (5.00, 10.00)10.00 (8.00, 14.00)Z=-5.84< 0.001Age(year), M (Q₁, Q₃)64.00 (53.00, 74.00)64.00 (53.00, 74.00)63.00 (53.00, 72.00)Z=-0.750.455Course(day), M (Q₁, Q₃)14.00 (6.00, 30.00)10.00 (5.00, 30.00)31.50 (10.00, 60.00)Z=-7.60< 0.001ALP(U/L), M (Q₁, Q₃)168.00 (105.90, 285.60)168.00 (106.57, 286.60)168.90 (99.90, 265.68)Z=-0.790.427GGT(U/L), M (Q₁, Q₃)244.30 (80.62, 483.50)251.50 (85.70, 484.25)202.10 (55.95, 446.98)Z=-1.190.233TBIL(µmol/L), M (Q₁, Q₃)20.25 (12.80, 44.22)20.05 (12.80, 44.30)20.95 (12.85, 43.80)Z=-0.450.651DBIL(µmol/L), M (Q₁, Q₃)9.80 (5.10, 28.28)9.60 (5.10, 28.72)10.10 (5.05, 25.48)Z=-0.090.931TC(mmol/L), M (Q₁, Q₃)4.19 (3.52, 4.91)4.24 (3.55, 4.93)4.07 (3.41, 4.89)Z=-1.650.098TG(mmol/L), M (Q₁, Q₃)1.32 (0.93, 1.82)1.33 (0.93, 1.83)1.30 (0.93, 1.77)Z=-0.480.634LDL-C(mmol/L), M (Q₁, Q₃)2.65 (2.16, 3.19)2.68 (2.16, 3.21)2.55 (2.09, 3.16)Z=-1.680.093FBG(mmol/L), M (Q₁, Q₃)5.09 (4.53, 6.26)5.09 (4.54, 6.20)5.12 (4.52, 6.62)Z=-0.440.658Cr(µmol/L), M (Q₁, Q₃)65.60 (55.80, 77.10)65.80 (55.80, 77.20)65.10 (56.52, 75.65)Z=-0.320.745UA(µmol/L), M (Q₁, Q₃)266.00 (206.25, 327.00)263.45 (206.00, 324.00)282.00 (208.40, 344.75)Z=-1.650.099Ca(mmol/L), M (Q₁, Q₃)2.20 (2.11, 2.29)2.20 (2.11, 2.29)2.19 (2.10, 2.29)Z=-0.600.545Gender, n(%)χ²=0.000.949 Male626 (55.40)527 (55.36)99 (55.62) Female504 (44.60)425 (44.64)79 (44.38)Smoke, n(%)χ²=19.69< 0.001 NO916 (81.06)793 (83.30)123 (69.10) YES214 (18.94)159 (16.70)55 (30.90)Drinking, n(%)χ²=60.25< 0.001 NO959 (84.87)842 (88.45)117 (65.73) YES171 (15.13)110 (11.55)61 (34.27)Hepatitis, n(%)χ²=1.350.245 NO1057 (93.54)894 (93.91)163 (91.57) YES73 (6.46)58 (6.09)15 (8.43)Hypertension, n(%)χ²=4.000.045 NO710 (62.83)610 (64.08)100 (56.18) YES420 (37.17)342 (35.92)78 (43.82)Diabetes, n(%)χ²=4.730.030 NO945 (83.63)806 (84.66)139 (78.09) YES185 (16.37)146 (15.34)39 (21.91)CHD, n(%)χ²=2.690.101 NO1054 (93.27)893 (93.80)161 (90.45) YES76 (6.73)59 (6.20)17 (9.55)Cerebral infarction, n(%)χ²=8.410.004 NO1039 (91.95)885 (92.96)154 (86.52) YES91 (8.05)67 (7.04)24 (13.48)Biliary tract operation, n(%)χ²=78.19< 0.001 NO1018 (90.09)890 (93.49)128 (71.91) YES112 (9.91)62 (6.51)50 (28.09)PAD, n(%)χ²=0.140.713 NO819 (72.48)692 (72.69)127 (71.35) YES311 (27.52)260 (27.31)51 (28.65)Silt, n(%)χ²=4.760.029 NO854 (75.58)708 (74.37)146 (82.02) YES276 (24.42)244 (25.63)32 (17.98)Multiple stones, n(%)χ²=48.58< 0.001 NO701 (62.04)632 (66.39)69 (38.76) YES429 (37.96)320 (33.61)109 (61.24)EST, n(%)χ²=0.700.403 NO357 (31.59)296 (31.09)61 (34.27) YES773 (68.41)656 (68.91)117 (65.73)EPBD, n(%)χ²=29.19< 0.001 NO168 (14.87)118 (12.39)50 (28.09) YES962 (85.13)834 (87.61)128 (71.91)Suppurative cholangitis, n(%)χ²=4.370.037 NO1066 (94.34)904 (94.96)162 (91.01) YES64 (5.66)48 (5.04)16 (8.99)Lithotripsy, n(%)χ²=20.85< 0.001 NO1058 (93.63)905 (95.06)153 (85.96) YES72 (6.37)47 (4.94)25 (14.04)Cholecystectomy, n(%)χ²=8.150.004 NO587 (51.95)512 (53.78)75 (42.13) YES543 (48.05)440 (46.22)103 (57.87)Basket, n(%)χ²=0.060.814 NO517 (45.75)437 (45.90)80 (44.94) YES613 (54.25)515 (54.10)98 (55.06)Biliary stent, n(%)χ²=67.39< 0.001 NO975 (86.28)856 (89.92)119 (66.85) YES155 (13.72)96 (10.08)59 (33.15)CBD stenosis, n(%)χ²=35.24< 0.001 NO1062 (93.98)912 (95.80)150 (84.27) YES68 (6.02)40 (4.20)28 (15.73) Baseline characteristics of the dataset Table 2Comparison of data distribution between the training set and test setVariablesTotal (n = 1130)Training (n = 791)Test (n = 339)Statistic P Diameter(mm), M (Q₁, Q₃)8.00 (5.00–11.00)8.00 (5.00–11.50)8.00 (5.00–10.00)Z = 0.2620.795Age(year), M (Q₁, Q₃)64.00 (53.00–74.00)64.00 (53.00–74.00)63.00 (51.50–73.00)Z = 1.1140.265Course(day), M (Q₁, Q₃)14.00 (6.00–30.00)14.00 (6.00–30.00)14.00 (6.00–30.00)Z = 0.3860.700ALP(U/L), M (Q₁, Q₃)168.00 (105.90–285.60)171.00 (105.00–293.95)163.00 (108.10–268.00)Z = 0.2610.794GGT(U/L), M (Q₁, Q₃)244.30 (80.62–483.50)238.20 (75.25–465.10)252.40 (97.15–510.05)Z = 0.9900.322TBIL(µmol/L), M (Q₁, Q₃)20.25 (12.80–44.22)20.20 (12.90–44.50)20.30 (12.60–43.55)Z = 0.3290.742DBIL(µmol/L), M (Q₁, Q₃)9.80 (5.10–28.28)9.80 (5.10–28.75)9.90 (5.15–26.55)Z = 0.3370.736TC(mmol/L), M (Q₁, Q₃)4.19 (3.52–4.91)4.19 (3.51–4.93)4.22 (3.54–4.86)Z = 0.4320.666TG(mmol/L), M (Q₁, Q₃)1.32 (0.93–1.82)1.33 (0.94–1.81)1.31 (0.91–1.84)Z = 0.2740.784LDL-C(mmol/L), M (Q₁, Q₃)2.65 (2.16–3.19)2.65 (2.14–3.21)2.67 (2.21–3.18)Z = 0.3510.726FBG(mmol/L), M (Q₁, Q₃)5.09 (4.53–6.26)5.09 (4.50–6.36)5.09 (4.58–6.18)Z = 0.6090.542Cr(µmol/L), M (Q₁, Q₃)65.60 (55.80–77.10)65.60 (55.80–77.30)65.50 (56.30–76.45)Z = 0.0800.936UA(µmol/L), M (Q₁, Q₃)266.00 (206.25–327.00)265.00 (206.00–321.25)269.00 (208.85–332.00)Z = 1.1900.234Ca(mmol/L), M (Q₁, Q₃)2.20 (2.11–2.29)2.20 (2.11–2.29)2.19 (2.11–2.29)Z = 0.1060.916Gender, n(%)χ²=0.6560.418 Male626 (55.4)432 (54.61)194 (57.23) Female504 (44.6)359 (45.39)145 (42.77)Smoke, n(%)χ²=0.2810.596 NO916 (81.06)638 (80.66)278 (82.01) YES214 (18.94)153 (19.34)61 (17.99)Drinking, n(%)χ²=0.0160.899 NO959 (84.87)672 (84.96)287 (84.66) YES171 (15.13)119 (15.04)52 (15.34)Hepatitis, n(%)χ²=3.5150.061 NO1057 (93.54)747 (94.44)310 (91.45) YES73 (6.46)44 (5.56)29 (8.55)Hypertension, n(%)χ²=1.4620.227 NO710 (62.83)488 (61.69)222 (65.49) YES420 (37.17)303 (38.31)117 (34.51)Diabetes, n(%)χ²=0.6230.430 NO945 (83.63)657 (83.06)288 (84.96) YES185 (16.37)134 (16.94)51 (15.04)CHD, n(%)χ²=0.0430.836 NO1054 (93.27)737 (93.17)317 (93.51) YES76 (6.73)54 (6.83)22 (6.49)Cerebral infarction, n(%)χ²=0.3010.583 NO1039 (91.95)725 (91.66)314 (92.63) YES91 (8.05)66 (8.34)25 (7.37)Biliary tract operation, n(%)χ²=0.0930.761 NO1018 (90.09)714 (90.27)304 (89.68) YES112 (9.91)77 (9.73)35 (10.32)PAD, n(%)χ²=1.8270.176 NO819 (72.48)564 (71.30)255 (75.22) YES311 (27.52)227 (28.70)84 (24.78)Silt, n(%)χ²=1.1830.277 NO854 (75.58)605 (76.49)249 (73.45) YES276 (24.42)186 (23.51)90 (26.55)Multiple stones, n(%)χ²=0.8030.370 NO701 (62.04)484 (61.19)217 (64.01) YES429 (37.96)307 (38.81)122 (35.99)EST, n(%)χ²=0.0700.791 NO357 (31.59)248 (31.35)109 (32.15) YES773 (68.41)543 (68.65)230 (67.85)EPBD, n(%)χ²=2.9420.086 NO168 (14.87)127 (16.06)41 (12.09) YES962 (85.13)664 (83.94)298 (87.91)Suppurative cholangitis, n(%)χ²=1.1390.286 NO1066 (94.34)750 (94.82)316 (93.22) YES64 (5.66)41 (5.18)23 (6.78)Lithotripsy, n(%)χ²=2.2150.137 NO1058 (93.63)735 (92.92)323 (95.28) YES72 (6.37)56 (7.08)16 (4.72)Cholecystectomy, n(%)χ²=0.0000.990 NO587 (51.95)411 (51.96)176 (51.92) YES543 (48.05)380 (48.04)163 (48.08)Basket, n(%)χ²=0.8080.369 NO517 (45.75)355 (44.88)162 (47.79) YES613 (54.25)436 (55.12)177 (52.21)Biliary stent, n(%)χ²=0.0800.777 NO975 (86.28)684 (86.47)291 (85.84) YES155 (13.72)107 (13.53)48 (14.16)CBD stenosis, n(%)χ²=2.1730.140 NO1062 (93.98)738 (93.30)324 (95.58) YES68 (6.02)53 (6.70)15 (4.42)Group, n(%)χ²=0.6720.412 Non-recurrent952 (84.25)671 (84.83)281 (82.89) Recurrent178 (15.75)120 (15.17)58 (17.11) Table 2Comparison of data distribution between the training set and test setVariablesTotal (n = 1130)Training (n = 791)Test (n = 339)Statistic P Diameter(mm), M (Q₁, Q₃)8.00 (5.00–11.00)8.00 (5.00–11.50)8.00 (5.00–10.00)Z = 0.2620.795Age(year), M (Q₁, Q₃)64.00 (53.00–74.00)64.00 (53.00–74.00)63.00 (51.50–73.00)Z = 1.1140.265Course(day), M (Q₁, Q₃)14.00 (6.00–30.00)14.00 (6.00–30.00)14.00 (6.00–30.00)Z = 0.3860.700ALP(U/L), M (Q₁, Q₃)168.00 (105.90–285.60)171.00 (105.00–293.95)163.00 (108.10–268.00)Z = 0.2610.794GGT(U/L), M (Q₁, Q₃)244.30 (80.62–483.50)238.20 (75.25–465.10)252.40 (97.15–510.05)Z = 0.9900.322TBIL(µmol/L), M (Q₁, Q₃)20.25 (12.80–44.22)20.20 (12.90–44.50)20.30 (12.60–43.55)Z = 0.3290.742DBIL(µmol/L), M (Q₁, Q₃)9.80 (5.10–28.28)9.80 (5.10–28.75)9.90 (5.15–26.55)Z = 0.3370.736TC(mmol/L), M (Q₁, Q₃)4.19 (3.52–4.91)4.19 (3.51–4.93)4.22 (3.54–4.86)Z = 0.4320.666TG(mmol/L), M (Q₁, Q₃)1.32 (0.93–1.82)1.33 (0.94–1.81)1.31 (0.91–1.84)Z = 0.2740.784LDL-C(mmol/L), M (Q₁, Q₃)2.65 (2.16–3.19)2.65 (2.14–3.21)2.67 (2.21–3.18)Z = 0.3510.726FBG(mmol/L), M (Q₁, Q₃)5.09 (4.53–6.26)5.09 (4.50–6.36)5.09 (4.58–6.18)Z = 0.6090.542Cr(µmol/L), M (Q₁, Q₃)65.60 (55.80–77.10)65.60 (55.80–77.30)65.50 (56.30–76.45)Z = 0.0800.936UA(µmol/L), M (Q₁, Q₃)266.00 (206.25–327.00)265.00 (206.00–321.25)269.00 (208.85–332.00)Z = 1.1900.234Ca(mmol/L), M (Q₁, Q₃)2.20 (2.11–2.29)2.20 (2.11–2.29)2.19 (2.11–2.29)Z = 0.1060.916Gender, n(%)χ²=0.6560.418 Male626 (55.4)432 (54.61)194 (57.23) Female504 (44.6)359 (45.39)145 (42.77)Smoke, n(%)χ²=0.2810.596 NO916 (81.06)638 (80.66)278 (82.01) YES214 (18.94)153 (19.34)61 (17.99)Drinking, n(%)χ²=0.0160.899 NO959 (84.87)672 (84.96)287 (84.66) YES171 (15.13)119 (15.04)52 (15.34)Hepatitis, n(%)χ²=3.5150.061 NO1057 (93.54)747 (94.44)310 (91.45) YES73 (6.46)44 (5.56)29 (8.55)Hypertension, n(%)χ²=1.4620.227 NO710 (62.83)488 (61.69)222 (65.49) YES420 (37.17)303 (38.31)117 (34.51)Diabetes, n(%)χ²=0.6230.430 NO945 (83.63)657 (83.06)288 (84.96) YES185 (16.37)134 (16.94)51 (15.04)CHD, n(%)χ²=0.0430.836 NO1054 (93.27)737 (93.17)317 (93.51) YES76 (6.73)54 (6.83)22 (6.49)Cerebral infarction, n(%)χ²=0.3010.583 NO1039 (91.95)725 (91.66)314 (92.63) YES91 (8.05)66 (8.34)25 (7.37)Biliary tract operation, n(%)χ²=0.0930.761 NO1018 (90.09)714 (90.27)304 (89.68) YES112 (9.91)77 (9.73)35 (10.32)PAD, n(%)χ²=1.8270.176 NO819 (72.48)564 (71.30)255 (75.22) YES311 (27.52)227 (28.70)84 (24.78)Silt, n(%)χ²=1.1830.277 NO854 (75.58)605 (76.49)249 (73.45) YES276 (24.42)186 (23.51)90 (26.55)Multiple stones, n(%)χ²=0.8030.370 NO701 (62.04)484 (61.19)217 (64.01) YES429 (37.96)307 (38.81)122 (35.99)EST, n(%)χ²=0.0700.791 NO357 (31.59)248 (31.35)109 (32.15) YES773 (68.41)543 (68.65)230 (67.85)EPBD, n(%)χ²=2.9420.086 NO168 (14.87)127 (16.06)41 (12.09) YES962 (85.13)664 (83.94)298 (87.91)Suppurative cholangitis, n(%)χ²=1.1390.286 NO1066 (94.34)750 (94.82)316 (93.22) YES64 (5.66)41 (5.18)23 (6.78)Lithotripsy, n(%)χ²=2.2150.137 NO1058 (93.63)735 (92.92)323 (95.28) YES72 (6.37)56 (7.08)16 (4.72)Cholecystectomy, n(%)χ²=0.0000.990 NO587 (51.95)411 (51.96)176 (51.92) YES543 (48.05)380 (48.04)163 (48.08)Basket, n(%)χ²=0.8080.369 NO517 (45.75)355 (44.88)162 (47.79) YES613 (54.25)436 (55.12)177 (52.21)Biliary stent, n(%)χ²=0.0800.777 NO975 (86.28)684 (86.47)291 (85.84) YES155 (13.72)107 (13.53)48 (14.16)CBD stenosis, n(%)χ²=2.1730.140 NO1062 (93.98)738 (93.30)324 (95.58) YES68 (6.02)53 (6.70)15 (4.42)Group, n(%)χ²=0.6720.412 Non-recurrent952 (84.25)671 (84.83)281 (82.89) Recurrent178 (15.75)120 (15.17)58 (17.11) Comparison of data distribution between the training set and test set Data processing before modeling Initially, we employed the Boruta algorithm for a preliminary selection of important features (Fig. 2a). The fundamental principle of the Boruta algorithm is the random creation of artificial variables within the dataset, known as shadow variables [12]. A real variable is deemed significant within the model if its importance measure surpasses that of the shadow variables [10]. We performed 500 iterations in total (Fig. 2b). We then used univariate LR and identified 9 important features: Drinking, Biliary tract operation, Multiple stones, EPBD, Biliary stent, Lithotripsy, CBD stenosis, Diameter, Course. Features with a p-value < 0.05 in univariate LR were included in the subsequent multivariate analysis. We excluded “Lithotripsy (p-value = 0.48)” by multivariate LR. A total of 8 key features were ultimately used to construct the machine learning model, including Drinking, Biliary tract operation, Multiple stones, EPBD, Biliary stent, CBD stenosis, Diameter, Course (Fig. 2c). Lastly, we utilized the SMOTE for data balancing [13]. The imbalanced training set consisted of 671 people in the non-recurrent group and 120 in the recurrent group. Following the application of SMOTE, we obtained a balanced final training set with 671 people in each of the non-recurrent and recurrent groups. Fig. 2Screening of important features. a The outcome of important feature selection using the Boruta algorithm. The horizontal axis represents the names of the features. The vertical axis represents the average measure of variable importance, known as the mean decrease accuracy. Green denotes features with the highest mean decrease accuracy values and are considered important. Red indicates features with lower measure values considered unimportant and should not be included in the model. Features for which the Boruta method could not ascertain the importance are marked in yellow. b The iterative trajectory of the Boruta method. The horizontal axis is the number of runs, and the vertical axis is the Z-scores. After 500 iterations, the model is relatively stable. Green stands for confirmed features, yellow for features pending confirmation, red for rejected features, and blue represents the shadow features. c Univariate and multivariate LR analyses. Univariate LR analysis identified 9 meaningful features, and after multivariate LR analysis on these 9 features, ultimately 8 features were deemed significant Screening of important features. a The outcome of important feature selection using the Boruta algorithm. The horizontal axis represents the names of the features. The vertical axis represents the average measure of variable importance, known as the mean decrease accuracy. Green denotes features with the highest mean decrease accuracy values and are considered important. Red indicates features with lower measure values considered unimportant and should not be included in the model. Features for which the Boruta method could not ascertain the importance are marked in yellow. b The iterative trajectory of the Boruta method. The horizontal axis is the number of runs, and the vertical axis is the Z-scores. After 500 iterations, the model is relatively stable. Green stands for confirmed features, yellow for features pending confirmation, red for rejected features, and blue represents the shadow features. c Univariate and multivariate LR analyses. Univariate LR analysis identified 9 meaningful features, and after multivariate LR analysis on these 9 features, ultimately 8 features were deemed significant Model construction and evaluation We trained three machine learning models on the training set: RF, XGBoost, and LR. Hyperparameters were tuned using the random search method, including 100 iterations and ten-fold cross-validation. The optimal parameters for the three models are detailed in Table 3. The ROC curve for each model was plotted and their AUC values compared (Fig. 3a and b), showing the RF having the highest in the test set. To more comprehensively evaluate the performance of the three models, accuracy, recall, precision, and F1-score for each model were calculated on the test set (Table 3). The F1-score is the harmonic mean of precision and recall. Thus, the F1-score is considered a more reasonable measure of a predictive model’s overall performance [14, 15]. We can see that the predictive model based on the RF algorithm has the best performance in all aspects. Table 3The performance and optimal parameters of the three models on the test setModelAccuracyRecallPrecisionF1-scoreOptimal parametersRF0.780.530.390.45‘max_depth’: 62, ‘max_features’: ‘sqrt’, ‘min_samples_leaf’: 1, ‘min_samples_split’: 6, ‘n_estimators’: 434LR0.740.480.330.39‘C’: 0.110361931233146, ‘solver’: ‘liblinear’XGBoost0.770.470.360.41‘colsample_bytree’: 0.85, ‘learning_rate’: 0.01, ‘max_depth’: 462, ‘n_estimators’: 1742, ‘subsample’: 0.83 Table 3The performance and optimal parameters of the three models on the test setModelAccuracyRecallPrecisionF1-scoreOptimal parametersRF0.780.530.390.45‘max_depth’: 62, ‘max_features’: ‘sqrt’, ‘min_samples_leaf’: 1, ‘min_samples_split’: 6, ‘n_estimators’: 434LR0.740.480.330.39‘C’: 0.110361931233146, ‘solver’: ‘liblinear’XGBoost0.770.470.360.41‘colsample_bytree’: 0.85, ‘learning_rate’: 0.01, ‘max_depth’: 462, ‘n_estimators’: 1742, ‘subsample’: 0.83 The performance and optimal parameters of the three models on the test set The aforementioned evaluation metrics primarily focus on the model’s performance and do not intuitively reflect the model’s clinical value. Therefore, we also calculated the clinical benefits of each model and plotted the DCA curves (Fig. 3c and d). In the DCA, the x‑axis (threshold probability) represents the minimum probability of recurrence at which a clinician or patient would opt for an intervention (e.g., intensified follow‑up or prophylactic measures). The y‑axis measures the net benefit, which accounts for both true positives and false positives. In the test set (Fig. 3d), across most clinically relevant threshold probabilities (approximately 0.01–0.30), the RF model’s curve lies consistently above those of the XGBoost and LR models, as well as above the two benchmark strategies (“treat all” and “treat none”). This visually demonstrates that the RF model provides the highest net clinical benefit within this range. DCA curve analysis is part of the standard TRIPOD procedure for constructing clinical prediction models [16]. The RF prediction model provides the highest clinical benefit in the test set (Fig. 3d). Fig. 3Evaluation of the performance of the three models. The ROC curves of the three models on the training set (a) and the test set (b). The DCA curves of the three models on the training set (c) and the test set (d). In the training set, thresholds with clinical value are as follows: LR (0.127–0.599), RF (0–0.599), XGBoost (0–0.599). In the test set, thresholds with clinical value are as follows: LR (0.127–0.203), RF (0.012–0.030, 0.034–0.299), XGBoost (0–0.002, 0.089–0.299) Evaluation of the performance of the three models. The ROC curves of the three models on the training set (a) and the test set (b). The DCA curves of the three models on the training set (c) and the test set (d). In the training set, thresholds with clinical value are as follows: LR (0.127–0.599), RF (0–0.599), XGBoost (0–0.599). In the test set, thresholds with clinical value are as follows: LR (0.127–0.203), RF (0.012–0.030, 0.034–0.299), XGBoost (0–0.002, 0.089–0.299) Model interpretation The SHAP summary plot (Fig. 4a) indicates that the importance of each feature to the prediction value, from highest to lowest, is as follows: EPBD, Course, Diameter, Multiple stones, Biliary stent, Drinking, Biliary tract operation, CBD stenosis. Among them, EPBD has a negative contribution to the model’s prediction of stone recurrence, while the others have a positive contribution to the model’s predicted value. Fig. 4SHAP explainability of the RF prediction model. a SHAP summary plot derived from the training set, providing a global interpretation of feature importance. Blue indicates a negative contribution to the predicted outcome (recurrence), and red indicates a positive contribution; stripe length represents the magnitude of contribution. b–e Local explanations for individual patients from the test set (not used during training): (b) waterfall plot for patient #326, (c) waterfall plot for patient #321, (d) force plot for patient #326, (e) force plot for patient #321 SHAP explainability of the RF prediction model. a SHAP summary plot derived from the training set, providing a global interpretation of feature importance. Blue indicates a negative contribution to the predicted outcome (recurrence), and red indicates a positive contribution; stripe length represents the magnitude of contribution. b–e Local explanations for individual patients from the test set (not used during training): (b) waterfall plot for patient #326, (c) waterfall plot for patient #321, (d) force plot for patient #326, (e) force plot for patient #321 Figure 4b shows the RF model’s prediction process for the 326th patient in the test set. The largest stone’s diameter being 13 mm, the initial disease course of 32 days, and multiple stones have a positive contribution to the prediction of stone recurrence, while EPBD has a negative contribution. Figure 4c illustrates the RF model’s prediction process for the 321st patient in the test set. Biliary stenting, the largest stone diameter of 40 mm, an initial disease course of 180 days, alcohol history, and multiple stones positively contribute to the prediction of stone recurrence, while EPBD and the absence of CBD narrowing negatively contribute. Force plots (Fig. 4d and e) are another method to display the RF model’s individual prediction process. Discussion In this study, we utilized three machine learning algorithms: RF, XGBoost, and LR to develop prediction models for the recurrence of CBDS after ERCP. Upon comprehensive comparison, we found that the RF model had the best clinical value. In a highly imbalanced dataset (recurrence group only 15.75%), models often sacrifice precision to achieve higher recall, especially when using a default probability threshold for classification. The current model’s performance profile (higher recall, moderate precision) might be more suitable for screening high-risk patients (avoiding missed cases) rather than for definitive diagnosis. Furthermore, by interpreting the RF model using SHAP, we identified the following key factors influencing the recurrence of CBDS after ERCP, in order of significance: EPBD, Course, Diameter, Multiple stones, Biliary stent, Drinking, Biliary tract operation, and CBD stenosis. EPBD is a protective factor against the recurrence of CBDS after ERCP. EPBD preserves about 70% of the Oddi sphincter function, which significantly reduces the risk of intestinal reflux, retrograde bacterial infection, and consequently, the recurrence of stones[17]. Studies indicate that the recurrence rate of stones is lower when using EPBD alone or in conjunction with EST to treat CBDS compared to using EST alone [18, 19]. A meta‑analysis by Liu et al. confirmed that EPBD with prior limited EST significantly improved stone clearance rates compared with EPBD alone (OR = 2.47), without increasing recurrence or major complications [17]. However, the optimal endoscopic papillary management strategy for CBDS remains controversial, and EPBD is not without risks. EPBD alone has been associated with a higher risk of post‑ERCP pancreatitis (PEP) compared with EST alone, particularly when applied to large or multiple stones without prior sphincterotomy. Therefore, ongoing debates persist regarding optimal balloon size, the extent of EST, and patient subgroups most likely to benefit from each strategy. Our study further supports the use of EPBD (particularly when combined with limited EST) as a protective strategy against CBDS recurrence, while acknowledging that careful patient selection is necessary to minimize the risk of PEP. We did not systematically record post‑ERCP pancreatitis, so the trade‑off between reduced recurrence and procedural risks could not be directly assessed in this cohort. The duration of the initial illness episode is another important factor. The longer the duration of the first episode of illness, the greater the likelihood of recurrence after CBDS surgery. This may be due to long-standing, untreated stones continuously irritating the bile duct, with recurrent inflammation causing bile duct epithelial cell necrosis and scarring, which in turn leads to bile duct constriction and bile excretion obstruction. Although some studies have identified that an initial illness duration of ≥ 10 days is an independent risk factor for the recurrence of stones after ERCP [20]. However, there is limited literature on this topic, and further exploration is needed into the effect and mechanism of the initial course of illness on postoperative CBDS recurrence. The diameter of CBDS is directly proportional to the probability of its recurrence. A maximum stone diameter of ≥ 10 mm is an independent risk factor for recurrence [5]. This is because the larger the stone, the greater the likelihood of common bile duct dilatation. Dilation of the common bile duct can easily cause bile stasis and bacterial infection [21]. Additionally, larger CBDS may require endoscopic mechanical lithotripsy, and it is difficult to completely clear small stone fragments after lithotripsy, which can easily reform into stones [22]. The recurrence of CBDS after ERCP is also related to the number of stones. Our research findings are consistent with those of Yoo [23] and Peng [24] that multiple stones (stone count ≥ 2) promote the recurrence of stones. This may be due to the generally smaller size of multiple CBDS, which are not easily completely cleared by ERCP stone extraction and require multiple retrievals, making bile duct injury and inflammation more likely. However, some studies suggest that there is no significant correlation between the number of CBDS and its recurrence, which may be due to inconsistent definitions of stone numbers and significant bias in retrospective clinical studies, warranting further research into the relationship between the two [6]. Biliary stent is a common methods of bile drainage that can alleviate bile stasis and cholangitis. However, our research has found that biliary stents may promote the recurrence of CBDS. As demonstrated by Kaneko et al. [25], the placement of plastic stents is an independent risk factor for the formation of “stent-stone complex”. This is because bile salts tend to deposit and adhere to the stent, becoming a nidus for recurrent CBDS. Additionally, long-term stent placement will affect the dynamics of the bile duct, making bile stasis and concentration more likely to occur. Alcohol consumption can promote the recurrence of CBDS after ERCP. As a previous study has shown, drinking at least once a week increases the risk of recurrent biliary complications [26]. This is because alcohol inhibits the activity of the Oddi sphincter, causing delayed biliary drainage and biliary complications [27, 28]. Additionally, factors related to metabolic syndrome caused by drinking are closely associated with the occurrence of CBDS [3]. Patients with a history of biliary surgery have a higher recurrence rate of CBDS, which may be due to unavoidable iatrogenic damage leading to bile duct epithelial cell necrosis and scar formation, resulting in bile duct stricture and bile excretion disorder [5]. In addition, suturing after choledochotomy can affect the angulation of the common bile duct, promoting the recurrence of CBDS [21]. The mechanism by which CBD stricture promotes recurrent CBDS is relatively easy to understand, primarily due to impaired bile excretion and recurrent cholangitis. High concentrations of glycoproteins, calcium ions, and oxygen-free radicals in stagnant bile can easily form the core of gallstones. Recurrent infections not only damage the normal functions of the bile ducts but also lead to an imbalance in the richness and diversity of the microbiome in the bile, which in turn promotes the recurrence of stones [29, 30]. Some features not included in the model construction are worth discussing, such as EST, where damage to the Oddi’s sphincter is inevitable during EST. When the sphincter’s contraction is impaired, the alkaline fluid from the small intestine can reflux into the common bile duct. The irritation from intestinal fluids leads to unstable circulation in the common bile duct, making bile duct infections more likely, thereby promoting the recurrence of CBDS [31]. PAD, although a non-significant feature in our study, has been shown in previous research to increase the risk of CBDS recurrence due to compression of the common bile duct and secondary infections caused by food residues in PAD [32]. Additionally, features not covered in our study, such as angulation of the common bile duct, the nature of the stones, and the microbiome within the bile duct have also been proven to be risk factors for CBDS recurrence after ERCP [12]. Our study presents several significant advantages. Firstly, we employed the Boruta algorithm for feature selection. This method takes into account the interactions between features while effectively filtering out those that do not have a substantial impact, yielding more comprehensive and accurate results in feature selection. Furthermore, in contrast to many studies that neglect the issue of data imbalance, we applied SMOTE to balance the data distribution. Secondly, we utilized three machine learning algorithms and conducted a comparative analysis from multiple perspectives, ultimately identifying the model with the highest clinical application value. Lastly, we used SHAP to quantify the contribution of each feature to the final prediction results, enhancing the interpretability of the model. This study has several limitations. First, it is a single‑center retrospective study without external validation. The retrospective design may have introduced selection bias, as patients with incomplete clinical data were excluded, and enrollment was not consecutive according to a predefined prospective protocol. Second, follow‑up imaging was not standardized: the timing and modality of imaging (ultrasound, CT, MRI, MRCP, or ERCP) were determined by the attending physicians rather than a fixed protocol. This variability may have led to underdetection or delayed detection of recurrent stones, potentially affecting the accuracy of the recurrence rate and model performance. Third, despite adjusting for multiple procedure‑related variables (e.g., EPBD, biliary stents, lithotripsy) in our feature selection and model construction, residual confounding by ERCP procedure selection remains possible. The choice of specific endoscopic techniques was not randomized but depended on the endoscopist’s preference and intraoperative findings; unmeasured factors such as endoscopist experience, difficulty of cannulation, or anatomical variations may have influenced both the procedure performed and the risk of recurrence. Fourth, the information collected in this study is limited. Several potentially important factors—including the angulation of the common bile duct, the biochemical composition of stones, and the biliary microbiome—were not available in our database and could enhance the model’s performance if incorporated in future studies. Therefore, the next critical step based on our constructed high‑performance RF model and the eight identified key features is to conduct a prospective, multi‑center external validation study across different populations and endoscopic practice settings to confirm its generalizability and robustness. Moreover, it is feasible to integrate the present model into a clinical decision support system (CDSS): with a well‑designed data interface that captures the eight predictors (e.g., EPBD, stone diameter, multiple stones, etc.) from electronic health records, the model could provide real‑time individualized recurrence risk estimates at the bedside or endoscopy suite. Such a CDSS would assist clinicians in stratifying patients for tailored post‑ERCP follow‑up and preventive interventions, thereby translating the predictive model into direct clinical benefit. Conclusions We constructed predictive models for the recurrence of CBDS after ERCP using three machine learning algorithms: RF, XGBoost, and LR. Among these, the RF model demonstrated the best performance. The significant factors influencing the recurrence of CBDS after ERCP include EPBD, clinical course, stone diameter, presence of multiple stones, use of biliary stents, alcohol consumption, history of biliary tract operations, and presence of CBD stenosis. This study aids in identifying high-risk groups for CBDS recurrence post-ERCP and provides a reference for machine learning-assisted clinical decision-making.