Heat stress poses a significant challenge to livestock farming, particularly affecting the health and productivity of high-yield dairy cows. This study develops a machine learning framework aimed at predicting the core body temperature (CBT) of dairy cows to enable more effective heat stress management and enhance animal welfare. The dataset includes 3005 records of physiological data from real-world production environments, encompassing environmental parameters, individual animal characteristics, and infrared temperature measurements. Employed machine learning algorithms include elastic net (EN), artificial neural networks (ANN), random forests (RF), extreme gradient boosting (XGBoost), light gradient boosting machine (LightGBM), and CatBoost, alongside several optimization algorithms such as Bayesian optimization (BO) and grey wolf optimizer (GWO) to refine model performance through hyperparameter tuning. Comparative analysis of various feature sets reveals that the feature set incorporating the average infrared temperature of the trunk (IRTave_TK) excels in CBT prediction, achieving a coefficient of determination (R2) value of 0.516, mean absolute error (MAE) of 0.239 °C, and root mean square error (RMSE) of 0.302 °C. Further analysis shows that the GWO-XGBoost model surpasses others in predictive accuracy with an R2 value of 0.540, RMSE as low as 0.294 °C, and MAE of just 0.232 °C, and leads in computational efficiency with an optimization time of merely 2.41 s-approximately 4500 times faster than the highest accuracy model. Through SHAP (SHapley Additive exPlanations) analysis, IRTave_TK, time zone (TZ), days in lactation (DOL), and body posture (BP) are identified as the four most critical factors in predicting CBT, and the interaction effects of IRTave_TK with other features such as body posture and time periods are unveiled. This study provides technological support for livestock management, facilitating the development and optimization of predictive models to implement timely and effective interventions, thereby maintaining the health and productivity of dairy cows.