Linear regression is a fundamental concept in machine learning and statistical analysis that aims to establish a relationship between a dependent variable and one or more independent variables. It assumes a linear relationship between the input variables and the output variable.
The purpose of linear regression is to understand and predict the behavior of the dependent variable based on the independent variables. It helps us to identify and quantify the relationship between variables, allowing us to make predictions and infer insights from the data.
In simple linear regression, we have a single independent variable and one dependent variable. The goal is to fit a straight line to the data points that best represents the relationship between the variables. This line is determined by finding the best-fit coefficients (slope and intercept) that minimize the difference between the predicted values and the actual values.
The equation for a simple linear regression is typically represented as:
y = mx + b
Where:
y is the dependent variable (output or response variable)x is the independent variable (input or predictor variable)m is the slope of the line (the change in y for a unit change in x)b is the y-intercept (the value of y when x is zero)The fitted linear regression model can be used for various purposes:
Prediction: Once the model is trained on the existing data, it can be used to predict the values of the dependent variable for new or unseen data points.
Inference: By examining the coefficients of the regression equation, we can infer the strength and direction of the relationship between the variables.
Feature Selection: Linear regression can help identify the most influential variables by analyzing their coefficients. This is useful in determining which features have a significant impact on the outcome.
Outlier Detection: Linear regression can be used to identify outliers, which are data points that deviate significantly from the overall pattern of the data.
numpy, pandas, and sklearn (Scikit Learn).import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# Load the dataset
dataset = pd.read_csv('your_dataset.csv')
# Split the data into independent variables (X) and the dependent variable (y)
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
train_test_split function from Scikit Learn can be used for this purpose.# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Create a LinearRegression object
regressor = LinearRegression()
# Train the model using the training data
regressor.fit(X_train, y_train)
# Make predictions on the test data
y_pred = regressor.predict(X_test)
# Calculate the mean squared error
mse = mean_squared_error(y_test, y_pred)
coef_ attribute of the trained LinearRegression object provides the coefficients, and intercept_ provides the y-intercept.# Get the coefficients and intercept of the linear regression model
coefficients = regressor.coef_
intercept = regressor.intercept_