Skip to content

Commit

Permalink
plots changed
Browse files Browse the repository at this point in the history
plots theme changed
lollipop plot: threshold agrument added, closed #2
radar plot: labels changed, text_start_point and text_size arguments added
  • Loading branch information
ekarbowiak committed Mar 28, 2019
1 parent 3debdc9 commit 8929887
Show file tree
Hide file tree
Showing 50 changed files with 92 additions and 578 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: EIX
Title: EIX: Explain Interactions in XGBoost
Structure mining for XGBoost and LightGBM models
Version: 0.1
Version: 0.2
Authors@R: c(
person("Ewelina", "Karbowiak", email = "ewelina.karbowiak12@gmail.com", role = c("aut", "cre")),
person("Przemyslaw", "Biecek", email = "przemyslaw.biecek@gmail.com", role = c("aut","ths"))
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export(lollipop)
export(waterfall)
import(data.table)
import(ggplot2)
importFrom(DALEX,theme_mi2)
importFrom(DALEX,theme_drwhy)
importFrom(ggiraphExtra,coord_radar)
importFrom(ggrepel,geom_label_repel)
importFrom(ggrepel,geom_text_repel)
Expand Down
1 change: 1 addition & 0 deletions R/lollipop.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#' @import data.table
#'
#' @examples
#' options(warn = -1)
#' library("EIX")
#' library("Matrix")
#' sm <- sparse.model.matrix(left ~ . - 1, data = HR_data)
Expand Down
33 changes: 22 additions & 11 deletions R/plot_importance.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#' six measures of variables' or interactions' importance in the model.
#' If FALSE the plot containing two chosen measures
#' of variables' or interactions' importance in the model.
#' @param text_start_point place, where the names of the particular feature start. Available for `radar=TRUE`. Range from 0 to 1. Default 0.5.
#' @param text_size size of the text on the plot. Default 3.5.
#' @param xmeasure measure on the x-axis.Available for `radar=FALSE`. Default "sumCover".
#' @param ymeasure measure on the y-axis. Available for `radar=FALSE`. Default "sumGain".
#' @param ... other parameters.
Expand All @@ -35,7 +37,7 @@
#'
#' @import ggplot2
#' @import data.table
#' @importFrom DALEX theme_mi2
#' @importFrom DALEX theme_drwhy
#' @importFrom ggrepel geom_label_repel
#' @importFrom ggiraphExtra coord_radar
#'
Expand Down Expand Up @@ -83,7 +85,7 @@
#' @export


plot.importance <- function(x, ..., top = 10, radar = TRUE,
plot.importance <- function(x, ..., top = 10, radar = TRUE, text_start_point = 0.5, text_size=3.5,
xmeasure = "sumCover", ymeasure = "sumGain"){

Feature <- sumGain <- sumCover <- meanGain <- meanCover <-
Expand All @@ -97,7 +99,7 @@ plot.importance <- function(x, ..., top = 10, radar = TRUE,
ggplot(data.frame(x[1:top, ]),
aes_string(x = xmeasure, y = ymeasure, label = "Feature")) +
geom_point() +
scale_size() + geom_label_repel() + theme_mi2()
scale_size() + geom_label_repel() + theme_drwhy()

}else{
import <- as.data.table(x[1:top, ])
Expand All @@ -108,25 +110,34 @@ plot.importance <- function(x, ..., top = 10, radar = TRUE,
meanCover = meanCover / max(import[, meanCover]),
mean5Gain = mean5Gain / max(import[, mean5Gain]),
frequency = frequency / max(import[, frequency]))]
data<-import[,Feature:= ifelse(nchar(import[,Feature])>20, gsub(":", ": :",import[,Feature]),Feature)]

import$Feature <- factor(import$Feature, levels = import$Feature[order(import$sumGain, decreasing = TRUE)])
data_to_plot <- melt(import, id = 1, measures = 2:6)

#angles and hjust of labels
numberOfBars=nrow(import)
angle= 90-360*(row(import)[,1]-0.5)/numberOfBars

import$hjust<-ifelse( angle < -90, 1, 0)
import$angle<-ifelse(angle < -90, angle+180, angle)

data_to_plot <- melt(import, id = c(1,8,9), measures = 2:6, value.factor = FALSE)
data<-data_to_plot[,.(hjust=mean(hjust),angle=mean(angle)), by=Feature]

ggplot(data.frame(data_to_plot),
aes(x = Feature, y = value, colour = variable, group = variable)) +
geom_line(size = 1.5) +
geom_point(size = 2.5) +
theme_mi2() +
theme_drwhy() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.text.x = element_text(angle=45),
legend.position = "bottom",
#axis.text.y = element_blank(),
panel.grid.major.y = element_line(colour = "gray68", linetype = "dashed", size = 0.4),
axis.line = element_blank(),
plot.margin = margin(40, 40, 40, 40)) +
labs(fill = "Measures") +
coord_radar()+
scale_x_discrete(labels = lapply(strwrap(import[,Feature], width = 10, simplify = FALSE), paste, collapse="\n"))
axis.text.x=element_blank(),) +
labs(fill = "Measures")+
coord_radar() +
geom_text(data=data, aes(x=Feature, y= rep(text_start_point,top), label=lapply(strwrap(data[,Feature], width = 10, simplify = FALSE), paste, collapse="\n"), hjust=hjust), color="#371ea3", fontface="bold",alpha=0.6, size=text_size, angle= data$angle, inherit.aes = FALSE )

}
}
6 changes: 3 additions & 3 deletions R/plot_interactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#'
#' @import data.table
#' @import ggplot2
#' @importFrom DALEX theme_mi2
#' @importFrom DALEX theme_drwhy
#'
#' @examples
#' library("EIX")
Expand Down Expand Up @@ -65,11 +65,11 @@ plot.interactions <- function(x, ...) {

ggplot(data.frame(x), aes(Child, Parent, sumGain)) +
geom_tile(aes(fill = breaks)) +
theme_mi2() +
theme_drwhy() +
theme(axis.text.x = element_text(hjust = 1, angle = 90),
axis.text.y = element_text(hjust = 1, angle = 0)) +
scale_fill_manual(name = "sumGain",
values = c("#ffffff", "#ccccff", "#7f7fff", "#3232ff"),
values = c("#f4f5ff", "#ccccff", "#7f7fff", "#3232ff"),
drop = FALSE,
breaks = levels(x$breaks),
labels = c("very low", "low", "medium", "high")) +
Expand Down
18 changes: 10 additions & 8 deletions R/plot_lollipop.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
#' and variables in the roots (horizontal label) will be displayed,
#' if "interactions" then labels for all interactions,
#' if "roots" then labels for all variables in the root.
#' @param threshold on the plot will occur only labels with Gain higher than `threshold` of the max Gain value in the model.
#' The lower threshold, the more labels on the plot. Range from 0 to 1. Default 0.1.
#' @param log TRUE/FALSE logarithmic scale on the plot. Default TRUE.
#' @param ... other parameters.
#'
#' @return a ggplot object
#'
#' @import data.table
#' @import ggplot2
#' @importFrom DALEX theme_mi2
#' @importFrom DALEX theme_drwhy
#' @importFrom ggrepel geom_label_repel
#' @importFrom ggrepel geom_text_repel
#'
#' @examples
#' options(warn = -1)
#' library("EIX")
#' library("Matrix")
#' sm <- sparse.model.matrix(left ~ . - 1, data = HR_data)
Expand All @@ -42,7 +45,7 @@
#'
#' @export

plot.lollipop<-function(x, ..., labels = "topAll", log = TRUE){
plot.lollipop<-function(x, ..., labels = "topAll", log = TRUE, threshold=0.1){

Tree <- Quality <- depth <- Feature <- NULL
nodes <- x[[1]]
Expand All @@ -57,24 +60,23 @@ plot.lollipop<-function(x, ..., labels = "topAll", log = TRUE){
switch(labels,
topAll = {
p + geom_text_repel(data = data.frame(nodes),
aes(label = ifelse((interaction == TRUE) & (Quality > 0.1 * (max(nodes[, Quality]))), Feature, '')),
aes(label = ifelse((interaction == TRUE) & (Quality > threshold * (max(nodes[, Quality]))), Feature, '')),
angle = 90, nudge_y = 0.05, direction = "x", vjust = 0, segment.size = 0.2) +
geom_label_repel(data = data.frame(roots),
aes(label = ifelse(Quality > 0.1 * (max(nodes[, Quality] )), Feature, '')))
aes(label = ifelse(Quality > threshold * (max(nodes[, Quality] )), Feature, '')))
},
interactions = {
p + geom_text_repel(data = data.frame(nodes),
aes(label = ifelse(( interaction == TRUE ), Feature, '')),
aes(label = ifelse(( interaction == TRUE ) & (Quality > threshold * (max(nodes[, Quality]))), Feature, '')),
angle = 90, nudge_y = 0.05, direction = "x", vjust = 0, segment.size = 0.2 )
},
roots = {
p + geom_label_repel(data = data.frame(roots), aes(label = Feature))
p + geom_label_repel(data = data.frame(roots), aes(label = ifelse(Quality > threshold * (max(nodes[, Quality])), Feature, '')))
})}

p <- p + theme_mi2() + ylab("Gain") +
p <- p + theme_drwhy()+ ylab("Gain") +
scale_shape_discrete("Depth") +
scale_colour_discrete("Depth") + if (log) {scale_x_continuous(trans = 'log10')}

p
}

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A set of tools to explain XGBoost and LightGBM models.

Install from GitHub
```{r}
devtools::install_github("ekarbowiak/EIX")
devtools::install_github("ModelOriented/EIX")
```

## Cheatsheets
Expand Down
Binary file modified cheatsheets/EIX.pdf
Binary file not shown.
Binary file modified cheatsheets/EIX.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 cheatsheets/EIX.pptx
Binary file not shown.
21 changes: 7 additions & 14 deletions docs/articles/EIX.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/articles/EIX_files/figure-html/unnamed-chunk-10-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 docs/articles/EIX_files/figure-html/unnamed-chunk-6-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 docs/articles/EIX_files/figure-html/unnamed-chunk-7-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 docs/articles/EIX_files/figure-html/unnamed-chunk-8-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 docs/articles/EIX_files/figure-html/unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/reference/importance-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 docs/reference/importance-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 docs/reference/importance-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 docs/reference/importance-4.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 docs/reference/importance-5.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 docs/reference/importance-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/reference/interactions-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 docs/reference/interactions-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 docs/reference/interactions-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 docs/reference/interactions-4.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 docs/reference/lollipop-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 docs/reference/lollipop-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8929887

Please sign in to comment.