Skip to main contentHomepage

An Introduction to Linear Discriminant Analysis

Written by
Hong
fiq
Published on
--
Views
--
Comments
--
An Introduction to Linear Discriminant Analysis

Preface

Linear Discriminant Analysis (LDA) is a fundamental technique in statistics and machine learning for dimensionality reduction and classification. It aims to model the difference between classes by finding a linear combination of features that characterizes or separates two or more classes. In this blog, we will explore the principles of LDA, its mathematical formulation, and how to implement it in Python.

Linear Discriminant Analysis

Demo

In this tutorial, we will demonstrate the implementation of LDA using Python and the scikit-learn library.

How to Implement Linear Discriminant Analysis

First, ensure you have the necessary libraries installed:

Terminal
pip install numpy pandas scikit-learn matplotlib

Next, we will create a Python script to perform LDA.

Importing Libraries and Dataset

We begin by importing the required libraries and loading the dataset.

lda_example.py
import numpy as np
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# Load the dataset
data = pd.read_csv('your_dataset.csv')

Preparing the Data

Split the data into features and target variables and then into training and testing sets.

lda_example.py
# Assuming the last column is the target variable
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Applying Linear Discriminant Analysis

Create an LDA object and fit it to the training data.

lda_example.py
# Create an LDA object
lda = LDA()

# Fit the LDA model to the training data
lda.fit(X_train, y_train)

# Transform the data
X_train_lda = lda.transform(X_train)
X_test_lda = lda.transform(X_test)

Evaluating the Model

Predict the target variable for the test set and calculate the accuracy.

lda_example.py
# Predict the test set results
y_pred = lda.predict(X_test)

# Calculate the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy * 100:.2f}%')

Visualizing the Results

If the dataset is reduced to 2D or 3D, we can visualize the transformed data.

lda_example.py
# Plot the transformed data
plt.figure(figsize=(10, 7))
colors = ['r', 'g', 'b']
for i, color in zip(np.unique(y_train), colors):
    plt.scatter(X_train_lda[y_train == i, 0], X_train_lda[y_train == i, 1], c=color, label=f'Class {i}')
plt.title('LDA: Training Data Projection')
plt.xlabel('LD1')
plt.ylabel('LD2')
plt.legend(loc='best')
plt.show()

Conclusion

Linear Discriminant Analysis is a powerful tool for both dimensionality reduction and classification. By projecting data onto a lower-dimensional space, LDA maximizes the class separability, making it easier to visualize and classify data. This tutorial has demonstrated the basic steps to implement LDA in Python using the scikit-learn library. For more advanced usage, consider exploring LDA with different datasets and parameter tuning.

Feel free to reach out with any questions or comments about this tutorial!

Edit on GitHub
Last updated: --/--/----