|
| 1 | +#!/usr/bin/evn Rscript |
| 2 | +library(rpart) |
| 3 | +library(randomForest) |
| 4 | +library(e1071) |
| 5 | +data(iris) |
| 6 | +d<-iris |
| 7 | +plot(d[,1], d[,3], col=c(2,3,4)[unclass(d$Species)],main="Origin") |
| 8 | + |
| 9 | +message("=== RandomForest ===") |
| 10 | +forest <- randomForest(Species~.,data=d,ntree=500,importance=T) |
| 11 | +predforest <- predict(forest,d,type="class") |
| 12 | +table(d$Species,predforest) |
| 13 | +plot(d[,1], d[,3], col=c("red","blue","green")[unclass(predforest)],main="Forest") |
| 14 | + |
| 15 | +message("=== Rpart ===") |
| 16 | +tree <- rpart(Species~.,data=d,method="class") |
| 17 | +predtree <- predict(tree,d,type="class") |
| 18 | +table(d$Species,predtree) |
| 19 | +plot(d[,1], d[,3], col=c("red","blue","green")[unclass(predtree)],main="Rpart") |
| 20 | + |
| 21 | +message("=== Cmeans ===") |
| 22 | +r<-cmeans(d[,-5],3,150,verbose=F,method="cmeans",m=2) |
| 23 | +c<-apply(r$membership,1,function(x){if ( max(x) < 0.6 ) { which.max(x)+10 }else{ which.max(x)}} ) |
| 24 | +table(d$Species,c) |
| 25 | +c<-apply(r$membership,1,function(x){rgb(x[1],x[2],x[3])}) |
| 26 | +plot(d[,1], d[,3], col=c,main="Cmeans") |
| 27 | + |
| 28 | +message("=== Ufcl ===") |
| 29 | +r<-cmeans(d[,-5],3,150,verbose=F,method="ufcl",m=2) |
| 30 | +c<-apply(r$membership,1,function(x){if ( max(x) < 0.6 ) { which.max(x)+10 }else{ which.max(x)}} ) |
| 31 | +table(d$Species,c) |
| 32 | +c<-apply(r$membership,1,function(x){rgb(x[1],x[2],x[3])}) |
| 33 | +plot(d[,1], d[,3], col=c,main="Ufcl") |
| 34 | + |
| 35 | +message("=== Kmeans hartigan-wong ===") |
| 36 | +k<-kmeans(d[,-5],3,iter.max=10,nstart=1,algorithm="Hartigan-Wong") |
| 37 | +table(d$Species,k$cluster) |
| 38 | +plot(d[,1], d[,3], col=k$cluster,main="Kmeans hartigan-wong") |
| 39 | + |
| 40 | +message("=== average ===") |
| 41 | +h<-hclust(dist(d[,-5]),method = "average") |
| 42 | +table(d$Species,cutree(h,3)) |
| 43 | +plot(d[,1], d[,3], col=cutree(h,3),main="average") |
| 44 | +h<-hclust(dist(d[,-5])^2,method = "average") |
| 45 | +table(d$Species,cutree(h,3)) |
| 46 | +plot(d[,1], d[,3], col=cutree(h,3),main="average^2") |
| 47 | +message("=== complete ===") |
| 48 | +h<-hclust(dist(d[,-5]),method = "complete") |
| 49 | +table(d$Species,cutree(h,3)) |
| 50 | +plot(d[,1], d[,3], col=cutree(h,3),main="complete") |
| 51 | +h<-hclust(dist(d[,-5])^2,method = "complete") |
| 52 | +table(d$Species,cutree(h,3)) |
| 53 | +plot(d[,1], d[,3], col=cutree(h,3),main="complete^2") |
| 54 | +message("=== median ===") |
| 55 | +h<-hclust(dist(d[,-5]),method = "median") |
| 56 | +table(d$Species,cutree(h,3)) |
| 57 | +plot(d[,1], d[,3], col=cutree(h,3),main="median") |
| 58 | +message("=== ward ===") |
| 59 | +h<-hclust(dist(d[,-5]),method = "ward") |
| 60 | +table(d$Species,cutree(h,3)) |
| 61 | +plot(d[,1], d[,3], col=cutree(h,3),main="ward") |
| 62 | +h<-hclust(dist(d[,-5])^2,method = "ward") |
| 63 | +table(d$Species,cutree(h,3)) |
| 64 | +plot(d[,1], d[,3], col=cutree(h,3),main="ward^2") |
| 65 | + |
| 66 | + |
0 commit comments