Principal Component Analysis (PCA)¶

In this notebook, we will see a practical example of PCA.

Copyright 2024, Liberti Hub


In [19]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Generate synthetic data with three features
np.random.seed(42)
feature1 = np.random.rand(60) * 100
feature2 = feature1 * 0.5 + np.random.normal(0, 5, 60)  # correlated with feature1
feature3 = np.random.rand(60) * 100

# Combine into a DataFrame
data = pd.DataFrame({
    'Feature 1': feature1,
    'Feature 2': feature2,
    'Feature 3': feature3
})

# Standardize the data
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)

# Apply PCA to reduce dimensions to 3
pca = PCA(n_components=3)
pca_data = pca.fit_transform(data_scaled)

# Explained variance for each component
explained_variance = pca.explained_variance_ratio_

# Plot the real data
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(121, projection='3d')
ax1.scatter(data['Feature 1'], data['Feature 2'], data['Feature 3'], color='green')
ax1.set_xlabel('Feature 1')
ax1.set_ylabel('Feature 2')
ax1.set_zlabel('Feature 3')
ax1.set_title('Original Data')
plt.tight_layout()
plt.show()

# Plot the data in the first two principal components
plt.figure(figsize=(8, 6))
plt.scatter(pca_data[:, 0], pca_data[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('Data after PCA Transformation')
plt.show()

# Plot the variance explained by each component
plt.figure(figsize=(8, 6))
plt.bar(range(1, 4), explained_variance, alpha=0.7)
plt.xlabel('Principal Components')
plt.ylabel('Variance Explained (%)')
plt.title('Variance Explained by Each Component')
plt.show()