Thanks to visit codestin.com
Credit goes to www.scribd.com

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

ML 10

The document outlines an experiment using PCA (Principal Component Analysis) on a dataset with 5 samples and 4 numerical features. It includes code to perform PCA, transform the dataset into two principal components, and visualize the results with a scatter plot. The explained variance ratio indicates that the first principal component explains approximately 95.6% of the variance in the data.

Uploaded by

dhakeyashashree
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)
7 views2 pages

ML 10

The document outlines an experiment using PCA (Principal Component Analysis) on a dataset with 5 samples and 4 numerical features. It includes code to perform PCA, transform the dataset into two principal components, and visualize the results with a scatter plot. The explained variance ratio indicates that the first principal component explains approximately 95.6% of the variance in the data.

Uploaded by

dhakeyashashree
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/ 2

4/3/25, 1:54 AM Untitled7.

ipynb - Colab

NAME-MITALI CHOPADE PRN-221101030 TE-AI&DS ROLL NO-03

Experiment No-08

import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

# Define dataset (5 samples, 4 numerical features)


data = np.array([
[2.5, 3.1, 4.7, 8.2],
[1.2, 2.8, 3.5, 7.5],
[3.1, 3.9, 5.2, 9.1],
[2.9, 3.7, 4.9, 8.8],
[1.8, 2.5, 3.7, 7.2]
])

# Convert to DataFrame
df = pd.DataFrame(data, columns=["Feature1", "Feature2", "Feature3", "Feature4"])

# Print original dataset


print("Original Dataset:\n", df, "\n")

# Perform PCA (reduce to 2 components)


pca = PCA(n_components=2)
principal_components = pca.fit_transform(df)

# Create DataFrame with principal components


pca_df = pd.DataFrame(data=principal_components, columns=["PC1", "PC2"])

# Print transformed dataset


print("Dataset After PCA Transformation:\n", pca_df, "\n")

# Print explained variance ratio


print("Explained Variance Ratio:", pca.explained_variance_ratio_, "\n")

# Plot PCA results


plt.figure(figsize=(6, 4))
plt.scatter(pca_df["PC1"], pca_df["PC2"], alpha=0.7, color='blue')

plt.xlabel("Principal Component 1")


plt.ylabel("Principal Component 2")
plt.title("PCA Projection")
plt.grid()
plt.show()

https://colab.research.google.com/drive/1MTlbWY7t526-xPzjulv4BPbaWoNLT-gP#scrollTo=DLkGDzDiBXO0 1/2
4/3/25, 1:54 AM Untitled7.ipynb - Colab

Original Dataset:
Feature1 Feature2 Feature3 Feature4
0 2.5 3.1 4.7 8.2
1 1.2 2.8 3.5 7.5
2 3.1 3.9 5.2 9.1
3 2.9 3.7 4.9 8.8
4 1.8 2.5 3.7 7.2

Dataset After PCA Transformation:


PC1 PC2
0 0.242792 0.246992
1 -1.563740 -0.411367
2 1.625701 -0.102519
3 1.122231 -0.061085
4 -1.426984 0.327980

Explained Variance Ratio: [0.95593516 0.03985967]

https://colab.research.google.com/drive/1MTlbWY7t526-xPzjulv4BPbaWoNLT-gP#scrollTo=DLkGDzDiBXO0 2/2

You might also like