Created
May 11, 2025 21:18
-
-
Save plushycat/81b81c3f67f36e850026e46e706b290c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from sklearn.datasets import load_iris | |
from sklearn.decomposition import PCA | |
import matplotlib.pyplot as plt | |
# Load data and perform PCA | |
iris = load_iris() | |
data_reduced = PCA(n_components=2).fit_transform(iris.data) | |
# Create DataFrame with reduced dimensions and labels | |
df = pd.DataFrame(data_reduced, columns=['PC1', 'PC2']) | |
df['Label'] = iris.target | |
# Plot results | |
plt.figure(figsize=(8, 6)) | |
for i, label_name in enumerate(iris.target_names): | |
mask = df['Label'] == i | |
plt.scatter(df.loc[mask, 'PC1'], df.loc[mask, 'PC2'], | |
label=label_name, color=['r', 'g', 'b'][i]) | |
plt.title('PCA on Iris Dataset') | |
plt.xlabel('Principal Component 1') | |
plt.ylabel('Principal Component 2') | |
plt.legend() | |
plt.grid() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment