Wireless sensor networks (WSNs) are widely utilized in various applications due to their compact size, cost-effectiveness, and ease of deployment. Nonetheless, one of the biggest problems in WSNs is getting a reasonable estimate of the average location error of a node at the setup in the least amount of time. Wireless sensor networks can undergo changes over time due to various external and internal factors, such as environmental conditions, network congestion, hardware failures, or software updates. When these changes occur, the network may require redesigning, which can incur significant expenses. Traditional WSNs approaches, on the other hand, have been explicitly programmed, which makes it hard for networks to respond dynamically. Therefore, machine learning (ML) techniques can be used to respond appropriately in such scenarios. In this work, we proposed an optimized ML ensemble model for (i) identifying the critical network parameters for node localization when setting up wireless sensor networks with the accuracy needed in a short amount of time and (ii) predicting the average localization error of wireless sensor networks. We used the random forest algorithm with optimized hyperparameters from different optimization techniques to predict average localization error (ALE) using independent features like node density, anchor ratio, transmission range, and iterations.