![](https://crypto4nerd.com/wp-content/uploads/2024/03/1m54FAePyWLlt_4Gu2q8SLw-1024x538.jpeg)
- Linear regression is a supervised machine learning model which is used to analyse continuous numerical data.
- It is a plot the linear relationship between independent and dependent variables.
- In this Features/columns is called as Independent Variable and, Output/Class label is called as Dependent Variable.
- This method is used when you have only one Dependent Variable and one independent variable.
Ø Mathematical Expression:
Where,
- Y = Dependent Variable
- x = Independent Variable
- m = Slope
- C = y-intercept
Ø Core Logic:
- Training your model means we have to find the slope and Y-intercept value for the Linear Data.
- Using that slop and intercept value model will predict the output for new incoming data points.
Ø Scikit learn Libraries used:
1) For Splitting your data into train and test:
- from sklearn.model_selection import train_test_split
2) For import and create the linear regression model:
- from sklearn.linear_model import LinearRegression
3) Train the model:
- LR.fit(X_train, y_train)
- X = Independent variable/Input
- y = Dependent variable/Output
- At the time of training, we pass the X_train and its corresponding labels (y_train) to model
4) Check for Slope and y-intercept for data:
- LR.coef_ = for Slope
- LR.intercept_ = for y-intercept
5) Predict the result:
- Y_pred = LR.predict(X_test)
- At the time of prediction, we pass the X_test(Testing data) which we got from train_test_split.
6) Test for New Inputs:
7) Check the error:
- For regression problems there are separate accuracy metrics for checking the accuracy of your model.
- 1) Mean_Squared_Error
- 2) Mean_Absolute_Error
- 3) Root_Mean_Squared_Error
- RMSE 0 to +infinity Lower is the value, better is the model
Ø Code:
From sklearn.metrics import mean_squared_error, mean_absolute_error
8) R2 Score(Accuracy):
- Regression error metrics such as R2 indicate how closely a regression line fits the real data.
- Another name for it is the coefficient.
- R2 0 to 1 Higher is the value, better is the result
Ø Formula:
- actual_minus_predicted = sum((y_test — y_pred)**2)
- actual_minus_actual_mean = sum((y_test — y_test.mean())**2)
- r2 = 1 — actual_minus_predicted/actual_minus_actual_mean
9) Display the Fitted line by the model: