# Aufgabe 1 cr<-read.table("Datasets/crabs.txt",T) library(nnet) cr$sex<-as.factor(cr$sex) attach(cr) n<-nnet(sex~FL+RW+CL+CW+BD,data=cr,size=1) table(predict(n,type="c"),sex) summary(n) n<-nnet(sex~FL+RW+CL+CW+BD,data=cr,skip=TRUE,size=1) table(predict(n,type="c"),sex) summary(n) mkgrid <- function(x,y,points=50) { points<-rep(points,length.out=2) xv<-seq(min(x),max(x),length=points[1]) yv<-seq(min(y),max(y),length=points[2]) matrix(c(rep(xv,points[2]),rep(yv,each=points[1])),ncol=2) } g<-mkgrid(FL,RW,100) g<-as.data.frame(g) names(g)<-c("FL","RW") n<-nnet(sex~FL+RW,data=cr,size=1,maxit=1000) plot(FL,RW,col=unclass(sex)) points(g,col=predict(n,g,type="c"),pch=3) # Aufgabe 2 # die einfachsten Loesungen fuer XOR punkte: # - 2i-1h-1o + skip # - 2i-2h-1o # fuer XOR-Flaechen: # - 2i-2h-1o + skip # - 2i-3h-1o xd<-data.frame( X=c(rnorm(100),rnorm(100)+6), Y=c(rnorm(50),rnorm(100)+6,rnorm(50)), cl=as.factor(rep(c(1,2),each=50,length.out=200))) g<-as.data.frame(mkgrid(xd$X,xd$Y,100)) names(g)<-c("X","Y") n<-nnet(cl~.,xd,size=1,skip=T,maxit=1000) plot(xd$X,xd$Y,col=unclass(xd$cl)) points(g,col=predict(n,g,type="c"),pch=3) # Xtra zum Probieren: xd<-data.frame( X=c(runif(400),runif(400)+1), Y=c(runif(200),runif(400)+1,runif(200)), cl=as.factor(rep(c(1,2),each=200,length.out=800))) g<-as.data.frame(mkgrid(xd$X,xd$Y,100)) names(g)<-c("X","Y") n<-nnet(cl~.,xd,size=3,skip=F,maxit=1000) plot(xd$X,xd$Y,col=unclass(xd$cl)) points(g,col=predict(n,g,type="c"),pch=3) # und zuletzt ein ganz nettes Netz fuer die obigen Daten: n$wts<-c(0,3,0,0,0,-3,-3,3,3,-38,-800,800,800) # daten sind in 0..2, also 3=1.5 fuer 0..1; 800=# Faelle plot(xd$X,xd$Y,col=unclass(xd$cl)) points(g,col=predict(n,g,type="c"),pch=3) # Aufgabe 3 o<-read.table("Datasets/Olives/olives",T) otr<-subset(o,o$Test=="Training")[2:10] ott<-subset(o,o$Test=="Test")[2:10] n<-nnet(Region~.,otr,size=3,linout=T,skip=T) table(predict(n,type="c"),otr$Region) table(predict(n,ott,type="c"),ott$Region) # Aufgabe 4 cr<-read.table("Datasets/crabs.txt",T) cr<-cbind(cr,group=factor(cr$sex+cr$sp*2-2)) i<-sample(200,50) # hier z.B. tr:test = 3:1 ic<-setdiff(1:200,i) ctt<-cr[4:9][i,] ctr<-cr[4:9][ic,] n<-lapply(1:50, function(k) nnet(group~.,ctr,size=k)) etr<-unlist(lapply(n,function(x) sum(predict(x,type="c")!=ctr$group)))/150 plot(etr) ett<-unlist(lapply(n,function(x) sum(predict(x,ctt,type="c")!=ctt$group)))/50 points(ett,col=2) plot(ett/etr) # Aufgabe 5 olives<-read.table("Datasets/Olives/olives",T) olives$Area <- as.integer(olives$Area) attach(olives) n<-length(olives[,1]) fold <- 5 chunk <- trunc(n/fold) nn <- chunk*fold runs <- 20 CV <- matrix(0, runs, 1) for(i in (1:runs)) { for(j in 1:fold) { test <- (1:chunk)*fold-fold+j train <- (1:nn)[-test] onet <- nnet(olives[3:10], Area, size=i, subset=train, decay=0.001, skip=T, linout=T, maxit=1000) pred <- round(predict(onet, newdata=olives[test,3:10])) pred[pred>9] <- 9 pred[pred<1] <- 1 pred<-factor(pred, levels=1:9) conf <- as.matrix(table(pred, Area[test])) CV[i] <- CV[i] + sum(conf - diag(diag(conf), 9, 9)) } } plot(CV, xlab="Size", pch="o") lines(lowess(CV ~ (1:runs)))