Surface wave methods have received much attention due to their efficient, flexible and convenient characteristics. However, there are still critical issues regarding a key step in surface wave inversion. In most existing methods, the number of layers is assumed to be known prior to the process of inversion. However, improper assignment of this parameter leads to erroneous inversion results. A Bayesian nonparametric method for Rayleigh wave inversion is proposed herein to address this problem. In this method, each model class represents a particular number of layers with unknown S-wave velocity and thickness of each layer. As a result, determination of the number of layers is equivalent to selection of the most applicable model class. Regarding each model class, the optimization search of S-wave velocity and thickness of each layer is implemented by using a genetic algorithm. Then, each model class is assessed in view of its efficiency under the Bayesian framework and the most efficient class is selected. Simulated and actual examples verify that the proposed Bayesian nonparametric approach is reliable and efficient for Rayleigh wave inversion, especially for its capability to determine the number of layers.