Recent studies have shown that the IEEE 802.15.4 MAC protocol may suffer from severe limitations, in terms of reliability and energy efficiency, if CSMA/CA parameter settings are not appropriate. On the other hand, choosing the optimal setting that guarantees the application requirements with minimum energy consumption, may not be a trivial task in a real environment, where the operating conditions change over time. In this paper we propose a LEarning-based Adaptive Parameter tuning (LEAP) algorithm that, in addition to adapting the CSMA/CA parameter settings to the time-variant operating conditions, also exploits the past history to figure out the most appropriate settings for the current conditions. Simulation results show that, in stationary conditions, the performance of the proposed algorithm is very close to an ideal (but unfeasible) algorithm. It is shown that LEAP is able to select in dynamic scenarios the optimal settings faster than related algorithms.