-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_quality.R
More file actions
69 lines (60 loc) · 2.09 KB
/
Copy pathcluster_quality.R
File metadata and controls
69 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
library(tidyverse)
library(kableExtra)
precision <- function(x) {
precision <- c()
for (i in 1:ncol(x)) {
tp <- max(x[,i]) # finding tp for each column
row_i <- which.max(x[,i])[[1]] # finding row_index of each tp
tp_fp <- sum(x[row_i,]) # sum of row for each tp
precision <- c(precision, tp/tp_fp) # precision = tp/row
}
precision
}
recall <- function(x) {
recall <- c()
for (i in 1:ncol(x)) {
tp <- max(x[,i]) # finding tp for each column
tp_fn <- sum(x[,i]) # sum of each column
recall <- c(recall, tp/tp_fn) # recall = tp/column
}
recall
}
f1 <- function(x) {
2 * (precision(x) * recall(x)) / (precision(x) + recall(x))
}
support <- function(x) {
support <- c()
for (i in 1:ncol(x)) {
support <- c(support, sum(x[,i]))
}
support
}
macro <- function(x) {
macro_precision <- mean(precision(x))
macro_recall <- mean(recall(x))
macro_f1 <- 2 * (macro_precision * macro_recall) / (macro_precision + macro_recall)
out <- cbind(precision = macro_precision, recall = macro_recall, f1 = macro_f1, support = sum(x))
out
}
micro <- function(x, digits) {
tp <- c()
for (i in 1:ncol(x)) {
tp <- c(tp, max(x[,i]))
}
out <- cbind(precision = "", recall = "", f1 = round(sum(tp)/sum(x), digits), support = sum(x))
out
}
weighted.avg <- function(x) {
w_precision <- sum(precision(x)*colSums(x)/sum(x))
w_recall <- sum(recall(x)*colSums(x)/sum(x))
w_f1 <- sum(f1(x)*colSums(x)/sum(x))
out <- cbind(precision = w_precision, recall = w_recall, f1 = w_f1, support = sum(x))
}
cluster_report <- function(x, digits = 5, cap = "some classifier") {
out <- round(cbind(precision = precision(x), recall = recall(x), f1 = f1(x), support = support(x)), digits) %>%
rbind(data.frame(rbind(c("", "", "", ""), micro(x, digits), round(macro(x), digits), round(weighted.avg(x), digits)), row.names = c(" ", "Accuracy", "Macro avg", "Weighted avg"))) # binding it all together
out %>%
kbl(caption = paste("Classification quality measures", cap), booktabs = T) %>%
kable_styling(latex_options = "striped") %>%
row_spec(ncol(x), hline_after = T)
}