# What is Linear regression

##### Linear Regression
Introduction

Linear regression is an essential and fundamental statistical technique in machine learning. It is a statistical approach to find and model the relationship between a dependent variable and one or more independent variables.

The fundamental idea behind linear regression is to pick the best line that fits the data. It is a simple and straightforward technique for finding relationships between variables in a data set.

Linear regression is a supervised learning algorithm, meaning that it operates on labelled training sets. Linear regression is used to estimate the parameters of a linear equation. The goal is to fit a line or a hyperplane in such a way that it describes the relationship between the independent variables and the dependent variable.

In this article, we will discuss the basic concept of linear regression, types of linear regression, and examples of how to use it for solving various data sets. We will also discuss the mathematical concept behind linear regression.

##### Types of Linear Regression

There are two types of linear regression: simple linear regression and multiple linear regression.

• Simple Linear Regression: Simple linear regression is a type of linear regression where we have one dependent variable and one independent variable. The relationship between the two variables is represented by a straight line. The equation for a straight line can be represented as y=mx+b, where y represents the dependent variable, x represents the independent variable, m represents the slope of the line, and b represents the y-intercept.
• Multiple Linear Regression: Multiple linear regression is a type of linear regression where we have multiple independent variables and one dependent variable. It is denoted by a straight or a curved line. In this type of regression, the relationship between the independent variables and the dependent variable is represented by a straight or curved line.
##### Mathematics behind Linear Regression

Regression analysis involves finding the best-fitting line that represents the relationship between two or more variables. In the case of simple linear regression, we can represent the relationship between the two variables as y=mx+b. In which y represents the dependent variable, x represents the independent variable, m represents the slope of the line, and b represents the y-intercept.

The goal of linear regression is to find the best line that fits the data. The best line should be such that it reduces the error between the observed values and the predicted values. The error between the observed values and the predicted values is also known as the residual. The objective of linear regression is to minimize the sum of the residuals.

The best fit line in linear regression is obtained by minimizing the sum of squared residuals. The residual is the difference between the observed (actual) value and the predicted value (y-hat). The formula for finding the residual is:

Residual = Observed value - Predicted value

The formula for the sum of squared residuals is:

SSR = sum[(y - y-hat)^2]

The objective of linear regression is to minimize the sum of squared residuals. We can do this by finding the partial derivative of SSR with respect to the slope and the y-intercept and then equating them to zero. Once we have obtained the value of the slope and the y-intercept, we can use them to predict the value of the dependent variable. The slope and the y-intercept are represented by the following formulas:

Slope (m) = ((n Σxy) - (Σx Σy)) / ((n Σx2) - (Σx)^2)

y-intercept (b) = (Σy - m Σx) / n

Where n is the number of observations, x is the independent variable, y is the dependent variable, and Σ represents the sum of the values.

##### Uses of Linear Regression

Linear regression is widely used in machine learning and statistical analysis. Here are some applications of linear regression:

• Medical Research: Linear regression is used to measure the relationship between a dependent variable and an independent variable. For example, it can be used to measure the relationship between blood pressure (dependent variable) and age (independent variable).
• Business: Linear regression is used to model the relationship between two or more variables in business. It can be used to forecast sales, demand, and pricing.
• Marketing: Linear regression is used in marketing to identify the relationship between advertising spending and sales. It can be used to optimize advertising campaigns and to calculate the return on investment.
##### Example of Linear Regression in Python

Here is an example of how to use linear regression in Python:

First, we need to import the necessary libraries:

```import numpy as np
import pandas as pdimport matplotlib.pyplot as plt%matplotlib inlinefrom sklearn.linear_model import LinearRegression```

Next, we need to load the data set:

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

We can then plot the data to visualize the relationship between the two variables:

```plt.scatter(data['x'], data['y'])
plt.xlabel('Independent Variable')plt.ylabel('Dependent Variable')plt.show()```

We can then fit the data into the linear regression model:

```model = LinearRegression()
model.fit(data[['x']], data['y'])```

We can then use the model to predict the values of the dependent variable:

`y_pred = model.predict(data[['x']])`

We can then plot the best-fit line:

```plt.scatter(data['x'], data['y'])
plt.plot(data['x'], y_pred, color='red')plt.xlabel('Independent Variable')plt.ylabel('Dependent Variable')plt.show()```

This is just a simple example of how to use linear regression in Python. There are many more applications and libraries available for linear regression in Python.

##### Conclusion

Linear regression is a fundamental and essential statistical technique in machine learning and data analysis. It is used to estimate the parameters of a linear equation and to model the relationship between two or more variables. Linear regression is widely used in medical research, business, marketing, and many other fields. In this article, we discussed the basics of linear regression, types of linear regression, mathematical concepts behind linear regression, and examples of how to use linear regression in Python.