Drug efficacy can be improved by understanding the effects of anesthesia on the neurovascular system. In this study, we used machine learning algorithms to predict the risk of infection in postoperative intensive care unit (ICU) patients who are on non-mechanical ventilation and are receiving hydromorphone analgesia. In this retrospective study, 130 patients were divided into high and low dose groups of hydromorphone analgesic pump patients admitted after surgery. The white blood cells (WBC) count and incidence rate of infection was significantly higher in the high hydromorphone dosage group compared to the low hydromorphone dosage groups (p < 0.05). Furthermore, significant differences in age (P = 0.006), body mass index (BMI) (P = 0.001), WBC count (P = 0.019), C-reactive protein (CRP) (P = 0.038), hydromorphone dosage (P = 0.014), and biological sex (P = 0.024) were seen between the infected and non-infected groups. The infected group also had a longer hospital stay and an extended stay in the intensive care unit compared to the non-infected group. We identified important risk factors for the development of postoperative infections by using machine learning algorithms, including hydromorphone dosage, age, biological sex, BMI, and WBC count. Logistic regression analysis was applied to incorporate these variables to construct infection prediction models and nomograms. The area under curves (AUC) of the model were 0.835, 0.747, and 0.818 in the training group, validation group, and overall pairwise column group, respectively. Therefore, we determined that hydromorphone dosage, age, biological sex, BMI, WBC count, and CRP are significant risk factors in developing postoperative infections.