Introduction: Postherpetic neuralgia (PHN) is a neuropathic pain secondary to shingles. Studies have shown that early pain intervention can reduce the incidence or intensity of PHN. The aim of this study was to predict whether a patient with acute herpetic neuralgia will develop PHN and to help clinicians make better decisions. Method: Five hundred two patients with shingles were reviewed and classified according to whether they had PHN. The risk factors associated with PHN were determined by univariate analysis. Logistic regression and random forest algorithms were used to do machine learning, and then the prediction accuracies of the two algorithms were compared, choosing the superior one to predict the next 60 new cases. Results: Age, NRS score, rash site, Charlson comorbidity index (CCI) score, antiviral therapy and immunosuppression were found related to the occurrence of PHN. The NRS score was the most closely related factor with an importance of 0.31. As for accuracy, the random forest was 96.24%, better than that of logistic regression in which the accuracy was 92.83%. Then, the random forest model was used to predict 60 newly diagnosed patients with herpes zoster, and the accuracy rate was 88.33% with a 95% confidence interval (CI) of 77.43-95.18%. Conclusions: This study provides an idea and a method in which, by analyzing the data of previous cases, we can develop a predictive model to predict whether patients with shingles will develop PHN.