Skip to content

Instantly share code, notes, and snippets.

@nivangio
Created March 28, 2016 21:59
Show Gist options
  • Save nivangio/3a86e42cba1737251f6d to your computer and use it in GitHub Desktop.
Save nivangio/3a86e42cba1737251f6d to your computer and use it in GitHub Desktop.
library(adaStump)
#Load Letters Dataset
letters.data <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/letter-recognition/letter-recognition.data", header = F)
names(letters.data) <- c("lettr", "xbox", "ybox", "width",
"high", "onpix", "xbar", "ybar",
"x2bar", "y2bar", "xybar", "x2ybr",
"xy2br", "xege", "xegvy", "yege", "yegvx")
#Create a binary clas
letters.data$isA <- ifelse(letters.data$lettr == "A","Yes","No")
#Create model formula. Exclude letter var for obvious reasons
antec <- paste0(setdiff(names(letters.data),c("isA","lettr")), collapse = "+")
fla <- as.formula(paste("isA", antec, sep = "~"))
#### Model training comparison ####
#ada model fit passing the corresponding control function to create Stumps
fit_with.ada <- ada(formula = fla,data = letters.data, type = "real",
control = rpart.control(maxdepth=1,cp=-1,minsplit=0,xval=0), iter = 40, nu = 0.05, bag.frac = 1)
#adaStump
fit_with.adaStump <- adaStump(formula = fla,data = letters.data,
type = "real", iter = 40, nu = 0.05, bag.frac = 1)
#Object size comparison
format(object.size(fit_with.ada), units = "KB")
format(object.size(fit_with.adaStump), units = "KB")
#### Model testing ####
system.time(predictions_ada <- predict(fit_with.ada, letters.data, type = "prob"))
system.time(predictions_adaStump <- predict(fit_with.adaStump, letters.data))
#### Model pruning ####
fit_with.prunedadaStump <- pruneTree(fit_with.adaStump)
system.time(predictions_prunedadaStump <- predict(fit_with.prunedadaStump, letters.data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment