[Music] hi guys welcome back to data every day today we're looking at a data set of uh traffic at different times uh we have a longitude and latitude from where the traffic is beginning and ending and then we have uh the time uh that a time of day that the traffic is happening and then if it's a weekday or not and we're trying to predict the speed um based on the other features so let's hop into the notebook i'm going to use numpy and pandas for working with the data we're going to do a little visualization with pi plot and seaborn and then i will import from sklearn for pre-processing the train test split function and the standard scalar i don't think i'm using min max scalar today actually and then these are all the models we'll use we're using a bunch of different regression models and we'll compare the results of each one all right so let's go ahead and import that and i'll load in the data using pandas. read pandas. read.
csv we'll get the csv file path up here we're using the pghtrain. csv file so paste that in uh all right and let's take a look all right so here's the data set it's pretty simple only seven columns 570 rows um but let's get a little more information on it with data. info and you can see that there are no missing values in any of the columns we also have no object columns everything is in numeric form so we're ready to proceed i'd like to start by visualizing some of the correlations between the data and since these are already all numeric i can just create correlations for the whole data set i'll create a new pi plot figure with a figure size of 12 by 10 and i'll use c for seaborne's heat map function to create a heat map of the correlations and we can get the correlations with data.
core we'll turn on annotations so we actually see the pearson correlation coefficients and then we will set the minimum color value to negative 1 and change give it a color map as well i'll give it mako all right and then we'll show uh plt not plf all right here it is uh and we noticed something interesting here um we have a 100 correlation here well not percent is a 1. 0 correlation uh between from x and the 2x and you'll see that from y and 2y also have extremely high 0. 98 aside from these two there is not really any strong correlations between any of the other variables it's just 2x with from x and 2y with from y now if we were dealing with a higher dimensional data set if we had more columns on our data set i would could definitely consider dropping uh one of these each so maybe dropping from x maybe dropping from my or maybe dropping 2x or and 2y because you can say that maybe from x is saying everything that 2x is trying to say right it says it has such a high correlation now because we have such a small amount of data this is usually not the way to go i actually tried this with a bunch of different models and it seemed like dropping one of the columns to actually reduce the performance just because we have much less data to work with uh in that if that's the case but if we were dealing with a lot of different variables it might be a good idea to drop some of the redundant features here um you can see we have no negative correlations at all everything is right about zero all right so let's uh let's start pre-processing so let's take a look at the data one more time and then i'm going to create a pre-process inputs function it's going to take in a data frame it's going to make a copy of the data frame and then it's going to return the data frame and so i'll call the processed version of the data x and we will store the result from pre-process inputs passing in data into x right now it's just a copy of data but now as we adjust it we can in this function we can see the result down here so there's really not much pre-processing that has to happen the only thing that might be questionable is whether we should one hot encode the time column because you could argue if you look at this xsubtime.
unique see there's only three different times of day and they i believe they do refer to the hour during the day so it's definitely can be considered as an ordinal variable the spacing between them is right but because uh there may be certain qualities of those times of day that aren't captured by just a sing a simple continuous variable like this you might want to consider giving each k each uh each value its own column so the model can sort of perceive them all as separate events however when i tried this some of the weaker some of the more simple models did perform better but our overall performance across all models did go down so it seems like keeping it on as an ordinal feature like this is the best move um i also tried encoding it as a one-hot columns and keeping the ordinal feature on and that also reduced performance so this seems to be the best encoding all right and so i guess that means we should just split the df because there's nothing else to do into x and y so y is going to be what we're trying to predict that will be the speed column so dfs of speed and then x is going to be everything else except the speed column so df. drop speed we're dropping from axis one so it's the column axis and then we'll do our train test split so a train test split we'll get 70 of the data for the train set and the other 30 for the test set and that will return four new sets of the data which is x train x test y train and y test and we'll use the train test split function from sklearn takes an x and y specified the train size like i said 70 percent uh this shuffles the data as well before it makes the split which is uh nice because we'd like we have to we'd have to do it manually if we didn't use this function and then we'll include a random state us to ensure that the shuffle and therefore the split is always done in the same way if we were to reproduce the results of this notebook all right and then i'll scale x after that uh so if i run this now this will return let's return these values and then get them over here and take a look at x train so right now x train you see the speed column is gone the speed column is now in y train and this is seventy percent of the data the other thirty percent is in the tesla however the only issue here uh is that we have um we have not scaled the data so maybe i'll show you uh the benefit of scaling by scaling i mean if we look at the mean of each column they're all over the place and the variances are all over the place as well actually these are quite similar but we have this one is huge compared to the others so what we'd like to do is standardize them we can do this with a standard scalar which will give each column a mean of zero and a variance of one and to do that we'll create our scalar object uh and we can fit the scalar object to the test the train set only because we sort of want to pretend we don't have access to the test set when we're doing this uh and then i guess i'll actually before i actually transform the data using the scalar i want to see how our models do without scaling so some models are fine such as tree based models they don't really have a problem if the data isn't scaled all that really matters is the relative differences between the data but some models definitely perform very badly if you don't scale your data the the difference can be huge between the uh the results so let's do training now uh training slash model selection so we're gonna use a whole bunch of models like i said we use all of these we have a linear regression just simple linear regression we'll do k nearest neighbors algorithm a neural network or mlp regressor here a support vector machine with a linear kernel support vector machine with a radial basis function kernel decision tree regressor and then our two ensemble models which is the random forest regressor and gradient boosting regressor both of them use trees by default uh the only real difference is that the random forest uh builds trees in parallel and then averages the results at the end and the gradient boosting builds trees in series so let's um let's i'm going to i'm going to create a dictionary down here called models and models is going to map the name of the model to actual instance of the model and for the sake of time i'm just going to copy this in we have the name of each model that i just typed out and then the actual instance that we can now fit uh and you'll see that i actually i spaced them so that we can line them up and write the scores next to each other all right and then uh for every name and model in models dot items so dot items will return the key value pairs as tuples so that we can iterate through like this then we'll call model. fit on xtrain and y train and we'll print out just a confirmation message that said the model was trained so i'm saying name and trained so we run this you can see all of our models have been trained that was fast but we really don't have much data uh to deal with so it's understandable and then we'll we'll uh in a question we'll display the results here so for name and model again in models models.
items i will print out the name of the model followed by r squared score and display the r squared score to five decimal places so r squared score is a measure of how dispersed the data is around our fit and it's a really good way of getting an idea of how our model is a regression model is doing so we'll format this with model. score which for for regression models in sklearn the score function here will return the r squared score and we want to evaluate the model on the test set so i'm passing x test and y test now we can see all of the r squared scores for each one um so let's let's copy this over because i want to see which of the models are affected by scaling and which are not so you can see it looks like our random forest is performing the best by far with a 0. 61 r squared score next best would be gradient boosting then decision tree and it seems like our tree based models are doing quite well compared to the others we the others are performing more or less the same the highest value you can get with r squared is 1.
0 but the lowest value is it's unlimited it can keep going more and more negative but these are so these are just about zero on all of these um so now let's see if some of these ones uh get improved when we scale because we know uh the tree based models aren't going to be affected by scaling most likely maybe i i i don't think they'll be affected at all but let's see um x strain and x test i'm going to scale both of these using the fit we have to the train set so i'm using scalar dot transform and then in the top we're transforming x train and the bottom we're transforming x test now if i run this you'll see we have a numpy array because the transform function returns uh in a numpy array form so i'm going to turn them back into data frames after so we can retain the nice look of the data i want to keep the indexes the same so index equals xtrain. xtrain. index and column names is going to be x train columns on the bottom here i'm going to change it to x test all right so now if we run this we have the data frame back with the columns and indices and the data has been scaled so if we look at the mean of each column they're all extremely close to zero and the variances are all extremely close to one so the the scaling is complete you'll see y train has not been scaled since we want to keep these as our label our target values all right so let's retrain the models and see how these do now all right um and okay wow look at this looks like random forest is has actually gone down in performance after scaling because we had a 0.
61 before and now we have a 0.