The seismic stability of unsupported rectangular excavations poses significant challenges in geotechnical engineering, especially in underground structures. This study addresses the need for accurate prediction methods to assess the vulnerability of such excavations under seismic loading conditions. This study addresses seismic stability in excavations and underground structures using a random forest (RF) model with three distinct optimization algorithms: the whale optimization algorithm (WOA), dragonfly optimization algorithm (DOA), and sparrow search optimization algorithm (SSOA). The method focuses on four dimensionless factors, with the seismic stability number (N) serving as the output. The results obtained from the proposed data-driven models indicate that the RF-DOA model has the best predictive performance and highest accuracy. In addition, scatter plots, error plots, line plots, and Taylor diagrams were generated to compare the performances of all the proposed models. Shapley analysis showed that the soil friction angle (Ï•) is the most significant influencing factor, and the horizontal seismic coefficient (k_h) is the least significant influencing factor. This research advances seismic stability prediction for underground structures, providing models for designing earthquake-resistant excavations. The RF-DOA hybrid model is highlighted for its practicality and efficiency in predicting seismic stability, proving essential for geotechnical engineering applications.