Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
Created June 19, 2018 14:14
Show Gist options
  • Select an option

  • Save artemklevtsov/d3f915e32ddf2d0bee5a996882f76d69 to your computer and use it in GitHub Desktop.

Select an option

Save artemklevtsov/d3f915e32ddf2d0bee5a996882f76d69 to your computer and use it in GitHub Desktop.
library(data.table)
library(ROCR)
library(ggplot2)
f_plot <- function(predictions, labels) {
if (is.list(predictions) && is.atomic(labels))
labels <- rep.int(list(labels), length(predictions))
p <- prediction(predictions, labels)
# Labels
nm <- names(predictions)
if (is.null(nm))
nm <- make.names(seq_along(predictions))
vals <- performance(p, "f")
data <- data.table(
name = rep.int(nm, lengths(vals@y.values)),
y = unlist(vals@y.values),
x = unlist(vals@x.values)
)
ggplot(data, aes(x = x, y = y, color = name)) +
geom_line() +
labs(y = vals@y.name,
x = vals@x.name,
color = "") +
facet_wrap(~ name, ncol = 1L, scales = "free") +
theme(legend.position = "bottom")
}
library(data.table)
library(ROCR)
library(ggplot2)
pr_plot <- function(predictions, labels) {
if (is.list(predictions) && is.atomic(labels))
labels <- rep.int(list(labels), length(predictions))
p <- prediction(predictions, labels)
# Labels
nm <- names(predictions)
if (is.null(nm))
nm <- make.names(seq_along(predictions))
vals <- performance(p, "prec", "rec")
data <- data.table(
name = rep.int(nm, lengths(vals@y.values)),
y1 = unlist(vals@x.values),
y2 = unlist(vals@y.values),
x = unlist(vals@alpha.values)
)
setnames(data, c("y1", "y2"), c(vals@x.name, vals@y.name))
data <- melt(data, id.vars = c("x", "name"))
ggplot(data, aes(x = x, y = value, color = variable)) +
geom_line() +
labs(y = "Value",
x = vals@alpha.name,
color = "") +
facet_wrap(~ name, ncol = 1L, scales = "free") +
theme(legend.position = "bottom")
}
library(data.table)
library(ROCR)
library(ggplot2)
roc_plot <- function(predictions, labels) {
if (is.list(predictions) && is.atomic(labels))
labels <- rep.int(list(labels), length(predictions))
p <- prediction(predictions, labels)
# AUC
auc <- performance(p, "auc")
auc <- unlist(auc@y.values)
# Labels
nm <- names(predictions)
if (is.null(nm))
nm <- make.names(seq_along(predictions))
nm <- sprintf("%s: AUC = %.3f", nm, auc)
# ROC
vals <- performance(p, "tpr", "fpr")
data <- data.table(
name = rep.int(nm, lengths(vals@y.values)),
x = unlist(vals@x.values),
y = unlist(vals@y.values)
)
# Plot
ggplot(data, aes(x = x, y = y, color = name)) +
geom_path() +
labs(x = vals@x.name,
y = vals@y.name,
color = "") +
theme(legend.position = "bottom",
legend.direction = "vertical")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment