-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1268 from slds-lmu/classif-nb
classif NB
- Loading branch information
Showing
14 changed files
with
195 additions
and
61 deletions.
There are no files selected for viewing
Binary file modified
BIN
+178 KB
(560%)
slides/supervised-classification/figure/disc_univariate-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+189 KB
(520%)
slides/supervised-classification/figure/disc_univariate-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+177 KB
(460%)
slides/supervised-classification/figure/disc_univariate-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+171 KB
(550%)
slides/supervised-classification/figure/disc_univariate-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# PREREQ ----------------------------------------------------------------------- | ||
|
||
library(knitr) | ||
library(ggplot2) | ||
library(MASS) | ||
library(mlr3) | ||
library(mlr3learners) | ||
library(mlr3viz) | ||
library(mvtnorm) | ||
|
||
# common settings | ||
set.seed(1234) | ||
plot_width <- 20 | ||
plot_height <- 10 | ||
plot_dpi <- 300 | ||
line_size <- 5 | ||
base_size <- 40 | ||
point_size <- 5 | ||
|
||
options(digits = 3, | ||
width = 65, | ||
str = strOptions(strict.width = "cut", vec.len = 3)) | ||
|
||
# DATA ------------------------------------------------------------------------- | ||
|
||
set.seed(123) | ||
|
||
n = 300 | ||
|
||
classa = data.frame(mvrnorm(n = n, | ||
mu = c(2, 2), | ||
Sigma = matrix(c(2, 0, 0, 2), | ||
ncol = 2, | ||
byrow = TRUE))) | ||
|
||
classb = data.frame(mvrnorm(n = n, | ||
mu = c(10, 7), | ||
Sigma = matrix(c(8, -6, -6, 8), | ||
ncol = 2, | ||
byrow = TRUE))) | ||
|
||
df = cbind(classa, factor(rep("a", ncol(classa)))) | ||
colnames(df) = c("x1", "x2", "y") | ||
|
||
foo = cbind(classb, factor(rep("b", ncol(classb)))) | ||
colnames(foo) = c("x1", "x2", "y") | ||
|
||
df = rbind(df, foo) | ||
|
||
task = TaskClassif$new("gauss_task", | ||
backend = df, | ||
target = "y", | ||
positive = "a") | ||
|
||
learner = lrn("classif.naive_bayes", predict_type = "prob") | ||
learner$train(task) | ||
|
||
tab = learner$model$tables | ||
mus = data.frame(x1 = tab$x1[, 1], x2 = tab$x2[, 1]) | ||
mu1 = as.numeric(mus[1,]) | ||
mu2 = as.numeric(mus[2,]) | ||
sds = data.frame(x1 = tab$x1[, 2], x2 = tab$x2[, 2]) | ||
S1 = diag(sds[1, ]) | ||
S2 = diag(sds[2, ]) | ||
|
||
x1seq = seq(min(df$x1), max(df$x1), length.out = 100) | ||
x2seq = seq(min(df$x2), max(df$x2), length.out = 100) | ||
|
||
# Creating grid for predictions | ||
grid = expand.grid(x1 = x1seq, x2 = x2seq) | ||
grid_preds = as.data.frame(learner$predict_newdata(grid)$prob) | ||
grid_preds$pred_class = factor(apply(grid_preds, 1, function(row) ifelse(row["a"] > row["b"], "a", "b"))) | ||
grid_preds$max_prob = apply(grid_preds[, c("a", "b")], 1, max) | ||
grid = cbind(grid, grid_preds) | ||
|
||
# Recompute density for visualizing distributions | ||
grid_dens1 = grid | ||
grid_dens1$dens = dmvnorm(grid_dens1[, c("x1", "x2")], mean = mu1, sigma = S1) | ||
grid_dens2 = grid | ||
grid_dens2$dens = dmvnorm(grid_dens2[, c("x1", "x2")], mean = mu2, sigma = S2) | ||
|
||
# PLOT ------------------------------------------------------------------------- | ||
|
||
# Generate the plot | ||
orig_data = as.data.frame(task$data()) | ||
pl = ggplot() + | ||
geom_tile(data = grid, aes(x = x1, y = x2, fill = pred_class, alpha = max_prob)) + | ||
geom_contour(data = grid_dens1, aes(x = x1, y = x2, z = dens), color = "#E69F00", alpha = 0.9, lwd = 1.5, bins = 10) + | ||
geom_contour(data = grid_dens2, aes(x = x1, y = x2, z = dens), color = "#56B4E9", alpha = 0.9, lwd = 1.5, bins = 10) + | ||
geom_point(data = orig_data, aes(x = x1, y = x2, color = y), size = point_size) + | ||
guides(shape = FALSE, alpha = FALSE) + | ||
scale_fill_manual(values = c("a" = "#E69F00", "b" = "#56B4E9")) + | ||
scale_color_manual(values = c("a" = "#E69F00", "b" = "#56B4E9")) + | ||
labs(x = expression(x[1]), y = expression(x[2]), color = "class", fill = "class") + | ||
theme_minimal() + | ||
scale_alpha(range = c(0.1, 0.5), guide = 'none') + | ||
theme( | ||
plot.title = element_text(hjust = 0.5, size = base_size, face = "bold"), | ||
axis.title = element_text(size = base_size, face = "bold"), | ||
axis.text = element_text(size = base_size * 0.75, face = "bold"), | ||
legend.title = element_text(size = base_size, face = "bold"), | ||
legend.text = element_text(size = base_size * 0.75, face = "bold") | ||
) | ||
|
||
# Save directly to PNG | ||
ggsave(filename = "../figure/nb-db.png", plot = pl, width = plot_width, height = plot_height, dpi = plot_dpi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# goal is to benchmark QDA versus NB versus LDA | ||
library(mlr3) | ||
library(mlr3learners) | ||
library(mlr3pipelines) | ||
library(mlr3viz) | ||
library(ggplot2) | ||
library(data.table) | ||
|
||
set.seed(123) | ||
|
||
task = tsk("spam") | ||
|
||
# because otherwise QDA throws a rank deficiency (because of factors in features I believe?) | ||
pca = po("pca", rank. = 56) # we only reduce the dimension by 1 | ||
|
||
lrn_qda = GraphLearner$new(pca %>>% lrn("classif.qda")) | ||
lrn_nb = GraphLearner$new(pca %>>% lrn("classif.naive_bayes")) | ||
lrn_lda = GraphLearner$new(pca %>>% lrn("classif.lda")) | ||
|
||
learners = list(lrn_nb, lrn_lda, lrn_qda) | ||
bmr = benchmark(benchmark_grid(task, learners, rsmp("cv", folds = 5))) | ||
|
||
a <- autoplot(bmr, type = "boxplot") + | ||
ylab("CE for 5-fold CV") + | ||
xlab("Learners") + | ||
scale_x_discrete(labels = c("QDA", "NB", "LDA")) + | ||
theme_minimal() + | ||
theme( | ||
axis.title = element_text(size = 22, face = "bold"), | ||
axis.text = element_text(size = 20, face = "bold"), | ||
legend.title = element_text(size = 22, face = "bold"), | ||
legend.text = element_text(size = 20, face = "bold"), | ||
axis.text.x = element_text(angle = 45, hjust = 1) | ||
) | ||
ggsave("../figure/nb-bench.png", plot = a, width = 12, height = 8, dpi = 300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters