Principal Component Analysis (PCA)

Performing PCA on multivariate data with Scikit-Learn

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

# Generate sample data
np.random.seed(42)
n_samples = 100
n_features = 3

X = np.random.randn(n_samples, n_features)
X[:, 0] += np.linspace(0, 5, n_samples)  # Add some correlation

# Perform PCA
pca = PCA()
pca.fit(X)

# Transform the data
X_pca = pca.transform(X)

# Print explained variance ratio
print("Explained variance ratio:")
print(pca.explained_variance_ratio_)

# Plot the first two principal components
plt.figure(figsize=(10, 6))
plt.scatter(X_pca[:, 0], X_pca[:, 1], alpha=0.7)
plt.title("PCA: First Two Principal Components")
plt.xlabel("PC1")
plt.ylabel("PC2")
plt.show()
Click Run or press shift + ENTER to run code.