Created
September 18, 2012 17:49
-
-
Save darmitage/3744607 to your computer and use it in GitHub Desktop.
Classification of zero-crossings bat calls using machine learning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(kernlab) | |
require(caret) | |
require(e1071) | |
require(nnet) | |
require(randomForest) | |
set.seed(34873458) | |
#Open call library (needs name (eg. MYLU), and parameters) | |
setwd = ("Dropbox/Anabat files/Dave's Florida Bat Calls/") | |
callsamplesALLNNET=read.csv(file="Dropbox/Anabat files/FloridaBatsALL.csv",header=T,sep=",") | |
callsamplesALLNNET[,-1]=scale(callsamplesALLNNET[,-1], center = T, scale = T) | |
fitControl <- trainControl(method = "cv", number = 10, returnResamp = "all", verboseIter = FALSE) | |
#train ANN on voucher calls | |
nnet1 <- train(Name ~., data = callsamplesALLNNET, method = "nnet", tuneLength = 10, | |
trControl = fitControl) | |
nnet1 | |
#open table of unknown calls (Filename = analook file with date and time) parameters must be identical to voucher calls above. | |
#test calls here are output of Analook scan of particular night's folder | |
testNNET = read.csv("Dropbox/Anabat files/test.csv", head = T, sep = ",") | |
testNNET$facName = factor(testNNET$Filename) | |
levels(testNNET$facName) = 1:1000 | |
samp = testNNET$facName | |
testNNET[,2:12] = scale(testNNET[,2:12], center = TRUE, scale = TRUE) | |
#predict identity of unknown calls using NNET | |
nnetpred <- predict(nnet1, newdata = testNNET[,2:12]) | |
species = data.frame(nnetpred) | |
res = cbind(samp, species) | |
#return a consensus (i.e. mode) of call assignments for each sequence | |
df.list <-vector("list",max(as.numeric(res$samp))) | |
for (i in 1:max(as.numeric(res$samp))) { | |
set = subset(res, samp==i) | |
consensus = names(sort(-table(set$nnetpred)))[1] | |
df.list[[i]] = consensus } | |
print(df.list) | |
results = cbind(levels(testNNET[,1]),as.matrix(df.list)) | |
colnames(results) = c("sequence", "consensus ID") | |
write.csv(results, file = "Dropbox/Anabat files/results.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment