시리즈: Music
python
59 줄
· 업데이트 2026-02-03
pca03-3D.py
Music/pca03-3D.py
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 1. Load Iris dataset from CSV
csv_path = "./iris.csv"
df = pd.read_csv(csv_path)
# 2. Add synthetic features
df['sepal area'] = df['sepal length (cm)'] * df['sepal width (cm)']
df['petal area'] = df['petal length (cm)'] * df['petal width (cm)']
df['sepal/petal ratio'] = (df['sepal length (cm)'] + 1) / (df['petal length (cm)'] + 1)
# 3. Standardize features
features = df.columns.difference(['species'])
scaler = StandardScaler()
df_scaled = pd.DataFrame(scaler.fit_transform(df[features]), columns=features)
df_scaled['species'] = df['species']
# 4. Apply KMeans clustering
kmeans = KMeans(n_clusters=3, random_state=42)
df_scaled['cluster'] = kmeans.fit_predict(df_scaled[features])
# 5. Apply PCA (3D)
pca = PCA(n_components=3)
pca_components = pca.fit_transform(df_scaled[features])
df_pca = pd.DataFrame(pca_components, columns=['PC1', 'PC2', 'PC3'])
df_pca['cluster'] = df_scaled['cluster']
df_pca['species'] = df['species']
# 6. Save to CSV
df_pca.to_csv("./iris_pca_3d_clusters.csv", index=False)
# 7. Plot 3D PCA
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
colors = ['r', 'g', 'b']
for cluster_id in range(3):
ax.scatter(
df_pca[df_pca['cluster'] == cluster_id]['PC1'],
df_pca[df_pca['cluster'] == cluster_id]['PC2'],
df_pca[df_pca['cluster'] == cluster_id]['PC3'],
label=f'Cluster {cluster_id}',
s=60,
alpha=0.8
)
ax.set_title("3D PCA with KMeans Clustering on Complex Iris Dataset")
ax.set_xlabel(f'PC1 ({pca.explained_variance_ratio_[0]*100:.1f}%)')
ax.set_ylabel(f'PC2 ({pca.explained_variance_ratio_[1]*100:.1f}%)')
ax.set_zlabel(f'PC3 ({pca.explained_variance_ratio_[2]*100:.1f}%)')
ax.legend()
plt.tight_layout()
plt.show()
관련 글
Music
python
업데이트 2026-02-03
essentia.py
essentia.py — python source code from the Music learning materials (Music/essentia.py).
글 읽기 →
Music
python
업데이트 2026-02-03
ex01.py
ex01.py — python source code from the Music learning materials (Music/ex01.py).
글 읽기 →
Music
python
업데이트 2026-02-03
extract_midi_notes.py
extract_midi_notes.py — python source code from the Music learning materials (Music/extract_midi_notes.py).
글 읽기 →
Music
python
업데이트 2026-02-03
getMidiNotes.py
getMidiNotes.py — python source code from the Music learning materials (Music/getMidiNotes.py).
글 읽기 →
Music
python
업데이트 2026-02-03
markovchain_v2.py
markovchain_v2.py — python source code from the Music learning materials (Music/markovchain_v2.py).
글 읽기 →
Music
python
업데이트 2026-02-03
musc5211.py
musc5211.py — python source code from the Music learning materials (Music/musc5211.py).
글 읽기 →