Dimensionsreduktion und Clustering — Unsupervised Machine Learning
\[ \text{min} \left( \frac{1}{n} \sum_{i}^{n} \left( x_i^T x_i - (u_1^T x_i)^2 \right) \right) \]
# Daten laden und skalieren
data(iris)
iris_scaled <- scale(iris[, -5])
# PCA durchführen
pca <- prcomp(iris_scaled)
# Zusammenfassung der PCA
summary(pca)
# PCA-Plot
library(ggplot2)
pca_data <- as.data.frame(pca$x)
pca_data$Species <- iris$Species
ggplot(pca_data, aes(x = PC1, y = PC2, color = Species)) +
geom_point() +
labs(title = "PCA der Iris-Daten", x = "Hauptkomponente 1", y = "Hauptkomponente 2")
# K-Means Clustering durchführen
set.seed(123)
kmeans_result <- kmeans(iris_scaled, centers = 3)
# K-Means Plot
kmeans_data <- as.data.frame(iris_scaled)
kmeans_data$Cluster <- factor(kmeans_result$cluster)
ggplot(kmeans_data, aes(x = PC1, y = PC2, color = Cluster)) +
geom_point() +
labs(title = "K-Means Clustering der Iris-Daten", x = "Hauptkomponente 1", y = "Hauptkomponente 2")