لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science Machine Learning Lesson 7: Linear Regression

Linear Regression is a supervised machine learning technique. It is based on supervised learning. It is mostly used for finding out the relationship between variables and forecasting.

Luckily Python has methods for finding a relationship between data-points and to draw a line of linear regression.
There is no need for you to break your head with complex mathematics all you have to do is use these methods instead of going through the mathematical formula.

In the example below, the x-axis represents Time, and the y-axis represents speed.
We have registered the Time and Speed of 13 cars as they were passing a high-way.
Let us see if the data we collected could be used in a linear regression.

Code

Output >

Now Import scipy and draw the line of Linear Regression

Code # Linear regression Explained

A linear regression line has an equation of the form Y = a + bX, where X is the explanatory variable and Y is the dependent variable. The slope of the line is b, and a is the intercept (the value of y when x = 0)

Explanation
The line of best fit is described by the equation ŷ = bX + a, where b is the slope of the line and a is the intercept (i.e., the value of Y when X = 0). This calculator will determine the values of b and a for a set of data comprising two variables, and estimate the value of Y for any specified value of X.

Uses
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, by conducting a linear analysis on the sales data with monthly sales, the company could forecast sales in future months.

Example: we can say that age and height can be described using a linear regression model. Since a person's height increases as its age increases, they have a linear relationship. Regression models are commonly used as a statistical proof of claims

What is R and R-squared in regression?
Simply put, R is the correlation between the predicted values and the observed values of Y. R square is the square of this coefficient and indicates the percentage of variation explained by your regression line out of the total variation. This value tends to increase as you include additional predictors in the model.

What r squared tells us?
R-squared (R2) is a statistical measure that represents the proportion of the variance for a dependent variable that's explained by an independent variable or variables in a regression model.

How do you interpret Bo and b1?
Interpretation of b1: when x1 goes up by one unit, then predicted y goes up by b1 value. Here we Jneed to be careful about the units of x1. Say, we are predicting rent from square feet, and b1 say happens to be 2.5. Then we would say that when square feet goes up by 1, then predicted rent goes up by \$2.5.

limitation
Linear regression is a great algorithm but it is highly impacted by outliers. Hence we cannot use it to solve a classification problem. We need an algorithm that absorbs the effects of outliers without impacting the final output. Logistic regression does that by using something called a Sigmoid function.

What is the difference between logistic regression and linear regression give an example?
Linear regression is used to estimate the dependent variable in case of a change in independent variables. For example, predict the price of houses. Whereas logistic regression is used to calculate the probability of an event. For example, classify if tissue is benign or malignant.

How do you tell if a regression model is a good fit?
Statisticians say that a regression model fits the data well if the differences between the observations and the predicted values are small and unbiased. Unbiased in this context means that the fitted values are not systematically too high or too low anywhere in the observation space.

How do you find b0 and b1 in linear regression?
Formula and basics
The mathematical formula of the linear regression can be written as y = b0 + b1*x + e ,
where: b0 and b1 are known as the regression beta coefficients or parameters:
b0 is the intercept of the regression line; that is the predicted value when x = 0 .
b1 is the slope of the regression line

Note: if you have python installed on your pc you can install matplotlib as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install matplotlib
press enter
This command will install matplotlib on your computer after which you can run on python.

Note: if you have python installed on your pc you can install scipy as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install scipy
press enter
This command will install scipy on your computer after which you can run on python.