Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Building Multiple Linear Regression | Section
Supervised Learning Essentials

bookBuilding Multiple Linear Regression

The LinearRegression class allows you to build Multiple Linear Regression the same way as Simple Linear Regression. It automatically handles multiple features (columns) in the input matrix.

Preparing the Data

We have the same dataset from the simple linear regression example, but it now has the mother's height as the second feature. We'll load it and look at its X variable:

12345678
import pandas as pd file_link='https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/heights_two_feature.csv' df = pd.read_csv(file_link) # Open the file # Assign the variables X = df[['Father', 'Mother']] y = df['Height'] print(X.head())
copy

By default, the parameter fit_intercept=True is set, so the model calculates the bias term () automatically! We can pass the X DataFrame (which is already 2D) directly to the model.

Finding the Parameters

Great! Now we can build the model, find the parameters, and make predictions.

12345678910111213141516171819202122
from sklearn.linear_model import LinearRegression import numpy as np # Initialize the model model = LinearRegression() # Train the model (No need to add constant!) model.fit(X, y) # Get the parameters beta_0 = model.intercept_ beta_1 = model.coef_[0] # Coefficient for 'Father' beta_2 = model.coef_[1] # Coefficient for 'Mother' print('beta_0 is: ', beta_0) print('beta_1 is: ', beta_1) print('beta_2 is: ', beta_2) # Predict new values # Feature values of new instances: [[Father, Mother], [Father, Mother], ...] X_new = np.array([[65, 62], [70, 65], [75, 70]]) y_pred = model.predict(X_new) # Predict the target print('Predictions:', y_pred)
copy
Note
Note

Now that our training set has 2 features, we must provide 2 features for each new instance we want to predict. That's why np.array([[65, 62],[70, 65],[75, 70]]) was used. Also, note that model.coef_ returns an array of coefficients corresponding to the order of columns in X. Since X was ['Father', 'Mother'], coef_[0] is the slope for Father, and coef_[1] is for Mother.

question mark

Does the LinearRegression class require adding a constant column manually?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookBuilding Multiple Linear Regression

Swipe to show menu

The LinearRegression class allows you to build Multiple Linear Regression the same way as Simple Linear Regression. It automatically handles multiple features (columns) in the input matrix.

Preparing the Data

We have the same dataset from the simple linear regression example, but it now has the mother's height as the second feature. We'll load it and look at its X variable:

12345678
import pandas as pd file_link='https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/heights_two_feature.csv' df = pd.read_csv(file_link) # Open the file # Assign the variables X = df[['Father', 'Mother']] y = df['Height'] print(X.head())
copy

By default, the parameter fit_intercept=True is set, so the model calculates the bias term () automatically! We can pass the X DataFrame (which is already 2D) directly to the model.

Finding the Parameters

Great! Now we can build the model, find the parameters, and make predictions.

12345678910111213141516171819202122
from sklearn.linear_model import LinearRegression import numpy as np # Initialize the model model = LinearRegression() # Train the model (No need to add constant!) model.fit(X, y) # Get the parameters beta_0 = model.intercept_ beta_1 = model.coef_[0] # Coefficient for 'Father' beta_2 = model.coef_[1] # Coefficient for 'Mother' print('beta_0 is: ', beta_0) print('beta_1 is: ', beta_1) print('beta_2 is: ', beta_2) # Predict new values # Feature values of new instances: [[Father, Mother], [Father, Mother], ...] X_new = np.array([[65, 62], [70, 65], [75, 70]]) y_pred = model.predict(X_new) # Predict the target print('Predictions:', y_pred)
copy
Note
Note

Now that our training set has 2 features, we must provide 2 features for each new instance we want to predict. That's why np.array([[65, 62],[70, 65],[75, 70]]) was used. Also, note that model.coef_ returns an array of coefficients corresponding to the order of columns in X. Since X was ['Father', 'Mother'], coef_[0] is the slope for Father, and coef_[1] is for Mother.

question mark

Does the LinearRegression class require adding a constant column manually?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 7
some-alt