The surface wave method is an efficient non-invasive technique to infer shear wave velocity profiles and has wide applications in civil engineering, earthquake engineering, and geophysics. Inversion analysis is a key step in this method. However, inferring the soil layer number is a challenging problem in inversion analysis and needs improvement. Machine-learning algorithms, including decision tree and random forest, are used in this study to infer the number of soil layers based on the dispersion curve of the surface wave. This process includes two steps for obtaining the model. In Step 1, a large set of synthetic dispersion curves is produced, and in Step 2, a model using machine-learning algorithms is trained based on the synthetic data. The analysis results showed that the obtained model could gain more than 75% accuracy in the test data. Comparing the results from the two machine learning models indicated that the random forest method produced a more favorable prediction. Furthermore, investigating the importance of features implied that the longest and shortest wavelengths were more important than other features.