Implementing Tabu Search in R -
i trying implement tabu search on classification dataset namely indian patients liver disease available in uci repository on https://archive.ics.uci.edu/ml/datasets/ilpd+(indian+liver+patient+dataset) facing issues. following code i've used
nf <- 10 ntr <- 193 nte <- 193 library(class) library(e1071) library(caret) library(party) library(nnet) ilpd <- read.csv("c:/users/dell/desktop/codes , datasets/ilpd.csv") nrow(ilpd) set.seed(9850) gp<-runif(nrow(ilpd)) ilpd<-ilpd[order(gp),] idx <- createdatapartition(y = ilpd$class, p = 0.7, list = false) train<-ilpd[idx,] test<-ilpd[-idx,] ver<-test[,11] evaluate <- function(th){ if (sum(th) == 0)return(0) model <- svm(train[ ,th==1], train[,11] , gamma = 0.1, kernel ="sigmoid", na.action = na.omit) pred <- predict(model, test[ ,th==1]) csrate <- sum(pred == ver)/nte penalty <- (nf - sum(th))/nf return(csrate + penalty) } library(tabusearch) res <- tabusearch(size = nf, iters = 2, objfunc = evaluate, config = matrix(1,1,nf), listsize = 5, nrestarts = 4) plot(res) plot(res, "traceplot") summary(res, verbose = true)
error:
error in if (any(co)) { : missing value true/false needed in addition: warning message: in fun(newx[, i], ...) : nas introduced coercion called from: svm.default(train[, th == 1], train[, 11], gamma = 0.1, kernel = "sigmoid", na.action = na.omit)
some part of data
structure(list(age = c(55l, 48l, 14l, 17l, 40l, 37l), gender = c(0l, 0l, 0l, 0l, 1l, 0l), tb = c(0.9, 2.4, 0.9, 0.9, 0.9, 0.7), db = c(0.2, 1.1, 0.3, 0.2, 0.3, 0.2), alkphos = c(116l, 554l, 310l, 224l, 293l, 235l), sgpt = c(36l, 141l, 21l, 36l, 232l, 96l), sgot = c(16l, 73l, 16l, 45l, 245l, 54l), tp = c(6.2, 7.5, 8.1, 6.9, 6.8, 9.5 ), alb = c(3.2, 3.6, 4.2, 4.2, 3.1, 4.9), ag = c(1, 0.9, 1, 1.55, 0.8, 1), class = structure(c(2l, 1l, 2l, 1l, 1l, 1l), .label = c("one", "two"), class = "factor")), .names = c("age", "gender", "tb", "db", "alkphos", "sgpt", "sgot", "tp", "alb", "ag", "class"), row.names = c(216l, 405l, 316l, 103l, 20l, 268l), class = "data.frame")
if me it
i wanted see how tabu worked anyway seemed place start.
basically need test code better, evaluate
did not work. easy test hand creating values of th
, calling evaluate
on them.
also use high level comments organize code , keep track of doing, when posting so save time figuring out intend.
not sure if these results good, amount of data minimal hard tell.
anyway here changed code:
nf <- 10 ntr <- 193 nte <- 193 library(class) library(e1071) library(caret) library(party) library(nnet) ilpd1 <- structure( list( age = c(55l,48l,14l,17l,40l,37l), gender = c(0l,0l,0l,0l,1l,0l), tb = c(0.9,2.4,0.9,0.9,0.9,0.7), db = c(0.2,1.1,0.3,0.2,0.3,0.2), alkphos = c(116l,554l,310l,224l,293l,235l), sgpt = c(36l,141l,21l,36l,232l,96l), sgot = c(16l,73l,16l,45l,245l,54l), tp = c(6.2,7.5,8.1,6.9,6.8,9.5), alb = c(3.2,3.6,4.2,4.2,3.1,4.9), ag = c(1,0.9,1,1.55,0.8,1), class = structure(c(2l,1l,2l,1l,1l,1l), .label = c("one","two"), class = "factor") ), .names = c("age","gender","tb","db","alkphos", "sgpt","sgot","tp","alb","ag","class"), row.names = c(216l,405l,316l,103l,20l,268l), class = "data.frame" ) ilpd <- ilpd1 #ilpd <- read.csv("ilpd.csv") nrow(ilpd) set.seed(9850) # setup test , training data gp <- runif(nrow(ilpd)) ilpd <- ilpd[order(gp),] idx <- createdatapartition(y = ilpd$class,p = 0.7,list = false) train <- ilpd[idx,] test <- ilpd[ - idx,] ver <- test[,11] evaluate <- function(th) { # evaluate tabu value of th # tabusearch use function evaluate points in search space # # if turned off return 0 not interested if(sum(th) == 0) return(0) # want train our svm on columns th==1 svmtrn <- train[,th==1] # need have class varible our label if (is.null(trn$class)) return(0) # train svm # note first argument forumula training model <- svm(class~.,svmtrn,gamma = 0.1,kernel = "sigmoid",na.action = na.omit) pred <- predict(model,test) # evaluate how our prediction worked csrate <- sum(pred == ver) / nte penalty <- (nf - sum(th)) / nf return(csrate + penalty) } library(tabusearch) evaluate(matrix(1,1,nf)) res <- tabusearch(size = nf,iters = 2,objfunc = evaluate, config = matrix(1,1,nf),listsize = 5,nrestarts = 4) plot(res) plot(res,"traceplot") summary(res,verbose = true)
here output results:
[1] 6 [1] 0.005181347 tabu settings type = binary configuration no of algorithm repeats = 1 no of iterations @ each prelim search = 2 total no of iterations = 12 no of unique best configurations = 8 tabu list size = 5 configuration length = 10 no of neighbours visited @ each iteration = 10 results: highest value of objective fn = 0.70518 occurs # of times = 1 optimum number of variables = 3 optimum configuration: [1] 1 0 0 0 0 1 0 0 0 1
and here plot:
Comments
Post a Comment