[R] LiblineaR: read/write model files?

Sam Steingold sds at gnu.org
Sun Jul 15 19:00:15 CEST 2012


> * Sam Steingold <fqf at tah.bet> [2012-07-13 15:51:46 -0400]:
>
> How do I read/write liblinear models to files?
> E.g., if I train a model using the command line interface, I might want
> to load it into R to look the histogram of the weights.
> Or I might want to train a model in R and then apply it using a command
> line interface.

read.liblinear <- function (file) {
  cat("read.liblinear(",file,")\n")
  lines <- readLines(file)
  stopifnot(lines[6]=="w")
  parsed <- strsplit(lines[1:5]," ",fixed=TRUE)
  stopifnot(parsed[[1]][1] == "solver_type")
  stopifnot(parsed[[2]][1] == "nr_class")
  stopifnot(parsed[[3]][1] == "label")
  stopifnot(parsed[[4]][1] == "nr_feature")
  stopifnot(parsed[[5]][1] == "bias")
  stopifnot(as.numeric(parsed[[2]][2]) + 1 == length(parsed[[3]]))
  stopifnot(as.numeric(parsed[[4]][2]) + 6 == length(lines))
  ret <- list(solver.type=parsed[[1]][2],
              label=parsed[[3]][2:length(parsed[[3]])],
              bias=as.numeric(parsed[[5]][2]),
              weight=as.numeric(lines[7:length(lines)]))
  nattr <- length(ret$weight)
  n0 <- length(which(ret$weight==0))
  cat("solver.type:",ret$solver.type,"\nlabel:",ret$label,"\nbias:",ret$bias,
      "\nweight(total:",nattr,"; 0:",n0,"=",(100*n0/nattr),"%)\n")
  print(summary(ret$weight))
  ret
}


-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org http://honestreporting.com
http://www.PetitionOnline.com/tap12009/ http://americancensorship.org
Incorrect time synchronization.



More information about the R-help mailing list