runnig

SVM training

Oct 21st, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.56 KB | None | 0 0
  1. library('e1071')
  2.  
  3. frm6 <- read.csv("./frm6.csv")
  4. frm7 <- read.csv("./frm7.csv")
  5. fulldata <- merge(frm6, frm7, all=TRUE)
  6.  
  7. # pick only relevant data columns
  8. fulldata <- fulldata[,c("radius", "angle", "class")]
  9.  
  10. # number of rows in the data frame
  11. N <- nrow(fulldata)
  12.  
  13. # add new column of Bs
  14. fulldata$fg <- rep("B",N)
  15.  
  16. # set to "F" those data samples which have "class > 1"
  17. fulldata$fg[fulldata$class > 1] <- "F"
  18.  
  19. # leave only relevant data columns
  20. fulldata <- fulldata[,c("fg","radius", "angle")]
  21.  
  22. # make FG column a column of factors
  23. fulldata$fg <- factor(fulldata$fg)
  24.  
  25. Ntrain <- N * 0.7
  26.  
  27. fulldata.indices <- 1:N
  28.  
  29. # make an array of sample indices to use the selected samples for training
  30. traindata.indices <- sample(fulldata.indices, Ntrain)
  31.  
  32. traindata <- fulldata[traindata.indices, ]
  33. testdata <- fulldata[-traindata.indices, ]
  34.  
  35. #tuned <- tune.svm(fg ~., data=traindata, kernel="radial", gamma= 10^(-6:-1), cost = 10^(1:5))
  36. #summary(tuned)
  37.  
  38. model <- svm(fg ~., data=traindata, kernel="radial", gamma=0.1, cost=1000)
  39. print(model)
  40.  
  41. prediction <- predict(model, testdata[,c("radius", "angle")])
  42. tbl <- table(pred = prediction, true = testdata$fg)
  43.  
  44. true_neg <- tbl[1,1]
  45. true_pos <- tbl[2,2]
  46.  
  47. false_pos <- tbl[1,2]
  48. false_neg <- tbl[2,1]
  49. pos <- true_pos + false_pos
  50. neg <- true_neg + false_neg
  51.  
  52. sensitivity <- true_pos / pos
  53. specificity <- true_neg / neg
  54. precision <- true_pos / (true_pos+false_pos)
  55. recall <- true_pos / (true_pos + false_neg)
  56. accuracy <- (true_pos + true_neg) / (pos + neg)
  57.  
  58. F1 <- 2 * (precision * recall) / (precision + recall)
Advertisement
Add Comment
Please, Sign In to add comment