Admin Admin
Nombre de messages : 418 Date d'inscription : 27/09/2005
| Sujet: TP2 Mar 22 Nov à 13:08 | |
| - Code:
-
# generate data # first generate means for each class nmix <- 10 # number of mixture components (nb de paquets de #gaussienne) x <- rnorm(nmix,0,1) #10 variables aleat. N(0,1) y <- rnorm(nmix,1,1) #10 variables aleat. N(1,1) means_pos = cbind(x,y) #colle x et y a côté x <- rnorm(nmix,1,1) #10 variables aleat. N(1,1) y <- rnorm(nmix,0,1) #10 variables aleat. N(0,1) means_neg = cbind(x,y) #colle x et y a côté
# generate n examples u <- array(1,c(1,nmix)) #tableau à 10 "1" u <- u/nmix #tableau à 10 "0.1" n <- 100
# first generate which mixture components m_neg = sample(1:nmix, n, replace = TRUE, prob = u) #echantillon de n nombres #entre 1 et nmix de proba u m_pos = sample(1:nmix, n, replace = TRUE, prob = u) x_neg <- array(0,c(n,2)) #tableau 100 lignes 2 col de "0" x_pos <- array(0,c(n,2)) #tableau 100 lignes 2 col de "0"
# assigns means for ( i in 1:n ) { x_neg[i,]=means_neg[m_neg[i],] x_pos[i,]=means_pos[m_pos[i],] } x_neg[,1] = x_neg[,1] + rnorm(n,0,1/5) x_neg[,2] = x_neg[,2] + rnorm(n,0,1/5) x_pos[,1] = x_pos[,1] + rnorm(n,0,1/5) x_pos[,2] = x_pos[,2] + rnorm(n,0,1/5)
x <- rbind(x_neg,x_pos);
# plot the data
#plot(x[,1],x[,2],type="n") #points(x_neg[,1],x_neg[,2],col="blue") #points(x_pos[,1],x_pos[,2],col="red")
########################################################################## ########################################################################## # PAS NECESSAIRE POUR CETTE QUESTION # linear regression # x <- rbind(x_neg,x_pos); x <- cbind(array(1,c(2*n,1)),x) y <- rbind(array(-1,c(n,1)),array(1,c(n,1)));
beta <- solve(t(x) %*% x, t(x) %*% y)
x1 = array(seq(-2,3, by= 0.1), dim = c(51,1)) y1 = array(seq(-2,3, by= 0.1), dim = c(51,1))
d = dim(x1)[1]
z_reg = array(0,(d*d), dim = c(d,d))
for(i in 1:d) { for(j in 1:d) { z_reg[i,j] <- t(beta) %*% c(1,x1[i],y1[j]) } }
contour(x1,y1,z_reg, level=0, labels=NULL) points(x_neg[,1],x_neg[,2],col="blue") points(x_pos[,1],x_pos[,2],col="red") ########################################################################## ##########################################################################
# plot the bayesian ( = optimal ) decision boundary
z_bayes <- array(0,d*d, dim = c(d,d))
for(i in 1:d) { for(j in 1:d) { for(k in 1:nmix) { z_bayes[i,j] <- z_bayes[i,j] + ( dnorm(x1[i], means_pos[k,1], 1/5) * dnorm(y1[j], means_pos[k,2], 1/5)) - ( dnorm(x1[i], means_neg[k,1], 1/5) * dnorm(y1[j], means_neg[k,2], 1/5)) } } }
contour(x1,y1,z_bayes, level=0, labels='Bayes') points(x_neg[,1],x_neg[,2],col="blue") points(x_pos[,1],x_pos[,2],col="red")
;
Dernière édition par le Mar 22 Nov à 14:01, édité 11 fois | |
|