Global warming, environmental pollution, and the soaring cost of energy consumption for ships have drawn the attention of the International Maritime Organization and the shipping industry. By reducing the energy consumption of ships, the greenhouse gas emissions and operating costs of ships can be effectively reduced simultaneously. However, current research on the ship energy consumption optimization based on operating mode is mainly focused on route and speed optimization and less on trim optimization, but ship trim is also an important factor affecting energy consumption. Therefore, this study proposed a ship trim optimization method based on operational data and ensemble learning to achieve energy savings and emission reductions for inland sea ships. First, data processing and feature selection of operational data from an inland ro-ro passenger ship were undertaken. Second, the energy consumption prediction models were established based on ensemble learning. Finally, the trim optimization model was developed by combining the energy consumption model with the best prediction performance and enumeration method. Experimental results show that compared with linear regression, neural networks, and support vector machines, ensemble learning methods have better prediction performance in energy consumption modeling, and the extra tree (ET) model has the best prediction performance. With the trim optimization, the energy consumption and carbon emissions of a ro-ro passenger ship can be reduced by 1.4641%, which is conducive to the green and low-carbon navigation of ships.