GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Install
  • User Guide
  • API
  • Examples
  • Community
  • Getting Started
  • Release History
  • Glossary
  • Development
  • FAQ
  • Support
  • Related Projects
  • Roadmap
  • Governance

This view

  • Original page
  • New address

GeezerVison is showing scikit-learn.org — Comparison of LDA and PCA 2D projection of Iris dataset

Note

Go to the end to download the full example code or to run this example in your browser via JupyterLite or Binder.

Comparison of LDA and PCA 2D projection of Iris dataset#

The Iris dataset represents 3 kind of Iris flowers (Setosa, Versicolour and Virginica) with 4 attributes: sepal length, sepal width, petal length and petal width.

Principal Component Analysis (PCA) applied to this data identifies the combination of attributes (principal components, or directions in the feature space) that account for the most variance in the data. Here we plot the different samples on the 2 first principal components.

Linear Discriminant Analysis (LDA) tries to identify attributes that account for the most variance between classes. In particular, LDA, in contrast to PCA, is a supervised method, using known class labels.

  • PCA of IRIS dataset
  • LDA of IRIS dataset
explained variance ratio (first two components): [0.92461872 0.05306648]

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

iris = datasets.load_iris()

X = iris.data
y = iris.target
target_names = iris.target_names

pca = PCA(n_components=2)
X_r = pca.fit(X).transform(X)

lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)

# Percentage of variance explained for each components
print(
    "explained variance ratio (first two components): %s"
    % str(pca.explained_variance_ratio_)
)

plt.figure()
colors = ["navy", "turquoise", "darkorange"]
lw = 2

for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(
        X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=0.8, lw=lw, label=target_name
    )
plt.legend(loc="best", shadow=False, scatterpoints=1)
plt.title("PCA of IRIS dataset")

plt.figure()
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(
        X_r2[y == i, 0], X_r2[y == i, 1], alpha=0.8, color=color, label=target_name
    )
plt.legend(loc="best", shadow=False, scatterpoints=1)
plt.title("LDA of IRIS dataset")

plt.show()

Total running time of the script: (0 minutes 0.193 seconds)

Launch binder
Launch JupyterLite

Download Jupyter notebook: plot_pca_vs_lda.ipynb

Download Python source code: plot_pca_vs_lda.py

Download zipped: plot_pca_vs_lda.zip

Related examples

Principal Component Analysis (PCA) on Iris Dataset

Principal Component Analysis (PCA) on Iris Dataset

Dimensionality Reduction with Neighborhood Components Analysis

Dimensionality Reduction with Neighborhood Components Analysis

Incremental PCA

Incremental PCA

Plot the decision surface of decision trees trained on the iris dataset

Plot the decision surface of decision trees trained on the iris dataset

Gallery generated by Sphinx-Gallery

Download source code
Download Jupyter notebook
Download zipped
Launch JupyterLite
Launch binder

scikit-learn is financially supported by Probabl and other institutions.

Enterprise-grade solutions and services

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.