Linear Regression is one of the most fundamental algorithms in machine learning. It models the relationship between a dependent variable and one or more independent variables using a straight line. This tutorial explores how it works, its assumptions, applications, and a practical implementation in Python.
Linear Regression is a supervised learning algorithm used for predicting continuous values. It assumes a linear relationship between input variables (features) and the output variable (target).
Simple Linear Regression involves one independent variable.
Multiple Linear Regression uses two or more independent variables.
For simple linear regression:
| Application | Description |
|---|---|
| Predicting Sales | Estimating future sales based on marketing spend or seasonal factors |
| Real Estate Pricing | Predicting house prices based on area, number of rooms, location, etc. |
| Healthcare | Predicting patient metrics like blood pressure based on age, weight, lifestyle factors |
| Finance | Forecasting stock prices or risk based on financial indicators |
| Engineering | Modeling relationships between process variables and outputs |
# Import required libraries
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
# Sample data: Study hours vs. exam score
X = np.array([[1], [2], [3], [4], [5], [6]]) # Hours studied
y = np.array([50, 55, 65, 70, 75, 85]) # Exam score
# Create a Linear Regression model
model = LinearRegression()
model.fit(X, y)
# Predict
y_pred = model.predict(X)
# Coefficients
print("Intercept (b0):", model.intercept_)
print("Slope (b1):", model.coef_[0])
# Plotting the regression line
plt.scatter(X, y, color='blue', label='Actual Scores')
plt.plot(X, y_pred, color='red', label='Regression Line')
plt.xlabel("Hours Studied")
plt.ylabel("Exam Score")
plt.title("Linear Regression Example")
plt.legend()
plt.show()Output -
Intercept (b0): 42.66666666666667
Slope (b1): 6.857142857142858To evaluate the model, use metrics such as:
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y, y_pred)
r2 = r2_score(y, y_pred)
print("MSE:", mse)
print("R² Score:", r2)Output-
MSE: 1.746031746031747
R² Score: 0.9874285714285714statsmodels for statistical insight and p-values.Sign in to join the discussion and post comments.
Sign in