Milk Production prediction for next year using LSTM – with source code – easiest explanation – 2024

So guys in today’s blog we will implement the Milk Production prediction for the next year with the previous 13-year milk production data. We will use LSTM for this project because of the fact that the data is Sequential. So without any further due, Let’s do it…

Step 1 – Importing required libraries.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM,Activation,Dense,Dropout
%matplotlib inline

Step 2 – Read the input data.

df = pd.read_csv('monthly-milk-production.csv',index_col='Month')
df.index = pd.to_datetime(df.index)
Milk Production prediction

Step 3 – Plotting data.

Milk Production prediction

Step 4 – Scaling Data.

scaler = MinMaxScaler()

array = []
train_data = []
train_labels = []

for i in range(len(df)):
    array.append(df.iloc[i]['Milk Production'])

array = np.array(array).reshape(-1,1)
array = scaler.fit_transform(array)
  • Using MinMaxScaler here to bring our data in the 0-1 range.
  • Then just reshaping it to make just one column and n no. of rows where n represents the no. of elements in the array.
Milk Production prediction

Step 5 – Creating training data.

k = 0
for i in range(len(array)):

train_data = np.squeeze(train_data)
train_labels = np.array(train_labels)

  • Here we are just creating training data.
  • train data will have the first array as the first 12 points of the array and its corresponding train label will be the 13th entry in the array.
  • In this way, we will have 14 arrays of 12 points each.
  • But the training label will have only 13 points that’s why we will also take only 13 arrays in train data.
Milk Production prediction

Step 6 – Getting train data in shape.

train_data = train_data[:len(train_labels)]
train_data = np.expand_dims(train_data,1)
  • Taking the first 13 arrays in train data.
  • Then expanding dimensions in 1st dimension. We are doing this step because Keras wants the data in this format.
Milk Production prediction

Step 7 – Checking train data and train labels.

  • This is the first array in train_data. It shows the milk production of 12 months.
  • And its corresponding train label will be the milk production of the 13th month, which means the first month of next year.
  • We will train our data on 12 months’ data and ask our model to predict the production of the 13th month (1st month of next year).
Milk Production prediction
Milk Production prediction

Step 8 – Creating a model.

model = Sequential()






Milk Production prediction

Step 9 – Training model.

E = 1000

H =,train_labels,epochs=E)
Milk Production prediction

Step 10 – Plotting loss curve for Milk Production prediction model.

epochs = range(0,E)
loss = H.history['loss']
Milk Production prediction

Step 11 – Checking if our Milk Production prediction model is overfitting or not.

preds = scaler.inverse_transform(model.predict(train_data))
plt.plot(range(0,13),preds,label='our predictions')
plt.plot(range(0,13),scaler.inverse_transform(train_labels),label='real values')
Milk Production prediction

Step 12 – Create a seed for next year’s Milk Production prediction.

seed = array[-12:]
  • Here we are creating a seed which means the 12 last data points in the array. Suppose this is the milk production of 12 months of 1975.
  • Now we will ask our model to predict the production for Jan 1976.
  • When it will predict then we will update our seed.
  • And now our seed will be Feb 1975 – Jan 1976 and we will ask our model to predict for Feb 2022.
  • And in this way, we will predict for full 1976.
  • This is all done below.
Milk Production prediction
Milk Production prediction

Step 13 – Next year’s Milk Production prediction.

for _ in range(12):
    curr_12_months = seed[-12:]
    curr_12_months = np.squeeze(curr_12_months)
    curr_12_months = np.expand_dims(curr_12_months,0)
    curr_12_months = np.expand_dims(curr_12_months,0)
    pred = model.predict(curr_12_months)
    seed = np.append(seed,pred)

  • This step is explained above.
Milk Production prediction

Step 14 – Plotting next year’s Milk Production prediction.

next_year_prediction = scaler.inverse_transform(seed[-12:].reshape(-1,1))
  • This is the production of 1976.
Milk Production prediction

Download Source Code…

Do let me know if there’s any query regarding the Milk Production prediction by contacting me on email or LinkedIn. You can also comment down below for any queries.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…


Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

Your email address will not be published. Required fields are marked *