Series: Music
python
49 lines
· Updated 2026-02-03
pca04-scratch.py
Music/pca04-scratch.py
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns
# Load the Iris dataset
iris = load_iris()
X = iris.data # Feature matrix
y = iris.target # Labels
feature_names = iris.feature_names
target_names = iris.target_names
# Step 1: Standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 2: Compute the covariance matrix
cov_matrix = np.cov(X_scaled, rowvar=False)
# Step 3: Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)
# Step 4: Sort the eigenvalues and eigenvectors in descending order
sorted_indices = np.argsort(eigenvalues)[::-1]
eigenvalues_sorted = eigenvalues[sorted_indices]
eigenvectors_sorted = eigenvectors[:, sorted_indices]
# Step 5: Select top k eigenvectors (here k = 2)
k = 2
W = eigenvectors_sorted[:, :k]
# Step 6: Project the data onto the new space
X_pca = X_scaled.dot(W)
# Create a DataFrame with PCA results
df_pca = pd.DataFrame(X_pca, columns=['PC1', 'PC2'])
df_pca['species'] = [target_names[i] for i in y]
# Plot the PCA result
plt.figure(figsize=(8, 6))
sns.scatterplot(data=df_pca, x='PC1', y='PC2', hue='species', palette='Set2', s=80)
plt.title('PCA from Scratch (Iris Dataset)')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.grid(True)
plt.tight_layout()
plt.show()
Related articles
Music
python
Updated 2026-02-03
essentia.py
essentia.py — python source code from the Music learning materials (Music/essentia.py).
Read article →
Music
python
Updated 2026-02-03
ex01.py
ex01.py — python source code from the Music learning materials (Music/ex01.py).
Read article →
Music
python
Updated 2026-02-03
extract_midi_notes.py
extract_midi_notes.py — python source code from the Music learning materials (Music/extract_midi_notes.py).
Read article →
Music
python
Updated 2026-02-03
getMidiNotes.py
getMidiNotes.py — python source code from the Music learning materials (Music/getMidiNotes.py).
Read article →
Music
python
Updated 2026-02-03
markovchain_v2.py
markovchain_v2.py — python source code from the Music learning materials (Music/markovchain_v2.py).
Read article →
Music
python
Updated 2026-02-03
musc5211.py
musc5211.py — python source code from the Music learning materials (Music/musc5211.py).
Read article →