# Energy Consumption Prediction with Machine Learning

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.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

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:

`(2075259, 7)`

The dataset contains 2,075,259 rows and 7 columns, let’s take a look at the number of null values:

`df.isnull().sum()`
```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())`

## Data Visualization

Let’s have a look at the data more closely by visualizing it:

Observations from the above visualizations:

1. Resampling by month, date or time is very important because it has a great interaction as expected (changing the periodicity of the system).
2. 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.
3. 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:

1. 100 neurons in the first visible layer
2. dropout 10%
3. 1 neuron in the output layer to predict Global_active_power
4. The entry form will be a step with 7 features
5. The loss function mean_squared_error and Adam’s efficient version of stochastic gradient descent
6. The model will be suitable for 50 training periods with a lot size of 70.