Sitemap

Linear Regression: A Beginner-Friendly Guide to Machine Learning

6 min readOct 31, 2026

--

Press enter or click to view image in full size

Introduction

Machine Learning helps computers learn patterns from data and make predictions. One of the most fundamental and widely used Machine Learning algorithms is Linear Regression.

Linear Regression is a supervised learning algorithm mainly used to predict continuous numerical values.

For example, it can be used to predict:

  • House prices
  • Salary
  • Product sales
  • Diamond prices
  • Temperature
  • Business revenue

In this article, we will understand what Linear Regression is, how it works, its equation, important assumptions, Python implementation, evaluation metrics, advantages, disadvantages, and real-world applications.

1. What is Linear Regression?

Linear Regression is a supervised machine learning algorithm used to predict a continuous numerical value.

It identifies the relationship between:

  • Independent Variable (X): The input or feature.
  • Dependent Variable (y): The output or target we want to predict.

For example, suppose we want to predict a person’s salary based on their years of experience.

  • X: Years of Experience
  • y: Salary

Linear Regression tries to find the best possible straight line that represents the relationship between these variables.

2. The Linear Regression Equation

The basic equation of Simple Linear Regression is:

ŷ = b₀ + b₁X

Where:

  • ŷ = Predicted value
  • b₀ = Intercept
  • b₁ = Slope or coefficient
  • X = Independent variable

The model uses this equation to predict the value of y for a given value of X.

Understanding the Components

Intercept (b₀):

The predicted value of y when X is equal to zero.

Slope (b₁):

The amount by which the predicted value changes when X increases by one unit.

For example, if the slope is positive, the predicted value generally increases as X increases.

If the slope is negative, the predicted value generally decreases as X increases.

3. How Does Linear Regression Work?

Linear Regression follows these basic steps:

Step 1: Collect the Data

The model receives historical data containing input features and target values.

For example:

Years of ExperienceSalary125,000230,000335,000440,000

Step 2: Find the Relationship

The algorithm identifies the relationship between the input variable and the target variable.

Step 3: Find the Best-Fit Line

Linear Regression finds a straight line that best represents the data.

The goal is to make the predictions as close as possible to the actual values.

Step 4: Make Predictions

Once the model learns the relationship, it can predict values for new input data.

For example:

Years of Experience = 5

The model may predict an appropriate salary based on the learned pattern.

4. What is the Best-Fit Line?

The best-fit line is the line that minimizes the difference between the actual values and predicted values.

The difference between an actual value and its predicted value is called a residual or error.

Linear Regression commonly uses the Least Squares Method.

This method minimizes the sum of the squared residuals.

In simple words, the algorithm tries to find the line that produces the smallest overall prediction error.

5. Simple Linear Regression vs Multiple Linear Regression

A. Simple Linear Regression

Simple Linear Regression uses only one independent variable to predict the target.

Example:

Years of Experience → Salary

Equation:

ŷ = b₀ + b₁X

B. Multiple Linear Regression

Multiple Linear Regression uses multiple independent variables.

Get Kadamanchipriya’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, house price prediction may use:

  • Number of bedrooms
  • Area
  • Location score
  • Age of the house

The model uses multiple features together to predict the target value.

6. Important Assumptions of Linear Regression

Linear Regression works best when certain assumptions are reasonably satisfied.

1. Linearity

There should be a roughly linear relationship between the features and the target variable.

2. Independence

Observations and residuals should be reasonably independent.

3. Homoscedasticity

The variance of residuals should remain relatively consistent across the range of predicted values.

4. Normality of Residuals

For statistical inference, residuals are often assumed to be approximately normally distributed.

5. Low Multicollinearity

In Multiple Linear Regression, independent variables should not be excessively correlated with each other.

7. Implementing Linear Regression Using Python

Let’s build a simple Linear Regression model using Python and Scikit-learn.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import numpy as np
# Sample dataset
data = {
"Experience": [1, 2, 3, 4, 5, 6, 7, 8],
"Salary": [25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000]
}
df = pd.DataFrame(data)# Independent variable
X = df[["Experience"]]
# Target variable
y = df["Salary"]
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
# Create the model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate the model
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print("MAE:", mae)
print("MSE:", mse)
print("RMSE:", rmse)
print("R² Score:", r2)

8. Understanding the Python Code

train_test_split()

This divides the dataset into training and testing data.

LinearRegression()

This creates the Linear Regression model.

fit()

This trains the model using the training data.

predict()

This predicts target values for new input data.

mean_absolute_error()

Calculates the average absolute difference between actual and predicted values.

mean_squared_error()

Calculates the average squared prediction error.

r2_score()

Measures how well the model explains the variation in the target variable.

9. Evaluation Metrics for Linear Regression

After building a Linear Regression model, we need to evaluate its performance.

A. Mean Absolute Error (MAE)

MAE calculates the average absolute difference between actual and predicted values.

A lower MAE generally indicates smaller prediction errors.

B. Mean Squared Error (MSE)

MSE calculates the average squared difference between actual and predicted values.

It gives greater importance to larger errors.

C. Root Mean Squared Error (RMSE)

RMSE is the square root of MSE.

It expresses the prediction error in the same units as the target variable.

D. R-Squared (R²)

R² measures how much variation in the target variable is explained by the model.

A value closer to 1 can indicate a better fit on the evaluated data, but R² should not be considered alone.

10. Advantages of Linear Regression

  • Simple and easy to understand.
  • Easy to implement.
  • Fast to train.
  • Provides interpretable coefficients.
  • Works well when the relationship is approximately linear.
  • Useful as a baseline model.

11. Disadvantages of Linear Regression

  • May not perform well when relationships are highly non-linear.
  • Can be sensitive to significant outliers.
  • Performance may be affected when important assumptions are strongly violated.
  • Multiple Linear Regression can be affected by multicollinearity.
  • May underfit complex datasets.

12. Real-World Applications

Linear Regression is used in many real-world applications.

1. House Price Prediction

Predicting house prices based on features such as area, location, and number of rooms.

2. Salary Prediction

Predicting salary based on experience, skills, and other relevant features.

3. Sales Forecasting

Estimating future sales based on historical data and relevant variables.

4. Business Analysis

Understanding how different factors are associated with business revenue.

5. Price Prediction

Predicting the price of products, vehicles, or other items using historical data.

13. Conclusion

Linear Regression is one of the most important and beginner-friendly algorithms in Machine Learning.

It helps us understand the relationship between input features and a continuous target variable and uses this relationship to make predictions.

In this article, we explored:

  • What Linear Regression is
  • Simple and Multiple Linear Regression
  • The best-fit line
  • Important assumptions
  • Python implementation
  • Evaluation metrics
  • Advantages and disadvantages
  • Real-world applications

My Learning Takeaway:

Through learning Linear Regression, I understood how Machine Learning can identify relationships between variables and use those relationships to predict continuous numerical values. I also learned the importance of evaluating a model using metrics such as MAE, MSE, RMSE, and R².

Linear Regression provides a strong foundation for understanding more advanced Machine Learning algorithms.

#MachineLearning #LinearRegression #Python #DataScience #ArtificialIntelligence #Regression #ScikitLearn

--

--