Analysing Ad Budget
Analysing Ad Budget
If at any point in time you need help on solving this assignment, view our demo video to understand the different
steps of the code.
Happy coding!
Out[9]:
Unnamed: 0 TV Ad Budget ($) Radio Ad Budget ($) Newspaper Ad Budget ($) Sales ($)
Out[10]: 1000
In [7]: #Check the number of observations (rows) and attributes (columns) in the datas
et
df_data.shape
Out[7]: (200, 5)
4: Create objects to train and test the model; find the sales figures for each channel
Out[12]:
Newspaper Ad Budget ($) Radio Ad Budget ($) TV Ad Budget ($)
In [13]: #Create a target object (Hint: use the sales column as it is the response of t
he dataset)
Y_target = df_data['Sales ($)']
Out[14]: 0 22.1
1 10.4
2 9.3
3 18.5
4 12.9
Name: Sales ($), dtype: float64
In [15]: #Verify if all the observations have been captured in the feature object
X_feature.shape
Out[15]: (200, 3)
In [16]: #Verify if all the observations have been captured in the target object
Y_target.shape
Out[16]: (200,)
5: Split the original dataset into training and testing datasets for the model
In [17]: #Split the dataset (by default, 75% is the training data and 25% is the testin
g data)
from sklearn.model_selection import train_test_split
X_train,X_test, Y_train, Y_test = train_test_split(X_feature,Y_target,random_s
tate = 1)
In [19]: #Verify if the training and testing datasets are split correctly (Hint: use th
e shape() method)
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)
(150, 3)
(50, 3)
(150,)
(50,)
2.8769666223179176
[0.00345046 0.17915812 0.04656457]
In [29]: #Import required libraries for calculating MSE (mean square error)
from sklearn import metrics
import numpy as np
1.404651423032894
In [ ]: