Using Linear Discriminant Analysis For Dimensionality Reduction


# Load libraries
from sklearn import datasets
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

Load Iris Data

# Load the Iris flower dataset:
iris = datasets.load_iris()
X =
y =

Create A Linear

# Create an LDA that will reduce the data down to 1 feature
lda = LinearDiscriminantAnalysis(n_components=1)

# run an LDA and use it to transform the features
X_lda =, y).transform(X)

View Results

# Print the number of features
print('Original number of features:', X.shape[1])
print('Reduced number of features:', X_lda.shape[1])
Original number of features: 4
Reduced number of features: 1

View Percentage Of Variance Retained By New Features

## View the ratio of explained variance
array([ 0.99147248])