Updated May 23, 2023

## Introduction to Regression

Now let us first understand What is regression? and why we use it. This is a predictive modeling technique in which we find the relationship between independent and dependent variables. It is mainly used for time series modeling, forecasting, and finding causal relationships between the variables.

Why do we use regression? For example, we can use this model to estimate the price of houses based on the data collected in the past years and define a curve. Based on this curve, we can make predictions about the houses. It analysis also helps us to compare the effects of variables measured in different scales. This analysis also helps to identify the impact of an independent variable or the strength of it on a dependent variable.

### What is Regression?

It is a method to determine the statistical relationship between a dependent variable and one or more independent variables. The change in the independent variable is linked to altering the dependent variables. This can be broadly classified into two major types.

- Linear Regression
- Logistic Regression

### Types of Regression

It has seven types, but Linear and Logistic Regression are mainly used. These are the basic and simplest modeling algorithms. We will discuss both of these in detail here.

#### 1. Linear Regression

- The simplest case of linear regression is to find a relationship using a linear model (i.e. line) between an input-independent variable (input single feature) and an output-dependent variable. This is called Bivariate Linear Regression.
- On the other hand, when there is a linear model representing the relationship between a dependent output and multiple independent input variables is called Multivariate Linear Regression.
- The dependent variable is continuous and independent variables may or may not be continuous. We find the relationship between them with the help of the best-fit line, also known as the Regression line. The equation of a line is,

`y = m * x + b`

Where,

**x:**Independent Variable**y:**Dependent Variable**m:**Slope of Line**b:**y Intercept

The most common method is the Least Square Method to evaluate the best-fit line. This method calculates the regression line by minimizing the least squared error between the regression line and the data points. Another method to find this line is also called the R Squared analysis.

It is particularly useful when the relationship between the input variables and the output is not very complex. Also, note that it is very sensitive to outliers.

**Syntax in Python:**

The Python library named sklearn contains an inbuilt function; we will use LinerRegression from sklearn.

Let us first install the sklearn package.

`pip install scikit-learn`

from sklearn.linear_model import LinearRegression

linearReg = LinearRegression()

To train the model, we will use the fit() function.

`linearReg.fit(x_train, y_train)`

#### 2. Logistic Regression

- It is used when the output is categorical. It is more like a classification problem. The output can be Success / Failure, Yes / No, True/ False, or 0/1. There is no need for a linear relationship between the dependent output variable and independent input variables.
- If the output has only two possibilities, it is called Binary Logistic Regression. If the dependent output has more than two output possibilities and there is no order, it is called Multinomial Logistic Regression. If an order is associated with the output and has more than two output possibilities, it is called Ordinal Logistic Regression.
- For example, you want to create a model identifying whether the breast cancer is malignant(1) or benign(0). For example, if you want to classify if the input email is spam(1) or not (0).

The Sigmoid function can better explain it.

`hΘ (x) = sigmoid (Z)`

**Sigmoid Function:**

`sig(t) = 1 / 1+e`

^{−t}

The sigmoid function is the S-shaped curve. If the value goes near positive infinity, the predicted value will be 1. Similarly, the predicted value will be 0 if it goes negative infinity.

**Syntax in Python:**

For the implementation of logistic regression in Python, an inbuilt function is available in scikit- learn library of Python. For that first install scikit-learn using pip install.

`from sklearn.linear_model import LogisticRegression logisticRegr = LogisticRegression()`

To train the model, we will use the fit() function.

`logisticRegr.fit(x_train, y_train)`

### Conclusion

It is necessary to choose the right Regression model based on the dependent and independent variables of your data and the dimensionality of the data. Before selecting any model, it is necessary to explore data. Different evaluation metrics can be used to compare the model’s goodness, like R Squared, Root Mean Square Error, Confusion Matrix, F1 score, etc.

### Recommended Articles

This is a guide to What is Regression? Here we discuss what it is. Along with the two types of it in detail. You can also go through our other related articles to learn more –