Getting ready

In this recipe, we will use the shampoo sales dataset that contains the monthly sales of shampoo over a 3-year period. The original dataset is credited to Makridakis, Wheelwright, and Hyndman (1998). It is also available in the GitHub repository of this chapter in the folder named data. Download the shampoo_sales.txt file and copy it to a folder named data in your working directory.

Let's load the required library and read the dataset:

library("mxnet")
sales_data <- read.table("data/shampoo_sales.txt",sep = ",",header = TRUE)

# We require only one column from the dataset
sales_data <- as.data.frame(sales_data[,2])

Next, we normalize the data within a range of 0 to 1 using min-max normalization:

min_max_scaler <- function(x) {
(x - min(x))/(max(x) - min(x))
}

norm_sales_data <- min_max_scaler(sales_data)
t_sales_data <- t(norm_sales_data)

To train a one-to-one sequence prediction model using MXNet-R, we need to transform the training data into a suitable form. The training feature set should be of the form (n_dim x seq_len * num_samples) and the training labels should be of the form (seq_len x num_samples). Since we have one-dimensional data, n_dim is equal to 1.

The following code block converts data into the required structure:

n_dim <- 1
seq_len <- 4
num_samples <- 7

# extract only required data from dataset
x_data <- t_sales_data[1, 1:(seq_len * num_samples)]
dim(x_data) <- c(n_dim, seq_len, num_samples)

y_data <- t_sales_data[1, 2:(1+(seq_len * num_samples))]
dim(y_data) <- c(seq_len, num_samples)

In the next section, we will build the forecasting model using RNN with MXNet. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset