Implementing LDA using Scikit Learn

We import an LDA model from the Scikit Learn Library in this step.

Python3




from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
lda = LDA(n_components=2)
iris_lda = lda.fit_transform(X, iris['target'])


Now let’s plot the lower dimensional data on a 2D plane and try to visualize the distinction between the three classes.

Python3




sb.scatterplot(iris_lda[:, 0],
               iris_lda[:, 1],
               hue=iris['target'])
plt.show()


Output:

Visualising data obtained by using LDA

LDA maximizes the distance between different classes, whereas PCA maximizes the variance of the data. When there are fewer samples in each class, PCA performs better. LDA, however, performs better on large datasets with many classes.



Comparison of LDA and PCA 2D projection of Iris dataset in Scikit Learn

LDA and PCA both are dimensionality reduction techniques in which we try to reduce the dimensionality of the dataset without losing much information and preserving the pattern present in the dataset. In this article, we will use the iris dataset along with scikit learn pre-implemented functions to perform LDA and PCA with a single line of code. Converting it into 2D and then visualizing them in two dimensions helps us to identify the patterns present between the different classes of the dataset.

Similar Reads

Implementing PCA using Scikit Learn

Python3 from sklearn import datasets import pandas as pd import numpy as np import seaborn as sb import matplotlib.pyplot as plt    iris = datasets.load_iris() iris.keys()...

Implementing LDA using Scikit Learn

...