0% found this document useful (0 votes)
12 views3 pages

Experiment 10

The document outlines a Python script that performs PCA on the Iris dataset to visualize it in 2D and 1D. It standardizes the data, applies PCA, and generates scatter plots to display the results, including the explained variance ratio. The 2D PCA captures 96% of the total variance, highlighting the effectiveness of the dimensionality reduction.

Uploaded by

Rishab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Experiment 10

The document outlines a Python script that performs PCA on the Iris dataset to visualize it in 2D and 1D. It standardizes the data, applies PCA, and generates scatter plots to display the results, including the explained variance ratio. The 2D PCA captures 96% of the total variance, highlighting the effectiveness of the dimensionality reduction.

Uploaded by

Rishab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import numpy as np

import matplotlib.pyplot as plt


import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
target_names = iris.target_names

# Standardize the data


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# --- PCA to 2D for visualization ---


pca_2d = PCA(n_components=2)
X_pca_2d = pca_2d.fit_transform(X_scaled)

# --- PCA to 1D (optional, for comparison) ---


pca_1d = PCA(n_components=1)
X_pca_1d = pca_1d.fit_transform(X_scaled)

# --- Plot 2D PCA ---


plt.figure(figsize=(6, 5))
sns.scatterplot(x=X_pca_2d[:, 0], y=X_pca_2d[:, 1], hue=iris.target,
palette='Set1', s=60)
plt.title("PCA - 2D Projection of Iris Dataset")
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
plt.legend(title="Class")
plt.grid(True)
plt.tight_layout()
plt.show()

# --- Explained Variance ---


print("\nExplained Variance Ratio (2D PCA):")
print(pca_2d.explained_variance_ratio_)
print(f"Total Variance Captured:
{np.sum(pca_2d.explained_variance_ratio_):.2f}")

# --- Optional: Plot 1D PCA projection ---


plt.figure(figsize=(6, 3))
plt.scatter(X_pca_1d, np.zeros_like(X_pca_1d), c=y, cmap='Set1', s=40)
plt.title("PCA - 1D Projection of Iris Dataset")
plt.xlabel("Principal Component 1")
plt.yticks([])
plt.grid(True)
plt.tight_layout()
plt.show()

Explained Variance Ratio (2D PCA):


[0.72962445 0.22850762]
Total Variance Captured: 0.96

You might also like