v Using Linear Discriminant Analysis For Dimensionality Reduction - Machine Learning

Using Linear Discriminant Analysis For Dimensionality Reduction

Preliminaries

# 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 = iris.data
y = iris.target

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 = lda.fit(X, 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
lda.explained_variance_ratio_
array([ 0.99147248])