Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S06L02 - Linear regression implementation in python - Part 1

Machine Leaning Articles
1 views

S06L02 - Linear regression implementation in python - Part 1


May 15, 2025

Step-by-Step Guide to Building a Linear Regression Model in Python

Unlock the power of data-driven decision-making with this comprehensive guide on implementing linear regression in Python. Whether you're a beginner in data science or looking to refine your machine learning skills, this tutorial will walk you through the entire process, from understanding the dataset to making accurate predictions.


Table of Contents

  1. Introduction to Linear Regression
  2. Understanding the Dataset
  3. Setting Up Your Python Environment
  4. Importing and Exploring the Data
  5. Data Preprocessing
  6. Building the Linear Regression Model
  7. Making Predictions
  8. Evaluating the Model
  9. Conclusion
  10. Additional Resources

Introduction to Linear Regression

Linear regression is a fundamental algorithm in the field of machine learning and statistics. It establishes a relationship between a dependent variable and one or more independent variables by fitting a linear equation to the observed data. This technique is widely used for predictive analysis, forecasting, and understanding the strength of predictors.

Key Topics Covered:

  • What is Linear Regression?
  • Applications of Linear Regression
  • Linear vs. Non-Linear Regression
  • Cost Function and Optimization

Understanding the Dataset

For this tutorial, we'll utilize the Canada Per Capita Income dataset, which is available on Kaggle. This dataset comprises the yearly per capita income in Canada, measured in US dollars.

Dataset Overview:

  • Columns:
    • year: The year of the recorded income.
    • per capita income (US$): The income per individual in USD.

Sample Data:

year per capita income (US$)
1970 3399.299037
1971 3768.297935
1972 4251.175484
1973 4804.463248
1974 5576.514583

Setting Up Your Python Environment

Before diving into the code, ensure that your Python environment is set up with the necessary libraries. We'll be using:

  • NumPy: For numerical operations.
  • Pandas: For data manipulation and analysis.
  • Matplotlib & Seaborn: For data visualization.
  • Scikit-Learn: For building and evaluating the linear regression model.

Installation Commands:

pip install numpy pandas matplotlib seaborn scikit-learn

Importing and Exploring the Data

Start by importing the essential libraries and loading the dataset into a Pandas DataFrame.

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

# Set seaborn style for better aesthetics
sns.set()

# Load the dataset
data = pd.read_csv('canada_per_capita_income.csv')

# Display the first few rows
print(data.head())

Output:

   year  per capita income (US$)
0  1970              3399.299037
1  1971              3768.297935
2  1972              4251.175484
3  1973              4804.463248
4  1974              5576.514583

Visualizing the Data:

It's crucial to visualize the data to understand the underlying patterns and relationships.

# Scatter plot to visualize the relationship
sns.scatterplot(data=data, x='year', y='per capita income (US$)')
plt.title('Canada Per Capita Income Over Years')
plt.xlabel('Year')
plt.ylabel('Per Capita Income (US$)')
plt.show()

*This scatter plot reveals a positive linear trend, indicating that per capita income has generally increased over the years.*


Data Preprocessing

Data preprocessing ensures that the dataset is clean and suitable for building an effective model.

1. Checking for Missing Values

# Check for null values
print(data.isnull().sum())

Output:

year                         0
per capita income (US$)      0
dtype: int64

*No missing values found.*

2. Splitting Features and Target Variable

# Features
X = data.iloc[:, :-1]  # All columns except the last

# Target variable
Y = data.iloc[:, -1]   # The last column

3. Train-Test Split

Splitting the dataset into training and testing sets allows us to evaluate the model's performance on unseen data.

from sklearn.model_selection import train_test_split

# Split the data (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20, random_state=1)

*Using a random state ensures reproducibility of the results.*


Building the Linear Regression Model

With the data prepared, we can now build the linear regression model.

from sklearn.linear_model import LinearRegression

# Initialize the model
model = LinearRegression()

# Train the model
model.fit(X_train, y_train)

Model Summary:

print(model)

Output:

LinearRegression()

*This output signifies that our model is ready for making predictions.*


Making Predictions

Using the trained model, we can predict the per capita income for the test dataset.

# Make predictions on the test set
y_pred = model.predict(X_test)

# Display the predictions alongside actual values
comparison = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
print(comparison)

*This comparison allows us to visualize how closely our model's predictions match the actual data.*


Evaluating the Model

Evaluating the model's performance is crucial to understand its accuracy and reliability.

1. Calculating R² Score

The R² score, also known as the coefficient of determination, indicates how well the data fits the regression model.

from sklearn.metrics import r2_score

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

Interpretation:

  • R² = 1: Perfect fit.
  • R² = 0: The model does not explain any variability.
  • 0 < R² < 1: Indicates the proportion of the variance explained by the model.

*In our case, a higher R² value signifies a better fit.*

2. Visualizing Predictions vs. Actual Values

# Plotting Actual vs Predicted values
plt.figure(figsize=(10,6))
plt.scatter(X_test, y_test, color='blue', label='Actual')
plt.scatter(X_test, y_pred, color='red', label='Predicted')
plt.title('Actual vs Predicted Per Capita Income')
plt.xlabel('Year')
plt.ylabel('Per Capita Income (US$)')
plt.legend()
plt.show()

*This visualization helps in assessing the accuracy of predictions across different years.*


Conclusion

In this tutorial, we've delved into the process of building a linear regression model in Python using the Canada Per Capita Income dataset. From understanding the dataset to preprocessing, model building, prediction, and evaluation, each step is crucial for developing accurate and reliable predictive models.

Key Takeaways:

  • Linear regression is a powerful tool for predicting continuous variables.
  • Proper data preprocessing enhances model performance.
  • Visualization aids in understanding data trends and model accuracy.
  • Evaluation metrics like R² are essential for assessing model effectiveness.

Next Steps:

  • Explore more complex datasets with multiple features.
  • Learn about other regression techniques like Ridge and Lasso Regression.
  • Dive into classification algorithms for categorical data problems.

Additional Resources

  • Scikit-Learn Documentation
  • Kaggle: Canada Per Capita Income Dataset
  • Python Data Science Handbook by Jake VanderPlas
  • Machine Learning Crash Course by Google

Empower your data science journey by mastering linear regression in Python. Stay tuned for more tutorials and insights into machine learning and data analysis!

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.