Neonatal necrotizing enterocolitis (NEC) is a severe gastrointestinal disease that primarily affects preterm and very low birth weight infants, with high morbidity and mortality. We aim to build a reliable prediction model to predict the risk of NEC in preterm and very low birth weight infants. We conducted a retrospective analysis of medical data from infants (gestational age <32 weeks, birth weight <1,500 g) admitted to Maternal and Child Health Hospital of Guangxi Zhuang Autonomous Region. We collected clinical data, randomly dividing it into an 8:2 ratio for training and testing. Multivariate logistic regression was employed to identify significant predictors for NEC. Principal component analysis was used for dimensionality reduction of numerical variables. The prediction model was constructed through logistic regression, incorporating all relevant variables. Subsequently, we calculated performance evaluation metrics, including Receiver Operating Characteristic (ROC) curves and confusion matrices. Additionally, we conducted model performance comparisons with common machine learning models to establish its superiority. A total of 292 infants were included, with 20% (n = 58) randomly selected for external validation. Multivariate logistic regression revealed the significance of four predictors for NEC in preterm and very low birth weight infants: temperature (P = 0.003), Apgar score at 5 min (P = 0.004), formula feeding (P = 0.007), and gestational diabetes mellitus (GDM, P = 0.033). The model achieved an accuracy of 82.46% in the test set with an F1 score of 0.90, outperforming other machine learning models (support vector machine, random forest). Our logistic regression model effectively predicts NEC risk in preterm and very low birth weight infants, as confirmed by external validation. Key predictors include temperature, Apgar score at 5 min, formula feeding, and GDM. This study provides a vital tool for NEC risk assessment in this population, potentially improving early interventions and child survival. However, clinical validation and further research are necessary for practical application.