Introduction
Linear Regression is one of the most fundamental machine learning algorithms used for predicting continuous values. It establishes a relationship between independent variables (features) and a dependent variable (target). In Python, Scikit-Learn provides a simple and efficient way to build and train a linear regression model.
This article explains how to build a linear regression model using Python and Scikit-Learn with step-by-step implementation, real-world examples, and best practices.
What is Linear Regression?
Linear Regression is a supervised learning algorithm that models the relationship between input variables (X) and output variable (y) using a straight line.
The equation of linear regression is:
y = mx + b
Where:
y = predicted value
x = input feature
m = slope (coefficient)
b = intercept
Why Use Linear Regression?
Linear regression is widely used because:
It is simple and easy to understand
It works well for linear relationships
It is computationally efficient
It provides interpretable results
Prerequisites
Before starting, make sure you have:
Python installed
Basic understanding of Python
Libraries: numpy, pandas, matplotlib, scikit-learn
Install required libraries:
pip install numpy pandas matplotlib scikit-learn
Step 1: Import Required Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
Step 2: Load Dataset
data = pd.read_csv("data.csv")
X = data[['Feature']]
y = data['Target']
Step 3: Split the Dataset
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
This step ensures that we train and test the model on different data.
Step 4: Create and Train the Model
model = LinearRegression()
model.fit(X_train, y_train)
The model learns the relationship between input and output.
Step 5: Make Predictions
y_pred = model.predict(X_test)
Step 6: Evaluate the Model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("Mean Squared Error:", mse)
print("R2 Score:", r2)
Join the conversation! Your thoughts help the community grow.