Time Series Forecasting With RNN(LSTM)| Complete Python Tutorial|

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone my name is nachiketa and welcome back to another video in this video i'm gonna guide you through a complete time series forecasting project done using the current new networks more specifically lstms now i'm gonna guide you through the projects the entire notebook and i'm going to show you how to analyze the data how to pre-process it and the most difficult part is basically how to format the data correctly to feed it to your new network model importing the model training it is actually the easiest part which is done in just a few lines of code now we're going to be using the current neural networks or a specific variant of it called as lstm now i'm not going to dive into the details of that i have separately made a video on recurrent neural networks you can check that over there just know that the current neural networks is a particular form of artificial neural networks which does well on sequence of data right and lstm is a form of recurrent neural networks which is one of the most widely used networks for the purpose of time series forecasting because it does really well when the sequence of the data is very important that is what we're gonna be using it let's just dive into the code and i'm gonna explain everything as we move forward the first thing is to import some important libraries like pandas numpy and matplotlib.py plot and the first thing we do is we read our data set which is present in a csv format and basically for monthly production of milk in from a particular factory i'm gonna give you this notebook as well as the data set in the description and you can just play around with this code and basically i'm mentioning that the index column is a column called as if date and when i write past dates equals to true it makes sure that the pandas recognizes that there's a date in the time series and it does not treat it as a normal string and i run this code i specify that the index frequency is ms which basically means that i'm dealing with monthly data normally it can automatically infer that as well but i'm still just specifying it so i print the first five rows using df.head and i can see this is how my data looks like it is ranging from 1962 and it is monthly data about the production the next thing the first thing that we should do is plot the data set and i'm mentioning the figure size as 12 comma 6 and this is how my data looks like all right so right off the bat i can view that there is some sort of seasonality a repeating pattern and a general trend that is increasing with time right so to view in detail about the different components like what is the exact seasonal nature what is the trend like we use something called as the starts model and we and we import a function called seasonal decompose which is basically gonna decompose different parts with time series so once you run this you simply give it your data set and i give it which column i want to perform the decomposition on and when i simply write plot it will show me various components of the graph right it will show this is how the observed pattern is this is just the same graph that we've seen above this is just compressed on a smaller scale now when you see trend over here basically isolating the trend and removing the seasonal pattern so you can see that there's a general increasing trend with time and the seasonal part of the graph basically shows you just the seasonality by subtracting and removing the trend from the original graph and we can see a clear seasonal pattern over here and this is the residual part right this is basically whatever that cannot be explained by your trend or seasonal basically the noise part of your data set so this is the first analysis that we have done right now normally you should also check for stationarity and if your data is not stationary you should make it stationary first but i am not going to do that now because technically recurrent neural networks do not need stationary data they can perform a non-stationary data as well because the current neural networks can learn complex patterns in your data right arima models that sarima models that we have seen before are basic regression models so recurrent neural networks can work on non-stationary data however if your model is not able to make good predictions then converting your data into stationary can be a good option because that makes it easier for the model to learn the pattern so i'm not going to do that now you can check out my previous video on prediction with arima model there i have explained how to use the augmented diki filler test in order to determine whether stationary or not but let's just skip that for now and see how our new network model performs if we simply provided the data so first thing i check what is the length of the data set right and what i'm going to do is i'm going to split the data set into a training part and a testing part in my training section i'm basically what i'm gonna do is i'm gonna use all the values except the last 12 months i'm going to leave the last 12 months as the testing set so basically i'm going to train the model and then use that to make a prediction on the last 12 months of the data set so for that i simply subtract 12 from 168 which is basically 156 and the first 156 values i have reserved in train and the last 12 values last 12 months data i'm keeping in the test set right now what we're going to do is we're going to pre-process the data this is a very important step for neural networks what we're going to be doing is we're going to be using a min max scalar to convert the data set into scale of zero to one let me tell you why this is important let me just print the first five values and the last five values using the head and tail function and here is how the data looks like right you can see there's a difference in the magnitude from the range of 500 to the range of 800 right so we don't want the model to get confused because of different ranges of magnitude so it's always a good step to convert the data into a scale of zero to one so first we fit the scalar object that we have created on the training set which will basically be calculating the minimum value maximum value standard deviation and all and we're going to be transforming both the training and testing part using the scalar object using the transform function so now if i simply print the first n values i can see that these values will be in a range of zero to one so now what we're going to be doing is we're gonna be formatting the data exactly to give it to the new electric model this is the only difficult part i would say if you're looking at a code like this for the first time but let me break it down so to give to the neural network model what's going to happen is i'm going to give a sequence of data right let's say i'm going to give it values of 3 months or 12 months and that will be the input using that it will give me a prediction for the next month so to give you an example let's look at this let's say you have an input in this format i'm gonna give it three values of sequence right i'm gonna give it one two and three this is the input using that it should predict the next item in the sequence which is four right so if i want to give it 12 months as the input so then since this supervised learning i need to give the model the input and output beforehand so i need to give it 12 months as the input and what would have been the next month's prediction so i want to create batches like this to train our new electron model our job is very easy we simply use something called as a time series generator and for example let me show you how it works let's say instead of batches of 12 at a time let's say i'm only using three months three values at a time so the number of inputs is gonna be three the number of features is one number of features would be more if you're using multiple time series to make a prediction but that's not the case and we simply call the time series generator we give it the scaled training input and we give it what is the number of inputs we want to show you how the generator has converted the data let me take the first value in the generator and extract the input and output x and y from it and let me just print it and show you how it looks like so here is how it looks like right the first three values are taken as one batch using that it has to predict the fourth value and let's say i print instead of zero i print the first patch right well now what should happen is i have to take the next three values which is 0.019 0.209 and 0.245 to predict the fifth value in the time series so so when i print this that is what happens so that is how the generator is working it's basically creating batches of three three inputs and using that to make predict the next value if i print the shape of this this how it looks like right because at time there's one row there are three columns and this fourth dimension is basically for the number of features which is also one so here i created batches of three at a time if you want to take 12 months and print the next value then i'm gonna change the same code into n input is equal to 1 right and i simply run the code and the data has been generated now is the easier part i simply call the model i am calling the sequential class the dense and the lstm class and here is the part where i'm actually creating the model when i write sequential it basically makes sure that the layers are gonna be added in a sequence one after the other and i'm gonna be adding an lstm layer with a hundred neurons and the activation function as the value activation function and i've specified what is the input shape of our data and here is the final output layer which is gonna make the final prediction and i simply compile the model using the adam optimizer and the mean square error as the loss function you can print the model summary as well this is how your model architecture looks like we are almost done with the code however there's one more tricky concept of how to actually make predictions so we'll get to that and basically now i'm going to fit the model we simply have to call the generator which has the input and output for training the model and i'm gonna run it over 50 epochs so let's wait for this training to get completed so once the model training is completed i'm gonna be plotting the loss per epoch which the model stores after every epoch and i'm gonna simply plot it you can see that the loss has been decreasing with every epoch and i could have stopped somewhere at 30 epoch as well because the loss did not decrease significantly after that now is the important part on how to make the predictions actually so let me show you how we have to do this now what happens is i'm going to take 12 values at a time and use it to predict the next month value so let's say i have 1 2 three as the input using this i'm gonna predict the next value which is gonna be four right now to make a prediction for the next value i have to reformat the input the input has to be changed to now two three and four then i'm going to make a prediction that the next value in this sequence could be 5. using this prediction i have to append it on to the original input and i'll get the new sequence as 3 4 5 right and then i'll predict that the next value can be 6. so we have to make predictions on the two last 12 values get the next value then create a new input to make a future prediction so this is how that code looks like this is the only complex part which you have to actually understand so what i'm going to be doing is i'm going to be taking the last 12 values last 12 months values in my training set to make a prediction for the first value in the test set all right so before making prediction i also have to reshape the data basically i have to get it in a format something like this right one comma the number of uh inputs and the number of features right so that is what i am reshaping it as because that is how the model has been trained on so when i make a prediction it will make a prediction for the first value in the testing set so i can actually check the what the actual value was and see how close we were so the original value was 0.67 the model has spread at 0.60 which is pretty close so once that is done now we are ready to make predictions on the testing set and this is very easy to understand you just have to look at this code a few more times just understand the concept so we're creating an empty list of test predictions we're going to take the last 12 values in the training set and we're going to be reshaping that right we're going to be calling it the current input or the current batch of 12 values now we iterate through the testing set and basically what i'm gonna be doing is i'm gonna be using the last 12 values in order to make a prediction all right and i'm going to store it as the current prediction and i append that prediction into this test predictions list now what i'm going to be doing is like in this case we had one two three and four h3 prediction i have to reshape the input i have to replace four into the original input i'm gonna be taking the current batch and what i'm gonna be doing is i'm gonna be appending to it current prediction when i write one colon it basically means i'm gonna be dropping the first input right so it's taking the twelve values making a next prediction using that prediction it's getting a new input of twelve values and using that it's again making a prediction right so that is how it works access equal to one basically means it's gonna be appending it along the column like we wanted so that is done once you've done this you can print the test predictions and one problem is that you can see it's a range of zero to one we have to transform it back into the original scale because the original testing values are in a scale of eight hundred nine hundred i'm not taking the scalar object and i'm performing the inverse transform on the test predictions and i'm appending this prediction into the original test set right so now we can simply call the plot function and you will be able to see how the test values compare to the predictions and in this graph i can see that they are pretty similar right the model has done a pretty good job if you want to put a number to how good the prediction is you can simply calculate the root mean squared error using the functions from sk learn and the math library and i'm basically giving it the input as the original testing values and the predictions right so the root mean squared error as you can see it was around 24. if you use some other model you can use this as a baseline to compare how good other models perform with respect to your recurrent neural network so that was it with this video if you did like this video do like it and subscribe this channel i'm gonna be making a lot more videos on similar topics like this and see on the next video
Info
Channel: Nachiketa Hebbar
Views: 129,337
Rating: undefined out of 5
Keywords: time series, deep learning, time series forecasting, time series forecasting python, time series with rnn, time series with lstm, keras time series forecasting, time series prediction python, time series forecasting project, time series python tutorial, rnn for time series forecasting, recurrent neural network
Id: S8tpSG6Q2H0
Channel Id: undefined
Length: 13min 57sec (837 seconds)
Published: Wed May 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.