Created
May 10, 2021 19:27
-
-
Save nazareno/b63c3fbe14aa7c70127cf9a106b36a1b to your computer and use it in GitHub Desktop.
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
--- | |
title: "IC de precisão, recall, F1..." | |
output: html_notebook | |
--- | |
```{r setup, include=FALSE, message=FALSE, warning=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
library(tidyverse) | |
library(hrbrthemes) | |
theme_set(theme_ipsum_rc()) | |
library(boot) | |
library(broom) | |
``` | |
```{r} | |
conj_teste = tibble( | |
imagem = 1:200, | |
label = c(rep("Com gato", 100), rep("Sem gato", 100)) | |
) | |
``` | |
```{r} | |
resultado_amostra = bind_rows( | |
conj_teste %>% | |
mutate( | |
classificador = "Modelo 1", | |
previsao = if_else( | |
runif(200) > .2, | |
label, | |
if_else(label == "Com gato", "Sem gato", "Com gato")) | |
), | |
conj_teste %>% | |
mutate( | |
classificador = "Modelo 2", | |
previsao = if_else( | |
runif(200) > .5, | |
"Com gato", | |
"Sem gato" | |
) | |
) | |
) | |
``` | |
```{r} | |
resultado_amostra %>% | |
group_by(classificador) %>% | |
summarise( | |
n = n(), | |
vp = sum(label == "Com gato" & label == previsao), | |
fp = sum(label == "Com gato" & label != previsao), | |
vn = sum(label == "Sem gato" & label == previsao), | |
fn = sum(label == "Sem gato" & label == previsao), | |
) | |
``` | |
```{r} | |
precisao = function(d, i){ | |
d %>% | |
slice(i) %>% | |
summarise( | |
vp = sum(label == "Com gato" & label == previsao), | |
fp = sum(label == "Com gato" & label != previsao), | |
precisao = vp / (vp + fp) | |
) %>% | |
pull(precisao) | |
} | |
``` | |
```{r} | |
resultado_amostra %>% | |
filter(classificador == "Modelo 1") %>% | |
precisao(i = 1:nrow(.)) | |
``` | |
```{r} | |
booted <- boot(data = resultado_amostra %>% filter(classificador == "Modelo 1"), | |
statistic = precisao, | |
R = 2000) | |
estimado_m1 = tidy(booted, | |
conf.level = .95, | |
conf.method = "bca", | |
conf.int = TRUE) | |
glimpse(estimado_m1) | |
``` | |
```{r} | |
booted <- boot(data = resultado_amostra %>% filter(classificador == "Modelo 2"), | |
statistic = precisao, | |
R = 2000) | |
estimado_m2 = tidy(booted, | |
conf.level = .95, | |
conf.method = "bca", | |
conf.int = TRUE) | |
glimpse(estimado_m2) | |
``` | |
```{r} | |
bind_rows(modelo1 = estimado_m1, | |
modelo2 = estimado_m2, | |
.id = "modelo") %>% | |
ggplot(aes( | |
ymin = conf.low, | |
y = statistic, | |
ymax = conf.high, | |
x = modelo | |
)) + | |
geom_linerange() + | |
geom_point(color = "steelblue", size = 3) + | |
geom_text( | |
aes( | |
y = conf.high, | |
label = str_glue("[{round(conf.low, 2)}, {round(conf.high, 2)}]") | |
), | |
size = 3, | |
nudge_x = -.05, | |
show.legend = F | |
) + | |
scale_y_continuous(limits = c(0, 1)) + | |
labs( | |
title = "Precisão dos classificadores", | |
x = "", y = "Precisão") + | |
coord_flip() | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment