From 9e46ffd3fbd70dbb466258e84941710d21b817d1 Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Tue, 25 Nov 2025 19:51:35 +0100 Subject: [PATCH] Add geyser.R for unsupervised learning; update README with new section --- M2/Unsupervised Learning/geyser.R | 130 ++++++++++++++++++++++++++++++ README.md | 4 +- 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 M2/Unsupervised Learning/geyser.R diff --git a/M2/Unsupervised Learning/geyser.R b/M2/Unsupervised Learning/geyser.R new file mode 100644 index 0000000..8a102b1 --- /dev/null +++ b/M2/Unsupervised Learning/geyser.R @@ -0,0 +1,130 @@ +# Chargement de la librairie mclust +library(mclust) +# +# n est le nombre de lignes (éruptions) dans les données ’Old Faithful’ +n <- length(faithful[, 1]) +# +# partition initiale aléatoire +set.seed(0) +z.init <- rep(1, n) +n2 <- rbinom(n = 1, size = n, prob = 0.5) +s2 <- sample(1:n, size = n2) +z.init[s2] <- 2 +# +# Déroulement de l’algorithme EM étape par étape +# Initialisation de EM (50 itérations et 2 classes) +itmax <- 50 +G <- 2 +zmat <- matrix(rep(0, n * G), ncol = G) +for (g in (1:G)) { + zmat[, g] <- z.init == g +} +# +# Quelle information contient ’zmat’ ? +# +# Un vecteur est simplement une liste d’éléments du même type +mstep.out <- vector("list", itmax) +estep.out <- vector("list", itmax) + +# Itérations de l’algorithme EM +for (iter in 1:itmax) { + #étape M (maximisation) + mstep.tmp <- mstep(modelName = "VVV", data = faithful, z = zmat) + mstep.out[[iter]] <- mstep.tmp + # + # Quelle information est donnée par ’mstep.tmp’ ? + # (Indiquer et décrire ses composants) + # + #étape (Estimation) + estep.tmp <- estep( + modelName = "VVV", + data = faithful, + parameters = mstep.tmp$parameters + ) + estep.out[[iter]] <- estep.tmp + zmat <- estep.tmp$z + # + # Quelle information est donnée par ’step.tmp’ ? + # (Indiquer et décrire ses composants) + # +} + +# Extraction de la partition +# +EMclass <- rep(NA, n) +for (i in 1:n) { + zmati <- zmat[i, ] + # + # Que contient ’zmati’ ? + # + zmat.max <- zmati == max(zmati) + # + # Quelle information est donnée par ’zmat.max’ ? + # + zmat.argmax <- (1:G)[zmat.max] + EMclass[i] <- min(zmat.argmax) + # + # Quelle information est donnée par ’zmat.argmax’ ? + # A quoi sert l’instruction ’EMclass[i] <- min(zmat.argmax)’ ? + # +} + +# Graphe indiquant la position des centres mu1 et mu2 des classes +# On commence par extraire les estimations de mu_{g,d} à chaque itération +d <- length(faithful[1, ]) # dimension des données +mu1 <- matrix(rep(NA, itmax * d), ncol = 2) +mu2 <- matrix(rep(NA, itmax * d), ncol = 2) +for (iter in (1:itmax)) { + mu1[iter, ] <- estep.out[[iter]]$parameters$mean[, 1] + mu2[iter, ] <- estep.out[[iter]]$parameters$mean[, 2] +} +# Afficher les coordonnées des 2 centres à l’itération 1, +# puis leurs coordonnées à l’itération 50. Commenter. +# +# Représentation du déplacement des centres mu1 et mu2 +# +plot( + faithful, + type = "n", + xlab = "Eruptions", + ylab = "Waiting", + main = "Déplacement des moyennes" +) +y1 <- faithful[EMclass == 2, ] +y2 <- faithful[EMclass == 1, ] + +# Que contiennent y1 et y2 ? +# y1 contient les observations classées dans la classe 2 +# y2 contient les observations classées dans la classe 1 +# +lines(mu1[, 1], mu1[, 2], type = "l", lwd = 3, col = "blue") +lines(mu2[, 1], mu2[, 2], type = "l", lwd = 3, col = "red") +points(y1, col = "blue", pch = 1) +points(y2, col = "red", pch = 1) +points(mu1[itmax, 1], mu1[itmax, 2], col = "blue", pch = 19) +points(mu2[itmax, 1], mu2[itmax, 2], col = "red", pch = 19) +legend( + x = min(faithful[, 1]), + y = max(faithful[, 2]), + legend = c("Moyenne 1", "Moyenne 2"), + col = c("red", "blue"), + lty = c(1, 1), + lwd = c(3, 3) +) + +# Loading the mclust library +library(mclust) +data(faithful) +# Clustering avec Mclust +faithful.Mclust2 <- Mclust(faithful, model = "VVV", G = 2) +# +# Affichage de l’incertitude sur les affectations +# +plot(faithful.Mclust2, what = "uncertainty") +surfacePlot( + faithful, + faithful.Mclust2$parameters, + type = "contour", + what = "uncertainty" +) +points(faithful) diff --git a/README.md b/README.md index d7e4322..8cd06a1 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,10 @@ The projects are organized into two main sections: - `Linear Models` - `Machine Learning` - `Reinforcement Learning` - - `VBA` - `SQL` + - `Unsupervised Learning` + - `VBA` + ## 🛠️ Technologies & Tools