So in this video, we will learn how to apply machine learning, two time series forecasting, we'll
use the airline passengers data, which has been our standard benchmark throughout this course.
So the main idea is that we're going to have two versions of this script.
In this version, we will not use different saying.
We already know why this is suboptimal, but we're going to do it anyway just to see if it will work.
This will also help us get familiar with the basic structure of this code without having to worry about
different.
Remember that when you difference your data, you make it stationary, which is good.
But when you want to make a forecast, you then have to undo this different thing, which is non-trivial.
Now, libraries like stats, models do all that work for you, but because we're now using generic machine
learning methods, we'll need to do it ourselves.
So in order to introduce only one concept at a time, we'll save defensing for the next script.
In this script, we'll test the belief that machine learning can extrapolate.
Well, we kind of already know it can't, but this will help us to confirm what we saw before.
So let's begin by importing by pendas and matplotlib.
The next step is to update Saikat, learn so that we can import the metric.
The next step is to download our data.
The next step is to load in our data using PDA, reads GSV.
The next step is to compute the log, transform.
The next step is to split our data into train and test.
OK, so the next part is new in this block of code, we will learn how to convert a time series into
a supervised machine learning data set.
We'll start by converting our log passengers into an empire.
A no race are a little easier to index.
The next step is to set the number of legs in our model, which is called Big T. We also create empty
lists to store inputs and targets.
The next step is to loop through the Time series.
Note that we only go up to online series minus Bugti and encourage you to print out the final data to
ensure this is correct.
OK, so inside the loop we grab the time series from Index Little T up to little T plus big T.
This is our input, which is a time series of size, a big T.
Once we have this input, we append this to our list of inputs.
The next step is to compute the target, which is the next value of the Time series.
Now you might be wondering why is it still little T plus big T?
So remember that when you index a range of values in Python, the last index is exclusive, not inclusive.
So the last data point in the input is not the same as the target.
OK, so that's the target which we append to our list of targets.
Once we are outside the loop, we convert both the inputs and targets into no higher raise, which makes
them easier to index.
The next step is to split our input targets into train and test.
The next step is to fit a linear regression.
As you can see, we get an R-squared of about zero point nine six, which seems pretty good.
But the test, R-squared is only zero point six nine, which is not that good.
The next step is to create boolean arrays, to index both the train and testbeds note that because we
are using big T lag's in our model, we won't be able to make predictions for the first big T values.
So we'll set the first big T values of Traini reacts to false.
This kind of thing should give you a better idea of how Stat's models works behind the scenes, since
it has to contend with the same details.
The next step is to assign our one step forecasts to the data frame.
The next step is to plot our one step forecast.
Notice how our model seems to underestimate the peaks.
The next step is to compute a multi-step forecast.
So remember that stat's models does not compute a one step forecast.
It only computes a multi-step forecast.
So this code is closer to what we were doing before when we looked at Arima assets.
OK, so we'll start by creating an empty list to store our predictions.
The first step is to obtain the first test input, which is just X test indexed at zero.
Well, assign this to a variable called Last X since this will be updated as we go through the loop.
The next step is to enter a loop that will continue as long as our list of predictions is shorter than
and test.
Inside the loop, we'll call the predicate function to get a prediction.
Note that we have to reshape our last x ray because Saikat learn only accepts tuti arrays as input.
Recall that your data has to be in the form of a table with samples along the rows and features along
the columns.
Basically, we have one sample and two features, so we reshape last X to one by T.
After we get the prediction, we index the prediction at zero since there is only one prediction.
The next step is to append our prediction to a list of predictions.
Once we've started our prediction, the next step is to update last logistics for the next iteration
of the loop.
Basically, what we want to do is throw out the oldest value and append the newest value, which is
the latest prediction.
But recall that no higher rates are constant.
Sighs You can't delete or append anything to an array.
So the simplest way to perform this operation is to rotate all the values by one step.
The oldest value will wrap around to the end, but in the next line, we simply replace it with P.
So this effectively throws out the oldest value and adds the newest prediction to the end.
You should confirm to yourself that this implements the incremental forecasting procedure we discussed.
The next step is to store the multistep predictions to our data frame.
The next step is to plot our forecast.
As expected, the multi-step forecast seems to be a little worse compared to the ones that forecast.
The next step in this notebook is to create a multi output model, so in order to build this, it's
really just a matter of creating the right data set in this block of code.
We now have it to X and Y t X represents the number of time steps in the input, and seewhy represents
the number of steps in the output.
Now, again, the limit for this loop might be a bit confusing, but I encourage you to print out the
result and maybe also run through the loop by hand to convince yourself that this is correct.
Inside the loop, we grab the input series, which is the same as before.
The output series has length t y starting from the index, little T plus the X.
And again, once we're outside the loop, we convert X and Y into an umpire raise.
OK, and so in the next block, we split our inputs and targets into train and test.
Now, since we only want to forecast for the final and test time steps, our test set is effectively
just the final data point.
This is because it's a multi output data set and each target itself has n test the timestamps.
Note that I've put an underscore M at the end of these variable names so that we don't overwrite the
old values.
OK, so for the next step, we're going to fit a linear model.
Notice that the R-squared is now a bit better than before.
However, note that this is a bit misleading since the targets actually repeat the same values multiple
times.
For example, you have Y one y two by three and Y two y three in my four and so on.
OK, so when we tried to compute the R squared of the test, we get not a number.
So why does this happen?
Well, recall that the R squared involves computing the SC, which is essentially the sample variance
of the targets, but the sample variance of a single sample is zero since we only have one test data
point.
So unfortunately, the test R squared cannot be computed this way.
However, an alternative is to simply call the R-squared function ourselves and flatten the input arrays.
OK, so this gives us an R-squared of zero point eight, which is an improvement.
The next step is to save our multisport forecast to our data frame.
The next step is to plot our forecasts.
OK, so it looks like the multiple forecast is superior.
Now, just for fun, we're going to use a different metric in this block of code, we compute the map
for both the incremental and multiple forecasts.
So the multi map is a bit better as expected.
So in the next portion of this notebook, what I'd like to do is run through the exact same process
again, but with different machine learning models, since the code would essentially be the same,
it would be a good idea to make a function to do all the work.
So this function takes in two inputs.
The model we want to use and its name, the name is just the string so we can make sure all the color
names of our data frame are unique.
So basically everything in this function is stuff you just saw.
First we fit a model.
Next we do a one step forecast.
After that, we do an incremental, multi-step forecast.
After that, we store the forecast in the data frame.
After that, we compute the map.
And the final thing is to plot the forecast.
OK, so let's try a function with the support vector machine.
So the support vector machine does not perform that well, but again, remember that this is without
different saying.
The next step is to use the same function with the random forest.
Again, the predictions are pretty bad, but it does seem to fit very well to the train set, which
is pretty common for the random forest.
OK, so the next step is to make another function for the multi output forecast, remember that this
requires a different model because it has a different number of outputs.
So the basic steps are the same.
Fit the models, store the forecast, compute the map, make apply.
OK, so let's try to function with the support vector machine.
Now, I kind of gave it away in the come in here, but the support vector machine does not handle multiple
outputs.
Therefore, this is one example of a machine learning model for which this method will not work.
The next step is to try the random forest.
OK, so luckily the random force works, but again, it doesn't perform too well.