Skip to content

Instantly share code, notes, and snippets.

@plushycat
Created May 11, 2025 21:18
Show Gist options
  • Save plushycat/81b81c3f67f36e850026e46e706b290c to your computer and use it in GitHub Desktop.
Save plushycat/81b81c3f67f36e850026e46e706b290c to your computer and use it in GitHub Desktop.
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