A Step-by-Step Guide to Implementing Linear Regression in Python

Introduction to Linear Regression and Its Applications in Data Science

agus abdul rahman
2 min readMar 20, 2023
Image from spiceworks

Linear regression is a fundamental concept in data science, which is used to model the relationship between a dependent variable and one or more independent variables. It is a powerful tool for predicting future trends and making data-driven decisions. In this article, we will provide a step-by-step guide to implementing linear regression in Python.

Step 1: Import Libraries

The first step is to import the necessary libraries. We will use NumPy for numerical operations, Pandas for data manipulation, and Matplotlib for visualization.

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

Step 2: Load the Dataset The next step is to load the dataset into a Pandas dataframe. For this tutorial, we will use a simple dataset that contains the hours studied and the corresponding exam scores of a group of students.

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

Step 3: Visualize the Data Before implementing linear regression, it’s important to understand the relationship between the dependent and independent variables. We can do this by plotting a scatter plot of the data.

plt.scatter(data['Hours_Studied'], data['Exam_Scores'])
plt.xlabel('Hours Studied')
plt.ylabel('Exam Scores')
plt.show()

Step 4: Split the Data into Training and Testing Sets To evaluate the performance of our model, we need to split the data into training and testing sets. We will use 80% of the data for training and 20% for testing.

from sklearn.model_selection import train_test_split

X = data['Hours_Studied'].values.reshape(-1,1)
y = data['Exam_Scores'].values.reshape(-1,1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

Step 5: Implement Linear Regression Now we can implement linear regression using the Scikit-learn library.

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()
regressor.fit(X_train, y_train)

Step 6: Visualize the Linear Regression Line To visualize the linear regression line, we can plot a scatter plot of the data along with the regression line.

plt.scatter(X_train, y_train)
plt.plot(X_train, regressor.predict(X_train), color='red')
plt.xlabel('Hours Studied')
plt.ylabel('Exam Scores')
plt.show()

Step 7: Evaluate the Model Finally, we can evaluate the performance of our model by predicting the test set results and calculating the mean squared error.

Conclusion:

In this article, we provided a step-by-step guide to implementing linear regression in Python. We started by importing the necessary libraries and loading the dataset into a Pandas dataframe. We then visualized the data, split it into training and testing sets, and implemented linear regression using the Scikit-learn library. Finally, we evaluated the performance of our model by predicting the test set results and calculating the mean squared error. Linear regression is a powerful tool for predicting future trends and making data-driven decisions, and Python provides a simple and effective way to implement it.

References:

--

--