How effective is predicting daily energy production/consumption using Long and Short Term Memory (LSTM)
How effective is predicting daily energy production/consumption using Long and Short Term Memory (LSTM)

How effective is predicting daily energy production/consumption using Long and Short Term Memory (LSTM)
In this blog I will cover the performance of LSTM in predicting the daily production of electricity using a time series data. I’m using 186 daily energy production data collected and is used to train the next 20% of this day.
It’s been a while since I wrote an article here. After passing away of my father, I decided to take a long break from writing blogs in memory of him. This is my first blog since then.
Ok, let’s continue. While reading this article, if you are interested in trying to check the performance of the model by yourself, you can access the data from my GitHub account Here. Please note that this data is not actual data and then used for educational purpose only. The data also used the daily total energy production (consumption) and not a hourly basis. As this is for educational purpose only, we do not require the hourly energy consumption.
Our first work is to ensure that we have all necessary resources. To do so, for example a time series data, python & its ide installed in our laptop. You can use Anaconda, PyCharm or Visual studio. In my case, I will use PyCharm professional with jupyter note book.
Why Electricity Consumption is Important?
In most case, It’s not possible to store electricity once they’re produced. The electricity produced must be consumed, else just forced to leave the game. Due to this, I’m using the “Electric production” and “Electric Consumption” interchangeably. Predicting electricity consumption is very important for efficient resource allocation, infrastructure planning, and sustainable energy management. By accurately forecasting consumption patterns, utility companies can optimize energy generation and distribution, reducing waste and costs.
Predictive models also empower policymakers to make informed decisions regarding energy policies and investments. Accurate predictions also helps to manage operations effectively, minimizing downtime and optimizing production schedules.
Thanks to AI, not its not a difficult duty to predict the future consumption and adjust our resources accordingly ensuring the company’s efficiency.
Let’s Go!
Import important libraries and our data frame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df=pd.read_csv('energy.csv')
Now let’s understand our data structure and see if the data needs cleaning and filling and others. In this case just print(df), but it’s not possible to print the whole data if the data frame is big. In our case, let’s print the first five column.
df.head()

Good! we have three columns of data, RecordDate, Month and daily production/consumption. Sorry if you can’t understand, the month row reads an Ethiopian name of months.
It’s possible to see whole data information and summary. Check by using info() and describe() functions
df.info()
print(df.describe(), "\n")
Let’s re-structure our data shorten the long description Total daily production to consumption, the date time pretty format and see top 5 column.
df.rename(columns={"RecordDate":"DateTime","Total daily production":"Consumption"},inplace=True)
print(df.head(5))

As we’re dealing with time series data, it’s important to make everything clear for our next prediction. This will help us to know in detail about our dataset and see the outcome.
dataset = df
dataset["Month"] = pd.to_datetime(df["DateTime"]).dt.month
dataset["Year"] = pd.to_datetime(df["DateTime"]).dt.year
dataset["Date"] = pd.to_datetime(df["DateTime"]).dt.date
dataset["Time"] = pd.to_datetime(df["DateTime"]).dt.time
dataset["Day"] = pd.to_datetime(df["DateTime"]).dt.day_name()
dataset = df.set_index("DateTime")
dataset.index = pd.to_datetime(dataset.index)

By now we can graphically visualize our data by plotting the data in appropriate format using the plot() imported from the pandas library.
from matplotlib import style
fig = plt.figure()
axes1 = plt.subplot2grid((1,1), (0,0))
style.use("ggplot")
sns.lineplot(x= dataset["Year"], y= dataset["Consumption"], data = dataset)
sns.set(rc={'figure.figsize': (20,10)})
plt.title("Electricity Production")
plt.xlabel("Months in a Year")
plt.ylabel("Energy in GWH")
plt.grid(True)
plt.legend()
for label in axes1.xaxis.get_ticklabels():
label.set_rotation(90)
plt.figure(figsize=(15,10))
plt.plot(dataset["Consumption"])
plt.title("Daily Electricity Production")
plt.xlabel("Months in a Year")
plt.ylabel("Daily Production in GWH")
plt.grid(True)
plt.legend()

This is what our daily electricity consumption looks like in 186 days. It just shows progressive growth, which shows our consumers electricity need is growing. Let’s visualize it more.
from matplotlib import style
fig = plt.figure(figsize = (20,10))
ax1 = fig.add_subplot(611)
ax2 = fig.add_subplot(612)
#ax3 = fig.add_subplot(613)
#ax4 = fig.add_subplot(614)
#ax5 = fig.add_subplot(615)
#ax6 = fig.add_subplot(616)
style.use("ggplot")
y_2023 = dataset.loc["2023"]["Consumption"].to_list()
x_2023 = dataset.loc["2023"]["Date"].to_list()
ax1.plot(x_2023, y_2023, color= "#ff00ff", linewidth= 1.7)
y_2024 = dataset.loc["2024"]["Consumption"].to_list()
x_2024 = dataset.loc["2024"]["Date"].to_list()
ax2.plot(x_2024, y_2024, color= "#ff00ff", linewidth= 1.7)
plt.rcParams["figure.figsize"] = (20, 10)
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=2.5, wspace=0.4, hspace=0.4)
plt.title("Energy Consumption")
plt.xlabel("Months in Year")
plt.ylabel("Daily Production in GWH")
plt.grid(True, alpha=1)
plt.legend()

Let’s see our electricity distribution looks like and see the electric consumption in each month and year.
fig = plt.figure(figsize = (15,10))
sns.distplot(dataset["Consumption"])
plt.title("Electricity Distribution")

fig = plt.figure(figsize = (10,5))
sns.boxplot(x=dataset["Month"], y=dataset["Consumption"], data= df)
plt.title("Electricity Consumption VS Month")
plt.xlabel("Month")
plt.grid(True, alpha=1)
plt.legend()
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(90)

fig = plt.figure(figsize = (10,5))
sns.boxplot(x=dataset1.index.year, y=dataset1.Consumption, data= df)
plt.title("Electric Consumption VS Year")
plt.xlabel("Year")
plt.grid(True, alpha=1)
plt.legend()
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(90)

So far so good. We’ve a clear visualization of the electric consumption in each day, each month and year. We can no predict the future electricity consumption using the LSTM.
Let’s go!…..Part B
What's Your Reaction?