Myasthenia gravis (MG) is an autoimmune disorder that causes muscle weakness. Although the management is well established, some patients are refractory and require prolonged hospitalization. Our study is aimed to identify the important factors that predict the duration of hospitalization in patients with MG by using machine learning methods. A total of 21 factors were chosen for machine learning analyses. We retrospectively reviewed the data of patients with MG who were admitted to hospital. Five machine learning methods, including stochastic gradient boosting (SGB), least absolute shrinkage and selection operator (Lasso), ridge regression (Ridge), eXtreme gradient boosting (XGboost), and gradient boosting with categorical features support (Catboost), were used to construct models for identify the important factors affecting the duration of hospital stay. A total of 232 data points of 204 hospitalized MG patients admitted were enrolled into the study. The MGFA classification, treatment of high-dose intravenous corticosteroid, age at admission, treatment with intravenous immunoglobulins, and thymoma were the top five significant variables affecting prolonged hospitalization. Our findings from machine learning will provide physicians with information to evaluate the potential risk of MG patients having prolonged hospital stay. The use of high-dose corticosteroids is associated with prolonged hospital stay and to be used cautiously in MG patients.