As an environmentally friendly and efficient public transport, the optimization of the operating frequency of electric buses is of great significance for improving passenger satisfaction and reducing operating costs. This paper proposes an optimal electric bus frequency setting method that combines LSTM prediction and two-layer planning. First, LSTM neural network is utilized to predict the passenger flow of electric buses. Second, a two-layer planning model is constructed, with the upper model aiming at frequency optimization and the lower model aiming at electric bus frequency setting. Finally, this two-layer planning model is solved by genetic algorithm to obtain the optimal electric bus frequency setting. The inbound and outbound passenger flow data of the 5th station of 363 electric bus in Q city are used for practical verification. The prediction results of the LSTM model on inbound and outbound passenger flow on weekdays and natural days are basically consistent with the actual values. The optimal frequency of 62 trips was solved using genetic algorithm. The maximum deviation of the actual capacity supply from the actual capacity demand curve is only 0.09% when the frequency setting is verified under the scenario of thousands of passenger flows. From the above analysis, it is shown that it is practical to design the optimal electric bus frequency using LSTM prediction and two layer planning model.