This video demonstrates how to build a multivariate linear regression model using Python's scikit-learn library, including feature selection via correlation matrix analysis, train/test split (30% test size), model evaluation using R-squared scores, and making predictions with multiple input features. The instructor shows how adding a second feature (radio) to an existing TV-only model improved the R-squared score from 0.77 to 0.87 on test data, demonstrating the value of multivariate approaches over simple linear regression.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Part 3: Linear Model - Data Split Example - Multivariate Regression
Added:in this demonstration what I want to do is how to run a multivariate regression in the previous two demos I ended up using a simple linear regression model that contained one feature and one target now I'm going to work on creating a model that contains two features and one target uh here are some of the requirements that I need to fulfill I'm going to be creating a model with the two best features to predict Target and that's going to be done the two best features are going to be selected based off of the correlation Matrix then I'm going to do a train test split approach with 30 going to the test set and from there answer some questions such as how many observations are going to be in the train set and then what's the accuracy score of the model and that's going to be done using the the test set and we can also do some predictions as well all right now I am going to grab some information from previous code sales um and to first before I do that what I wanted what I want to do is figure out what the best features would be now here are the variables that we're dealing with now predicted cells is didn't come with the the original data frame that's something I added previously from another demo but we had TV radio newspaper uh it's possible features predicting the target which it sells but what I'll do now is I am going to focus on those by running a correlation Matrix um now let me just go ahead and drop the predicted cells just for the sake of clarity and then I'll go ahead now and let's look at the data frame and then do a correlation on it so here we have the correlation and if I'm going to go ahead with my target being cells we know television is pretty strong we have a correlation of 0.90 the next one up would be radio so I'm gonna go ahead and I'm going to use that in my model and then sleeve off newspaper now now what I'm going to do here is I'm going to go ahead and do the X train X test why train y test and use the train test split function make use of that now when it comes down to the first array it's going to be my first array is going to contain two data columns two variables that means I'm going to use I'm going to open up double brackets here and I'm going to use I'm going to put TV comma radio right and that would be my first array the second array is going to be cells I'm going to do a random I'm gonna go ahead and hit enter here I can do that right after the comma for space I'm going to do a random state or better yet let me go ahead and pursue a test size you know or it wouldn't matter for keyword arguments because it's like 30 percent of that is going to go in to the test subsets then the random States we're going to put 100 right there okay so I'll run that then I will Now work on instantiating a model and let me go ahead and call this reg2 and it's going to be a linear regression that's how you would instantiate it and then I'm going to use reg2 to now create the model with the data and specifically you fit it with the train and let me go ahead and do a score on the train even though I don't really have to do that but it's it's a good idea to do to just compare that with the test and all right so here we have it I'm going to run that here we have a pretty high r squared right it's a 0.90 it's pretty pretty good I'm gonna go ahead now and do a score when I feed it new data which is going to be the X test and Y tests all right I get 0.870 so pretty good right it's okay this is normal for us to have a variation between the train and test scores um so it's fine there's no significant difference here that raises in the eyebrows right well 0.87 uh bit and this is all based off of 30 going into the test if we were to play around with this number we might get different results but let's see here oh how many observations are in the train set well we can answer that question after we run this cell so let me go ahead and open up the cell put in that comment but train dot shape and we can see that there's 140 observations in the train set right what is the accuracy score of the model well the accuracy score uh just the accuracy score when it came down to the train right so this is based off of training data and then we also ran it we fed our model with unseen data related to the tests all right let's see here what's next predict sales by entering average uh entering average value for each feature in the model okay now since this is a multivariate regression that involves two features um it'll be a good idea to just examine how x-trained looks like right so here's X train we've got TV radio that's in this variable and we have the data that was randomly selected for the X train and you know this is randomly selected because if you look at the left column right here this is not a data column these are the index labels to the original data frame these are randomized and this randomization is really important to be able to generalize based off of the model assuming that you have good accuracy scores now that means if we're dealing let me go ahead and just put a dot head here if we want to use that model that we created called rig2 and do predictions off of it then that means when I do these predictions I I would need to put two values now because my model has two features right um so let me see what happens when I just put these two values here I'm going to put two and three point four and I might have to fix this but let's see 24.6 and that and I don't have to worry about that so this is the warning I can fix this morning I've talked about this before in the previous demo but here is the results of the prediction we're saying that if the company were to have spent this much right 200 to say roughly 200 uh 13 000 in television expenses and then 24 000 in radio uh they would be able to generate 18 000 uh roughly in unit cells okay so that's the approach that you would need to take when it comes down to predicting with a multivariate regression you would not want to just have one value right because in that case you are going to get an error um and so you have to be careful when it comes down to to that okay let me just go ahead and put in that other value run it now now to get rid of this warning dealing with the feature names really quick it has to deal with the train test split I should add the attribute right here when I was using this function to say dot values and since I re-ran this that means I'd have to rerun everything else below it to take effect right all right so that's not going to show anymore because it's not a data frame this is now an array all right so now we have it in this format I can use that head method there and then now when I predict it will just return the unit sales amounts without that warning right again I can also use the co-if you get the coefficients and these are going to be now coefficients for both of the features and it's in the order that I've placed them so this right here is a coefficient related to TV this is a coefficient related to radio now if you want we can compare how our model did and now I'm going to base this off of when I do my comparison I'm looking at the tests when I fed the model the rec2 model with unseen data I got a 0.87 accuracy score which is the r squared right the the variance explained by the independent variables now if I go back to what I did originally when I had a simple linear regression and that's up here and when I did the r squared for the test data it was we got a 0.77 so 0.77 we didn't in fact increase our make our model better when we added an additional feature right because with the simple linear regression model that I discussed earlier where it only had one feature which is television expenditures it had an r squared of 0.77 right and then when I changed the model up and added two features I got 0.87 so that's that's a pretty good increase when it comes down to predicting unit cells based off of the expenditures for advertisement hopefully this makes sense if you have any questions let me know thank you
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23

SuperBike Factory Has Gone... What's Next for the Motorcycle Industry?
thatbikersimon
11K views•2026-07-22