0% found this document useful (0 votes)
5 views2 pages

ML Expt 10

The document demonstrates the application of Principal Component Analysis (PCA) on the Iris dataset using Python. It standardizes the data, reduces its dimensionality from 4 to 2 components, and visualizes the results in a scatter plot. The original and transformed shapes of the dataset are printed, confirming the successful reduction in dimensions.

Uploaded by

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

ML Expt 10

The document demonstrates the application of Principal Component Analysis (PCA) on the Iris dataset using Python. It standardizes the data, reduces its dimensionality from 4 to 2 components, and visualizes the results in a scatter plot. The original and transformed shapes of the dataset are printed, confirming the successful reduction in dimensions.

Uploaded by

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

EXP 10

import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.preprocessing import StandardScaler

from sklearn.decomposition import PCA

# Load Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Standardize the data

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

# Apply PCA to reduce to 2 components

pca = PCA(n_components=2)

X_pca = pca.fit_transform(X_scaled)

# Print shapes

print("Original shape:", X.shape)

print("Transformed shape:", X_pca.shape)

# Plot the PCA result

plt.figure(figsize=(8, 6))

for label in np.unique(y):

plt.scatter(X_pca[y == label, 0], X_pca[y == label, 1], label=iris.target_names[label])


plt.xlabel('Principal Component 1')

plt.ylabel('Principal Component 2')

plt.title('PCA on Iris Dataset')

plt.legend()

plt.grid(True)

plt.show()

OUTPUT:

Original shape: (150, 4)

Transformed shape: (150, 2)

You might also like