Linear Regression in Machine Learning
Updated on August 16, 2025 | By Learnzy Academy
Linear regression is a supervised machine learning algorithm used to model the relationship between a dependent variable (target) and one or more independent variables (features) by fitting the best possible straight line through the data points.
The goal is to find the parameters (slope and intercept) that minimize the difference between predicted and actual values — typically using the least squares method.
Types of Linear Regression:
- Simple Linear Regression – One independent variable
- Multiple Linear Regression – More than one independent variable
Formula:
y = mx + c
Where:
- y → predicted value
- x → input value
- m → slope (change in y for each unit change in x)
- c → intercept (value of y when x = 0)
Example :-
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
# Sample Data
data = pd.DataFrame({
'Size_sqft': [1000, 1500, 2000, 2500, 3000],
'Price_lakhs': [50, 65, 80, 95, 110]
})
# Features (X) and Target (y)
X = data[['Size_sqft']] # Independent variable
y = data['Price_lakhs'] # Dependent variable
# Create Model
model = LinearRegression()
# Train the Model
model.fit(X, y)
# Output Coefficients
print("Slope (m):", model.coef_[0])
print("Intercept (c):", model.intercept_)
# Predict price for a new house of size 1800 sqft
predicted_price = model.predict([[1800]])
print("Predicted Price for 1800 sqft:", predicted_price[0], "lakhs")
Output :-
Slope (m): 0.025
Intercept (c): 25.0
Predicted Price for 1800 sqft: 70.0 lakhs
Click here to download practice questions on
Linear Regression