Instantly share code, notes, and snippets.

# ac00std/leaf2 Last active Apr 2, 2017

What would you like to do?
leaf2
 #データの読み込み leaf=read.csv("leaf.csv",header = F, colClasses=c("factor",rep("numeric",15)), col.names=c("Class","Specimen Number","Eccentricity","Aspect Ratio","Elongation","Solidity","Stochastic Convexity","Isoperimetric Factor","Maximal Indentation Depth","Lobedness","Average Intensity","Average Contrast","Smoothness","Third moment","Uniformity","Entropy")) leaf=leaf[,-2] y=leaf\$Class x=leaf[,-1] train <- seq.int(1, 340, by=2) test <- setdiff(1:340, train) #線形判別分析 library(MASS) leaf.lda=lda(x[train,],y[train]) test.lda=predict(leaf.lda, x[test,]) lda.tab=table(y[test],test.lda\$class) lda.tab sum(lda.tab[row(lda.tab)==col(lda.tab)])/sum(lda.tab) #識別率 #線形SVM library(e1071) leaf.svm=svm(Class~.,leaf[train,],method = "C-classification", kernel = "linear", cost = 2.5 ) test.svm=predict(leaf.svm, x[test,]) svm.tab=table(y[test],test.svm) svm.tab sum(svm.tab[row(svm.tab)==col(svm.tab)])/sum(svm.tab) #識別率 #RBFカーネルを用いたSVM library(e1071) leaf.rbf_svm=svm(Class~.,leaf[train,],method = "C-classification", kernel = "radial", cost = 20 ) test.rbf_svm=predict(leaf.rbf_svm, x[test,]) rbf_svm.tab=table(y[test],test.rbf_svm) rbf_svm.tab sum(rbf_svm.tab[row(rbf_svm.tab)==col(rbf_svm.tab)])/sum(rbf_svm.tab) #識別率 #randomforest library(randomForest) tuneRF(x[train,],y[train],doBest=T) leaf.rf=randomForest(x[train,],y[train],mtry=6) test.rf = predict(leaf.rf, x[test,]) rf.tab=table(y[test],test.rf) sum(rf.tab[row(rf.tab)==col(rf.tab)])/sum(rf.tab) #識別率 #kNN library(class) p2=knn(x[train,],x[test,],y[train],k=2) kNN.tab=table(y[test],p2) kNN.tab sum(kNN.tab[row(kNN.tab)==col(kNN.tab)])/sum(kNN.tab) #識別率 p5=knn(x[train,],x[test,],y[train],k=5) kNN.tab=table(y[test],p5) kNN.tab sum(kNN.tab[row(kNN.tab)==col(kNN.tab)])/sum(kNN.tab) #識別率 #naive bayes library(e1071) leaf.nb=naiveBayes(Class~.,data=leaf[train,]) test.nb=predict(leaf.nb,x[test,]) nb.tab=table(y[test],test.nb) nb.tab sum(nb.tab[row(nb.tab)==col(nb.tab)])/sum(nb.tab) #識別率