-
Notifications
You must be signed in to change notification settings - Fork 2
CSOAA multiclass classification
ivan-pavlov edited this page Mar 17, 2019
·
2 revisions
Cost Sensitive One Against All (CSOAA) - A multi-class predictive modeling reduction in Vowpal Wabbit
We will use abalone dataset and will try to predict age groups (based on number of abalone shell rings) from physical measurements
library(mltools)
library(rvw)
aburl = 'http://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data'
abnames = c('sex','length','diameter','height','weight.w','weight.s','weight.v','weight.sh','rings')
abalone = read.table(aburl, header = F , sep = ',', col.names = abnames)
data_full <- abalone
Split number of rings into groups with equal (as possible) number of observations
data_full$group <- bin_data(data_full$rings, bins=3, binType = "quantile")
group_lvls <- levels(data_full$group)
levels(data_full$group) <- c(1, 2, 3)
Prepare variables for CSOAA algorithm
data_full$cost_class_1 <- ifelse(data_full$group == 1, 0.8, 0.1)
data_full$cost_class_2 <- ifelse(data_full$group == 2, 0.8, 0.1)
data_full$cost_class_3 <- ifelse(data_full$group == 3, 0.8, 0.1)
data_full$rings <- factor(data_full$rings)
data_full$tag <- sapply(1:nrow(data_full), function(x) paste0("ex",x))
Prepare indices to split data
ind_train <- sample(1:nrow(data_full), 0.8*nrow(data_full))
Split data into train and test subsets
df_train <- data_full[ind_train,]
df_test <- data_full[-ind_train,]
Set up CSOAA VW model
vwmodel <- vwsetup(dir = "./",
option = "csoaa", num_classes = 3)
Train and test
vwtrain(vwmodel, data = df_train,
namespaces = list(NS1 = list("sex", "rings"), NS2 = list("diameter", "length", "height")),
targets = c("cost_class_1", "cost_class_2", "cost_class_3"), tag = "tag"
)
#> Converting data.frame to VW format
#> Starting VW training session
#> VW v8.6.1
#> Using data file: ./train.vw
#> Using model file: ./vw_1552857569_mdl.vw
#> Command line parameters:
#> --csoaa 3 -d ./train.vw -f ./vw_1552857569_mdl.vw --passes 1
#> final_regressor = ./vw_1552857569_mdl.vw
#> Num weight bits = 18
#> learning rate = 0.5
#> initial_t = 0
#> power_t = 0.5
#> using no cache
#> Reading datafile = ./train.vw
#> num sources = 1
#> average since example example current current current
#> loss last counter weight label predict features
#> 0.700000 0.700000 1 1.0 known 1 6
#> 0.700000 0.700000 2 2.0 known 2 6
#> 0.350000 0.000000 4 4.0 known 3 6
#> 0.262500 0.175000 8 8.0 known 1 6
#> 0.131250 0.000000 16 16.0 known 3 6
#> 0.087500 0.043750 32 32.0 known 1 6
#> 0.054687 0.021875 64 64.0 known 3 6
#> 0.027344 0.000000 128 128.0 known 1 6
#> 0.013672 0.000000 256 256.0 known 2 6
#> 0.006836 0.000000 512 512.0 known 1 6
#> 0.003418 0.000000 1024 1024.0 known 3 6
#> 0.001709 0.000000 2048 2048.0 known 1 6
#>
#> finished run
#> number of examples = 3341
#> weighted example sum = 3341.000000
#> weighted label sum = 0.000000
#> average loss = 0.001048
#> total feature number = 20044
vwpreds <- predict(vwmodel, data = df_test, full_probs = T, quiet = T)
head(vwpreds)
#> V1 V2
#> 1 3 ex2
#> 2 2 ex8
#> 3 2 ex21
#> 4 2 ex23
#> 5 2 ex26
#> 6 1 ex35