Wednesday, February 26, 2020

Simple Linear Regression

Simple Linear Regression is a linear regression model where we have one dependent and one independent variable.

We need to predict values for the dependent variable as a function of the independent variable.



Formula for Simple Linear Regression:


<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>y</mi><mo>=</mo><msub><mi>b</mi><mn>0</mn></msub><mo>+</mo><msub><mi>b</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub></math>

where

y is the dependent Variable
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>x</mi><mn>1</mn></msub></math> is the independent Variable
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>1</mn></msub></math> is the coefficient (connector between dependent and Independent)
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>0</mn></msub></math> is the Constant

Code Snippet:

from sklearn.linear_model import LinearRegression
simple_regressor = LinearRegression()
simple_regressor.fit(X_train, y_train)

# Prediction
y_pred = simple_regressor.predict(X_test)

Plot

plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, simple_regressor.predict(X_train), color = 'blue')
plt.show()




No comments:

Post a Comment