Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S07L01 - Multiple linear regression in Python

Machine Leaning Articles
1 views

S07L01 - Multiple linear regression in Python


May 15, 2025
```html

Mastering Multiple Linear Regression in Python: A Comprehensive Guide

Unlock the power of predictive analytics with multiple linear regression in Python. Whether you're a data science enthusiast or a seasoned professional, this guide walks you through building, evaluating, and optimizing a multiple linear regression model using Python's robust libraries. Dive in to enhance your data modeling skills and drive insightful decisions.


Table of Contents

  1. Introduction to Multiple Linear Regression
  2. Understanding the Dataset
  3. Setting Up the Environment
  4. Data Preprocessing
    • Importing Libraries
    • Loading the Data
    • Exploring the Data
    • One Hot Encoding Categorical Variables
  5. Splitting the Data
  6. Building the Multiple Linear Regression Model
  7. Making Predictions
  8. Comparing Actual vs. Predicted Values
  9. Evaluating Model Performance
  10. Conclusion

Introduction to Multiple Linear Regression

Multiple Linear Regression is a fundamental statistical technique used to predict the outcome of a target variable based on two or more predictor variables. Unlike simple linear regression, which relies on a single independent variable, multiple linear regression provides a more comprehensive understanding of data relationships, making it invaluable in fields like economics, medicine, and engineering.


Understanding the Dataset

For this guide, we'll utilize the Medical Cost Personal Dataset, accessible here on Kaggle. This dataset contains information about individuals' medical expenses and various factors that might influence those expenses, such as age, sex, BMI, number of children, smoking status, and region.

Sample Data:

age sex bmi children smoker region charges
19 female 27.9 0 yes southwest 16884.924
18 male 33.77 1 no southeast 1725.5523
28 male 33 3 no southeast 4449.462
... ... ... ... ... ... ...

Charges is our target variable, representing the medical expenses billed to an individual.


Setting Up the Environment

Before diving into the data analysis, ensure you have the necessary tools installed. We'll be using:

  • Python 3.x
  • Jupyter Notebook
  • Libraries: NumPy, Pandas, Matplotlib, Seaborn, Scikit-Learn

You can install the required libraries using pip:

pip install numpy pandas matplotlib seaborn scikit-learn

Data Preprocessing

Data preprocessing is a crucial step that involves cleaning and transforming raw data into a suitable format for modeling.

Importing Libraries

Begin by importing the essential Python libraries:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

Loading the Data

Load the dataset into a Pandas DataFrame:

data = pd.read_csv('S07_datasets_13720_18513_insurance.csv')

Exploring the Data

Understand the structure and contents of the dataset:

data.head()

Output:

age sex bmi children smoker region charges
19 female 27.9 0 yes southwest 16884.924
18 male 33.77 1 no southeast 1725.5523
28 male 33 3 no southeast 4449.462
33 male 22.705 0 no northwest 21984.47061
32 male 28.88 0 no northwest 3866.8552

One Hot Encoding Categorical Variables

Machine learning models require numerical input. Therefore, we need to convert categorical variables like sex, smoker, and region into numerical formats using One Hot Encoding.

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

# Define the column transformer with OneHotEncoder for categorical columns
columnTransformer = ColumnTransformer(
    [('encoder', OneHotEncoder(), [1, 4, 5])], 
    remainder='passthrough'
)

# Apply the transformation to the feature set
X = columnTransformer.fit_transform(X)

Explanation:

  • ColumnTransformer applies transformers to specified columns.
  • OneHotEncoder converts categorical variables into binary vectors.
  • remainder='passthrough' ensures that non-specified columns remain unchanged.

Splitting the Data

Divide the dataset into training and testing sets to evaluate the model's performance effectively.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, Y, test_size=0.20, random_state=1
)

Parameters:

  • test_size=0.20 allocates 20% of the data for testing.
  • random_state=1 ensures reproducibility.

Building the Multiple Linear Regression Model

With the data prepared, it's time to build and train the regression model.

from sklearn.linear_model import LinearRegression

# Initialize the model
model = LinearRegression()

# Train the model on the training data
model.fit(X_train, y_train)

Key Points:

  • LinearRegression() from Scikit-Learn is a straightforward way to implement linear models.
  • The .fit() method trains the model using the training data.

Making Predictions

Utilize the trained model to predict charges based on the test set.

y_pred = model.predict(X_test)

Comparing Actual vs. Predicted Values

Analyzing the differences between actual and predicted values provides insights into the model's performance.

comparision = pd.DataFrame()
comparision['Actual'] = y_test
comparision['Predicted'] = y_pred
comparision

Sample Output:

Actual Predicted
1646.4297 4383.680900
11353.2276 12885.038922
8798.5930 12589.216532
... ...
5227.98875 6116.920574

Observations:

  • Some predictions closely match the actual values.
  • Discrepancies indicate areas where the model can improve.

Evaluating Model Performance

Assess the model's accuracy using the R-squared (R²) metric, which represents the proportion of variance explained by the model.

from sklearn.metrics import r2_score

r2 = r2_score(y_test, y_pred)
print(f"R² Score: {r2:.2f}")

Output:

R² Score: 0.76

Interpretation:

  • An R² of 0.76 suggests that approximately 76% of the variance in medical charges is explained by the model.
  • While promising, there's room for improvement to achieve higher accuracy.

Conclusion

Building a multiple linear regression model in Python involves several crucial steps, from data preprocessing and encoding categorical variables to training the model and evaluating its performance. This guide provided a comprehensive walkthrough using the Medical Cost Personal Dataset, demonstrating how to harness Python's powerful libraries for predictive analytics.

Next Steps:

  • Feature Engineering: Explore creating new features or transforming existing ones to enhance model performance.
  • Model Optimization: Experiment with different algorithms or hyperparameters to achieve better accuracy.
  • Handling Overfitting: Implement techniques like cross-validation or regularization to prevent the model from memorizing the training data.

Embrace these strategies to refine your models further and unlock deeper insights from your data. Happy modeling!


Additional Resources

  • Jupyter Notebook: Access the Complete Notebook Here *(Replace with actual link)*
  • Dataset: Medical Cost Personal Dataset on Kaggle
  • Scikit-Learn Documentation: Linear Regression

Keywords: Multiple Linear Regression in Python, Data Preprocessing, One Hot Encoding, Scikit-Learn, Model Evaluation, R-squared, Predictive Analytics, Medical Cost Prediction, Python Data Science, Machine Learning Tutorial

```
Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.