Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library('e1071')
- frm6 <- read.csv("./frm6.csv")
- frm7 <- read.csv("./frm7.csv")
- fulldata <- merge(frm6, frm7, all=TRUE)
- # pick only relevant data columns
- fulldata <- fulldata[,c("radius", "angle", "class")]
- # number of rows in the data frame
- N <- nrow(fulldata)
- # add new column of Bs
- fulldata$fg <- rep("B",N)
- # set to "F" those data samples which have "class > 1"
- fulldata$fg[fulldata$class > 1] <- "F"
- # leave only relevant data columns
- fulldata <- fulldata[,c("fg","radius", "angle")]
- # make FG column a column of factors
- fulldata$fg <- factor(fulldata$fg)
- Ntrain <- N * 0.7
- fulldata.indices <- 1:N
- # make an array of sample indices to use the selected samples for training
- traindata.indices <- sample(fulldata.indices, Ntrain)
- traindata <- fulldata[traindata.indices, ]
- testdata <- fulldata[-traindata.indices, ]
- #tuned <- tune.svm(fg ~., data=traindata, kernel="radial", gamma= 10^(-6:-1), cost = 10^(1:5))
- #summary(tuned)
- model <- svm(fg ~., data=traindata, kernel="radial", gamma=0.1, cost=1000)
- print(model)
- prediction <- predict(model, testdata[,c("radius", "angle")])
- tbl <- table(pred = prediction, true = testdata$fg)
- true_neg <- tbl[1,1]
- true_pos <- tbl[2,2]
- false_pos <- tbl[1,2]
- false_neg <- tbl[2,1]
- pos <- true_pos + false_pos
- neg <- true_neg + false_neg
- sensitivity <- true_pos / pos
- specificity <- true_neg / neg
- precision <- true_pos / (true_pos+false_pos)
- recall <- true_pos / (true_pos + false_neg)
- accuracy <- (true_pos + true_neg) / (pos + neg)
- F1 <- 2 * (precision * recall) / (precision + recall)
Advertisement
Add Comment
Please, Sign In to add comment