annotate accuracy.R @ 39:5447fc8946ed draft default tip

Uploaded
author testtool
date Fri, 13 Oct 2017 11:22:49 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39
5447fc8946ed Uploaded
testtool
parents:
diff changeset
1 require(caret, quietly = TRUE)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
2
5447fc8946ed Uploaded
testtool
parents:
diff changeset
3 args <- commandArgs(trailingOnly = TRUE)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
4
5447fc8946ed Uploaded
testtool
parents:
diff changeset
5 input = args[1]
5447fc8946ed Uploaded
testtool
parents:
diff changeset
6 p = args[2]
5447fc8946ed Uploaded
testtool
parents:
diff changeset
7 output1 = args[3]
5447fc8946ed Uploaded
testtool
parents:
diff changeset
8 output2 = args[4]
5447fc8946ed Uploaded
testtool
parents:
diff changeset
9
5447fc8946ed Uploaded
testtool
parents:
diff changeset
10 dataset <- read.csv(input, header=TRUE)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
11
5447fc8946ed Uploaded
testtool
parents:
diff changeset
12 validation_index <- createDataPartition(dataset$Species, p=p, list=FALSE)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
13
5447fc8946ed Uploaded
testtool
parents:
diff changeset
14 validation <- dataset[-validation_index,]
5447fc8946ed Uploaded
testtool
parents:
diff changeset
15
5447fc8946ed Uploaded
testtool
parents:
diff changeset
16 validdataset <- dataset[validation_index,]
5447fc8946ed Uploaded
testtool
parents:
diff changeset
17
5447fc8946ed Uploaded
testtool
parents:
diff changeset
18 percentage <- prop.table(table(validdataset$Species)) * 100
5447fc8946ed Uploaded
testtool
parents:
diff changeset
19 cbind(freq=table(validdataset$Species), percentage=percentage)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
20
5447fc8946ed Uploaded
testtool
parents:
diff changeset
21 output_summary <- summary(validdataset)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
22 write.csv(output_summary,output1)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
23
5447fc8946ed Uploaded
testtool
parents:
diff changeset
24 control <- trainControl(method="cv", number=10)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
25 metric <- "Accuracy"
5447fc8946ed Uploaded
testtool
parents:
diff changeset
26
5447fc8946ed Uploaded
testtool
parents:
diff changeset
27 # a) linear algorithms
5447fc8946ed Uploaded
testtool
parents:
diff changeset
28 set.seed(7)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
29 fit.lda <- train(Species~., data=validdataset, method="lda", metric=metric, trControl=control)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
30 # b) nonlinear algorithms
5447fc8946ed Uploaded
testtool
parents:
diff changeset
31 # CART
5447fc8946ed Uploaded
testtool
parents:
diff changeset
32 set.seed(7)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
33 fit.cart <- train(Species~., data=validdataset, method="rpart", metric=metric, trControl=control)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
34 # kNN
5447fc8946ed Uploaded
testtool
parents:
diff changeset
35 set.seed(7)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
36 fit.knn <- train(Species~., data=validdataset, method="knn", metric=metric, trControl=control)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
37 # c) advanced algorithms
5447fc8946ed Uploaded
testtool
parents:
diff changeset
38 # SVM
5447fc8946ed Uploaded
testtool
parents:
diff changeset
39 set.seed(7)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
40 fit.svm <- train(Species~., data=validdataset, method="svmRadial", metric=metric, trControl=control)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
41 # Random Forest
5447fc8946ed Uploaded
testtool
parents:
diff changeset
42 set.seed(7)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
43 fit.rf <- train(Species~., data=validdataset, method="rf", metric=metric, trControl=control)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
44
5447fc8946ed Uploaded
testtool
parents:
diff changeset
45 results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))
5447fc8946ed Uploaded
testtool
parents:
diff changeset
46 output_results <- summary(results)
5447fc8946ed Uploaded
testtool
parents:
diff changeset
47
5447fc8946ed Uploaded
testtool
parents:
diff changeset
48 write.csv(as.matrix(output_results),output2)