Forecasting energy consumption can play an important role in an organization to improve the rate of energy consumption by making the right decisions at the right time. In this article, I will walk you through the task of Energy consumption prediction with machine learning using Python.
Energy Consumption Prediction with Machine Learning
With the significant growth of the population, more energy is consumed. According to research and statistics, energy consumption is expected to be in considerable proportions. For example, statistics from China show that energy consumption was around 28% in 2011, they predicted it could reach around 35% in 2020, so by analyzing the increasing rate, they can take better decisions at the right time to control the rate of energy consumption.
There are so many methods to predict the rate of energy consumption. The main methods depend on historical data. We can use the historical data time series to create prediction models.
In the section below, I will take you through the task of Energy Consumption prediction with Machine Learning using Python programming language.
Energy Consumption Prediction with Python
By using the historical data we can predict future energy consumption. Here, I will use the electric power consumption data of one household. Let’s import the dataset and let’s get started with the task:
The dataset contains 2,075,259 rows and 7 columns, let’s take a look at the number of null values:
Global_active_power 25979 Global_reactive_power 25979 Voltage 25979 Global_intensity 25979 Sub_metering_1 25979 Sub_metering_2 25979 Sub_metering_3 25979 dtype: int64
We have so many null values in the dataset, I will fill these null values with the mean values:
df = df.fillna(df.mean())
Let’s have a look at the data more closely by visualizing it:
Observations from the above visualizations:
- Resampling by month, date or time is very important because it has a great interaction as expected (changing the periodicity of the system).
- Therefore, if you process all the original data, the run time will be very expensive, but if you process data with large timescale samples (e.g., monthly), it will affect the predictability of the model.
- From observation, we can see it is relatively reasonable to resample the data per hour.
Using the LSTM Model
For the task of energy consumption prediction with Machine Learning, I will use the LSTM model because it is very well suited for large time-series data. I’ll start this step by preparing a helper function to frame the problem:
To reduce the time of the calculation and get the results fast, I will resample the dataset:
df_resample = df.resample('h').mean() df_resample.shape
So we will have 7 input variables and 1 output variable. Let’s split and prepare the data from the LSTM model:
Now let’s use the LSTM model for the task of energy consumption prediction. Below are the parameters that I will use in the LSTM model:
- 100 neurons in the first visible layer
- dropout 10%
- 1 neuron in the output layer to predict Global_active_power
- The entry form will be a step with 7 features
- The loss function mean_squared_error and Adam’s efficient version of stochastic gradient descent
- The model will be suitable for 50 training periods with a lot size of 70.
I hope you liked this article on Energy Consumption Prediction with Machine Learning using Python. Feel free to ask your valuable questions in the comments section below.