A.I, Data and Software Engineering

Recurrent neural network – predict monthly milk production

R

In part 1, we introduced a simple RNN for time-series data. To continue, this article applies a deep version of RNN on a real dataset to predict monthly milk production.

The data

Monthly milk production: pounds per cow. Jan 1962 – Dec 1975. You can download the data using this link.

Download: CSV file

The data contains the production of 168 months (14 years). We will use an RNN to predict the last 12 month of 1975 and compare with the real data. First, let have a look at the data.

Read the data to a pandas’ data frame and check the first few rows.

Month Milk Production
1962-01-01 01:00:00589.0
1962-02-01 01:00:00561.0
1962-03-01 01:00:00640.0
1962-04-01 01:00:00656.0
1962-05-01 01:00:00727.0

Make the index a time series by using to_datetime and plot the data for 14 years:

monthly milk product (pound per cow)

We can clearly see the trend and pattern of each year. Now, we will visualize the pattern by averaging the values of each month.

The production peaks in the summer (middle of the years) and bottoms in the winter period.

Preprocess the training data

We now split data into two parts, train and test set. We don’t want a random train test split, we want to specify that the test set is the last 12 months of data is the test set, with everything before it is the training.

Next, we use sklearn.preprocessing to scale the data using the MinMaxScaler. Remember to only fit_transform (not fit) on the training data, then transform the test data.

Create batch training data

We create a helper function to generate all training data at once. Note that the pre-generated training data like this is not really efficient for computation. We will cover the use of a data generator with Keras in the next post.

So every 12 data points at step t we have another 12 data points at steps t+1 (true values).

Setting Up The RNN Model

This time, we implement a deeper version of RNN with Long Short Term Memory (LSTM). LSTM has feedback connections. It can not only process single data points (such as images), but also entire sequences of data.

We can perform the training and plot the progress.

RNN loss

The milk Prediction

After training, we can predict the twelve months’ production.

Prediction VS Real values

To sum up

The deep RNN produced pretty good results. It can capture the pattern of monthly milk production. However, the training process can be improved by using a data generator rather than a pre-generated set.

Add comment

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest

Newsletters

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.