Alright, let's break down each line of this code in detail:
Initializing Plotting and Creating 3D Scatter Plot
%matplotlib inline
%matplotlib inline: This magic command ensures that Matplotlib plots
are displayed inline within Jupyter Notebooks. This means the plots will
appear directly below the code cells that generate them.
# create figure and 3D axes
fig = [Link](figsize=(8,7))
ax = fig.add_subplot(111, projection='3d')
fig = [Link](figsize=(8,7)): Creates a new figure with a specified size
of 8 inches by 7 inches.
ax = fig.add_subplot(111, projection='3d'): Adds a 3D subplot to the fig
ure. 111 means 1 row, 1 column, and the 1st subplot. The projection='
3d' argument specifies that this subplot is a 3D plot.
# set axis labels
ax.set_zlabel('MPG')
ax.set_xlabel('No. of Cylinders')
ax.set_ylabel('Weight (1000 lbs)')
ax.set_zlabel('MPG'): Sets the label for the z-axis to 'MPG' (Miles per Ga
llon).
ax.set_xlabel('No. of Cylinders'): Sets the label for the x-axis to 'No. of
Cylinders'.
ax.set_ylabel('Weight (1000 lbs)'): Sets the label for the y-axis to 'Weig
ht (1000 lbs)'.
# scatter plot with response variable and 2 predictors
[Link](df['cyl'], df['wt'], df['mpg'])
[Link](df['cyl'], df['wt'], df['mpg']): Creates a 3D scatter plot using t
he number of cylinders (df['cyl']) for the x-axis, weight (df['wt']) for the
y-axis, and miles per gallon (df['mpg']) for the z-axis.
Importing Regression Module and Splitting Data
# import regression module
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LinearRegression: Imports the Linear
Regression class from the linear_model module in sklearn.
# split predictors and response
X = [Link](['mpg'], axis=1)
y = df['mpg']
X = [Link](['mpg'], axis=1): Drops the mpg column from the DataFra
me df and assigns the result to X. X now contains all predictors.
y = df['mpg']: Assigns the mpg column from df to y, making y the resp
onse variable.
# create model object
lm = LinearRegression()
lm = LinearRegression(): Creates an instance of the LinearRegression
model and assigns it to the variable lm.
# import train/test split module
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split: Imports the train_t
est_split function from sklearn.model_selection.
# split into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20,
random_state=1)
train_test_split(X, y, test_size=0.20, random_state=1): Splits the data i
nto training and testing sets. 20% of the data is used for testing, and t
he random_state=1 ensures reproducibility.
Training the Model
# train model
[Link](X_train, y_train)
[Link](X_train, y_train): Trains the linear regression model using the trai
ning data (X_train and y_train).
Extracting Model Parameters
# extract model intercept
beta_0 = float(lm.intercept_)
beta_0 = float(lm.intercept_): Extracts the intercept of the linear regres
sion model and converts it to a float. Assigns the result to beta_0.
# extract model coefficients
beta_js = [Link](lm.coef_, [Link], columns=['Coefficient'])
beta_js = [Link](lm.coef_, [Link], columns=['Coefficient']):
Extracts the coefficients of the linear regression model and creates a D
ataFrame with