Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Linear Regression Tutorial

Introduction

Linear Regression is one of the simplest and most widely used Machine Learning algorithms. It is used for predicting a target variable (dependent variable) based on one or more input variables (independent variables). The goal of Linear Regression is to find the best-fitting straight line through the data points.

Mathematical Foundation

In a simple linear regression model, the relationship between the dependent variable \( Y \) and the independent variable \( X \) is modeled as:

\( Y = \beta_0 + \beta_1 X + \epsilon \)

Where:

  • \( Y \) is the dependent variable.
  • \( X \) is the independent variable.
  • \( \beta_0 \) is the y-intercept of the regression line.
  • \( \beta_1 \) is the slope of the regression line.
  • \( \epsilon \) is the error term.

Steps to Perform Linear Regression

1. Collect and prepare the data.

2. Split the data into training and testing sets.

3. Train the linear regression model using the training set.

4. Evaluate the model using the testing set.

5. Use the model to make predictions.

Example: Simple Linear Regression

Let's consider an example where we predict the salary based on years of experience.

Step 1: Import Libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

Step 2: Load Dataset

dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, -1].values

Step 3: Split the Data

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

Step 4: Train the Model

regressor = LinearRegression()
regressor.fit(X_train, Y_train)

Step 5: Make Predictions

Y_pred = regressor.predict(X_test)

Step 6: Evaluate the Model

mse = mean_squared_error(Y_test, Y_pred)
print('Mean Squared Error:', mse)
Mean Squared Error: 21.5174442311772

Conclusion

Linear Regression is a powerful tool for predicting a dependent variable based on one or more independent variables. By understanding the mathematical foundation and following the steps outlined in this tutorial, you can apply Linear Regression to various real-world problems.