Tuesday, 7 February 2023

Linear Regression

  • Definition:

Linear Regression is a statistical method used for modeling the relationship between a dependent variable and one or more independent variables. It is a linear approach to modeling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables). The goal is to find the best linear function that minimizes the difference between the predicted and actual values of the response variable. The result of this process is a linear equation that can be used to predict the value of the response variable based on the values of the independent variables.


  • Mathematical Form:

The mathematical form of a simple linear regression equation is given by:

y = β_0 + β_1x

Where:

y is the dependent variable (also known as the response or output variable)


x is the independent variable (also known as the predictor or input variable)


β0 is the intercept, a constant representing the expected value of y when x = 0


β1 is the slope, representing the change in y for a unit change in x


For multiple linear regression, the equation is:

y = β_0 + β_1x_1 + β_2x_2 + ... + β_nx_n

Where:

x_1, x_2, ..., x_n are n independent variables.


β_0, β_1, β2, ..., β_n are coefficients to be estimated from the data.


The goal of linear regression is to estimate the values of the coefficients β_0, β_1, β_2, ..., β_n that minimize the difference between the observed and predicted values of the dependent variable.

  • Graphically :






  • Real Life Example :

1. Predicting Housing Prices:

Real estate companies can use linear regression to predict the sale price of a house based on various factors such as square footage, number of bedrooms, location, etc.

2. Forecasting Stock Prices:

Financial companies can use linear regression to forecast stock prices based on various financial indicators such as earnings per share, dividends, etc.


3. Estimating Employee Salaries:

HR departments can use linear regression to estimate employee salaries based on factors such as years of experience, education, job performance, etc.

4. Predicting Car Mileage:

Automobile companies can use linear regression to predict the miles per gallon a car can get based on factors such as engine size, weight, etc.

5. Forecasting Sales:

Retail companies can use linear regression to forecast sales based on various factors such as advertising spend, consumer confidence, etc.


  • Python Code :


Here's an example of how you can implement simple linear regression in Python using scikit-learn:

#import library

import numpy as np

from sklearn.linear_model import LinearRegression

# Input data


x = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])

# Train the model

reg = LinearRegression().fit(x, y)

# Predict the value for a new input
reg.predict(np.array([[6]])) # Output: array([12.])

# Get the intercept and coefficient values
print("Intercept: ", reg.intercept_)
print("Coefficient: ", reg.coef_)


For multiple linear regression, the input data should have multiple columns for the independent variables:

import numpy as np from sklearn.linear_model import LinearRegression # Input data X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]) y = np.array([2, 4, 6, 8, 10]) # Train the model reg = LinearRegression().fit(X, y) # Predict the value for a new input reg.predict(np.array([[11, 12]])) # Output: array([14.]) # Get the intercept and coefficient values print("Intercept: ", reg.intercept_) print("Coefficients: ", reg.coef_)




No comments:

Post a Comment

Most Important Logistic Regression Interview Question

Q: What is logistic regression? A: Logistic regression is a statistical method for analyzing a dataset in which there are one or more indepe...