African Union Pledge to Support Somalia Elections Process

The African Union Mission in Somalia has hailed the political consensus in the country following a pre-election deal which was signed in September, arguing that it gave a platform for further…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Linear Regression Algorithm From Scratch In Python

A linear regression is one of the easiest statistical models in machine learning. It is used to show the linear relationship between a dependent variable and one or more independent variables.

Before we drill down to linear regression in depth, let me just give you a quick overview of what is a regression as Linear Regression is one of a type of Regression algorithm

Regression analysis is a form of predictive modeling technique which investigates the relationship between a dependent and independent variable

Linear regressions can be used in business to evaluate trends and make estimates or forecasts.
For example, if a company’s sales have increased steadily every month for the past few years, conducting a linear analysis on the sales data with monthly sales on the y-axis and time on the x-axis would produce a line that that depicts the upward trend in sales. After creating the trend line, the company could use the slope of the line to forecast sales in future months.

Linear regression can also be used to analyze the effect of pricing on consumer behavior.

For example, if a company changes the price on a certain product several times, it can record the quantity it sells for each price level and then performs a linear regression with quantity sold as the dependent variable and price as the explanatory variable. The result would be a line that depicts the extent to which consumers reduce their consumption of the product as prices increase, which could help guide future pricing decisions.

Linear regression can be used to analyze risk.

For example

A health insurance company might conduct a linear regression plotting number of claims per customer against age and discover that older customers tend to make more health insurance claims. The results of such an analysis might guide important business decisions made to account for risk.

Least squares is a statistical method used to determine the best fit line or the regression line by minimizing the sum of squares created by a mathematical function. The “square” here refers to squaring the distance between a data point and the regression line. The line with the minimum value of the sum of square is the best-fit regression line.

Regression Line, y = mx+c where,

y = Dependent Variable

x= Independent Variable ; c = y-Intercept

For the implementation part, I will be using a dataset consisting of head size and brain weight of different people.

In order to find the value of m and c, you first need to calculate the mean of X and Y

The value of m and c from above will be added to this equation

Now that we have the equation of the line. So for each actual value of x, we will find the predicted values of y. Once we get the points we can plot them over and create the Linear Regression Line.

Rsquared value is the statistical measure to show how close the data are to the fitted regression line

y = actual value

y ̅ = mean value of y

yp = predicted value of y

R-squared does not indicate whether a regression model is adequate. You can have a low R-squared value for a good model, or high R-squared value for a model that does not fit the data!

If you have reached up here, I assume now you have a good understanding of Linear Regression Algorithm using Least Square Method. Now its time that I tell you about how you can simplify things and implement the same model using a Machine Learning Library called scikit-learn

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

Add a comment

Related posts:

Buy Me A Coffee

So i read an article about Buy me a coffee after reading the article i went to check it out and ended up creating an account. Buy me a coffee is the easiest way for people to support creators it…