"...git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "f77cfe8328111fbcdeb3ad05527da84b73d27de0"
Commit af6a33e7 authored by James Lamb's avatar James Lamb Committed by Laurae
Browse files

[R-package] added missing importFroms in NAMESPACE (#1627)

parent 5bb5f1b9
...@@ -38,9 +38,11 @@ export(slice) ...@@ -38,9 +38,11 @@ export(slice)
import(methods) import(methods)
importFrom(R6,R6Class) importFrom(R6,R6Class)
importFrom(data.table,":=") importFrom(data.table,":=")
importFrom(data.table,as.data.table)
importFrom(data.table,data.table) importFrom(data.table,data.table)
importFrom(data.table,rbindlist) importFrom(data.table,rbindlist)
importFrom(data.table,set) importFrom(data.table,set)
importFrom(data.table,setnames)
importFrom(graphics,barplot) importFrom(graphics,barplot)
importFrom(graphics,par) importFrom(graphics,par)
importFrom(jsonlite,fromJSON) importFrom(jsonlite,fromJSON)
...@@ -49,4 +51,5 @@ importFrom(magrittr,"%T>%") ...@@ -49,4 +51,5 @@ importFrom(magrittr,"%T>%")
importFrom(magrittr,extract) importFrom(magrittr,extract)
importFrom(magrittr,inset) importFrom(magrittr,inset)
importFrom(methods,is) importFrom(methods,is)
importFrom(stats,quantile)
useDynLib(lib_lightgbm , .registration = TRUE) useDynLib(lib_lightgbm , .registration = TRUE)
...@@ -373,6 +373,7 @@ generate.cv.folds <- function(nfold, nrows, stratified, label, group, params) { ...@@ -373,6 +373,7 @@ generate.cv.folds <- function(nfold, nrows, stratified, label, group, params) {
# Creates CV folds stratified by the values of y. # Creates CV folds stratified by the values of y.
# It was borrowed from caret::lgb.stratified.folds and simplified # It was borrowed from caret::lgb.stratified.folds and simplified
# by always returning an unnamed list of fold indices. # by always returning an unnamed list of fold indices.
#' @importFrom stats quantile
lgb.stratified.folds <- function(y, k = 10) { lgb.stratified.folds <- function(y, k = 10) {
## Group the numeric data based on their magnitudes ## Group the numeric data based on their magnitudes
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#' tree_imp1 <- lgb.importance(model, percentage = TRUE) #' tree_imp1 <- lgb.importance(model, percentage = TRUE)
#' tree_imp2 <- lgb.importance(model, percentage = FALSE) #' tree_imp2 <- lgb.importance(model, percentage = FALSE)
#' #'
#' @importFrom magrittr %>% %T>% #' @importFrom magrittr %>% %T>% extract
#' @importFrom data.table := #' @importFrom data.table :=
#' @export #' @export
lgb.importance <- function(model, percentage = TRUE) { lgb.importance <- function(model, percentage = TRUE) {
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#' #'
#' tree_interpretation <- lgb.interprete(model, test$data, 1:5) #' tree_interpretation <- lgb.interprete(model, test$data, 1:5)
#' #'
#' @importFrom data.table as.data.table
#' @importFrom magrittr %>% %T>% #' @importFrom magrittr %>% %T>%
#' @export #' @export
lgb.interprete <- function(model, lgb.interprete <- function(model,
...@@ -76,6 +77,7 @@ lgb.interprete <- function(model, ...@@ -76,6 +77,7 @@ lgb.interprete <- function(model,
} }
#' @importFrom data.table data.table
single.tree.interprete <- function(tree_dt, single.tree.interprete <- function(tree_dt,
tree_id, tree_id,
leaf_id) { leaf_id) {
...@@ -119,6 +121,8 @@ single.tree.interprete <- function(tree_dt, ...@@ -119,6 +121,8 @@ single.tree.interprete <- function(tree_dt,
} }
#' @importFrom data.table rbindlist
#' @importFrom magrittr %>% extract
multiple.tree.interprete <- function(tree_dt, multiple.tree.interprete <- function(tree_dt,
tree_index, tree_index,
leaf_index) { leaf_index) {
...@@ -134,6 +138,7 @@ multiple.tree.interprete <- function(tree_dt, ...@@ -134,6 +138,7 @@ multiple.tree.interprete <- function(tree_dt,
} }
#' @importFrom data.table set setnames
single.row.interprete <- function(tree_dt, num_class, tree_index_mat, leaf_index_mat) { single.row.interprete <- function(tree_dt, num_class, tree_index_mat, leaf_index_mat) {
# Prepare vector list # Prepare vector list
......
#' Plot feature contribution as a bar graph #' Plot feature contribution as a bar graph
#' #'
#' Plot previously calculated feature contribution as a bar graph. #' Plot previously calculated feature contribution as a bar graph.
#' #'
#' @param tree_interpretation_dt a \code{data.table} returned by \code{\link{lgb.interprete}}. #' @param tree_interpretation_dt a \code{data.table} returned by \code{\link{lgb.interprete}}.
#' @param top_n maximal number of top features to include into the plot. #' @param top_n maximal number of top features to include into the plot.
#' @param cols the column numbers of layout, will be used only for multiclass classification feature contribution. #' @param cols the column numbers of layout, will be used only for multiclass classification feature contribution.
#' @param left_margin (base R barplot) allows to adjust the left margin size to fit feature names. #' @param left_margin (base R barplot) allows to adjust the left margin size to fit feature names.
#' @param cex (base R barplot) passed as \code{cex.names} parameter to \code{barplot}. #' @param cex (base R barplot) passed as \code{cex.names} parameter to \code{barplot}.
#' #'
#' @details #' @details
#' The graph represents each feature as a horizontal bar of length proportional to the defined contribution of a feature. #' The graph represents each feature as a horizontal bar of length proportional to the defined contribution of a feature.
#' Features are shown ranked in a decreasing contribution order. #' Features are shown ranked in a decreasing contribution order.
#' #'
#' @return #' @return
#' The \code{lgb.plot.interpretation} function creates a \code{barplot}. #' The \code{lgb.plot.interpretation} function creates a \code{barplot}.
#' #'
#' @examples #' @examples
#' library(lightgbm) #' library(lightgbm)
#' Sigmoid <- function(x) {1 / (1 + exp(-x))} #' Sigmoid <- function(x) {1 / (1 + exp(-x))}
...@@ -25,54 +25,56 @@ ...@@ -25,54 +25,56 @@
#' setinfo(dtrain, "init_score", rep(Logit(mean(train$label)), length(train$label))) #' setinfo(dtrain, "init_score", rep(Logit(mean(train$label)), length(train$label)))
#' data(agaricus.test, package = "lightgbm") #' data(agaricus.test, package = "lightgbm")
#' test <- agaricus.test #' test <- agaricus.test
#' #'
#' params <- list(objective = "binary", #' params <- list(objective = "binary",
#' learning_rate = 0.01, num_leaves = 63, max_depth = -1, #' learning_rate = 0.01, num_leaves = 63, max_depth = -1,
#' min_data_in_leaf = 1, min_sum_hessian_in_leaf = 1) #' min_data_in_leaf = 1, min_sum_hessian_in_leaf = 1)
#' model <- lgb.train(params, dtrain, 20) #' model <- lgb.train(params, dtrain, 20)
#' model <- lgb.train(params, dtrain, 20) #' model <- lgb.train(params, dtrain, 20)
#' #'
#' tree_interpretation <- lgb.interprete(model, test$data, 1:5) #' tree_interpretation <- lgb.interprete(model, test$data, 1:5)
#' lgb.plot.interpretation(tree_interpretation[[1]], top_n = 10) #' lgb.plot.interpretation(tree_interpretation[[1]], top_n = 10)
#' @importFrom data.table setnames
#' @importFrom graphics barplot par #' @importFrom graphics barplot par
#' @importFrom magrittr inset
#' @export #' @export
lgb.plot.interpretation <- function(tree_interpretation_dt, lgb.plot.interpretation <- function(tree_interpretation_dt,
top_n = 10, top_n = 10,
cols = 1, cols = 1,
left_margin = 10, left_margin = 10,
cex = NULL) { cex = NULL) {
# Get number of columns # Get number of columns
num_class <- ncol(tree_interpretation_dt) - 1 num_class <- ncol(tree_interpretation_dt) - 1
# Refresh plot # Refresh plot
op <- graphics::par(no.readonly = TRUE) op <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(op)) on.exit(graphics::par(op))
# Do some magic plotting # Do some magic plotting
graphics::par(mar = op$mar %>% magrittr::inset(., 1:3, c(3, left_margin, 2))) graphics::par(mar = op$mar %>% magrittr::inset(., 1:3, c(3, left_margin, 2)))
# Check for number of classes # Check for number of classes
if (num_class == 1) { if (num_class == 1) {
# Only one class, plot straight away # Only one class, plot straight away
multiple.tree.plot.interpretation(tree_interpretation_dt, multiple.tree.plot.interpretation(tree_interpretation_dt,
top_n = top_n, top_n = top_n,
title = NULL, title = NULL,
cex = cex) cex = cex)
} else { } else {
# More than one class, shape data first # More than one class, shape data first
layout_mat <- matrix(seq.int(to = cols * ceiling(num_class / cols)), layout_mat <- matrix(seq.int(to = cols * ceiling(num_class / cols)),
ncol = cols, nrow = ceiling(num_class / cols)) ncol = cols, nrow = ceiling(num_class / cols))
# Shape output # Shape output
graphics::par(mfcol = c(nrow(layout_mat), ncol(layout_mat))) graphics::par(mfcol = c(nrow(layout_mat), ncol(layout_mat)))
# Loop throughout all classes # Loop throughout all classes
for (i in seq_len(num_class)) { for (i in seq_len(num_class)) {
# Prepare interpretation, perform T, get the names, and plot straight away # Prepare interpretation, perform T, get the names, and plot straight away
tree_interpretation_dt[, c(1, i + 1), with = FALSE] %T>% tree_interpretation_dt[, c(1, i + 1), with = FALSE] %T>%
data.table::setnames(., old = names(.), new = c("Feature", "Contribution")) %>% data.table::setnames(., old = names(.), new = c("Feature", "Contribution")) %>%
...@@ -80,24 +82,25 @@ lgb.plot.interpretation <- function(tree_interpretation_dt, ...@@ -80,24 +82,25 @@ lgb.plot.interpretation <- function(tree_interpretation_dt,
top_n = top_n, top_n = top_n,
title = paste("Class", i - 1), title = paste("Class", i - 1),
cex = cex) cex = cex)
} }
} }
} }
#' @importFrom graphics barplot
multiple.tree.plot.interpretation <- function(tree_interpretation, multiple.tree.plot.interpretation <- function(tree_interpretation,
top_n, top_n,
title, title,
cex) { cex) {
# Parse tree # Parse tree
tree_interpretation <- tree_interpretation[order(abs(Contribution), decreasing = TRUE),][seq_len(min(top_n, .N)),] tree_interpretation <- tree_interpretation[order(abs(Contribution), decreasing = TRUE),][seq_len(min(top_n, .N)),]
# Attempt to setup a correct cex # Attempt to setup a correct cex
if (is.null(cex)) { if (is.null(cex)) {
cex <- 2.5 / log2(1 + top_n) cex <- 2.5 / log2(1 + top_n)
} }
# Do plot # Do plot
tree_interpretation[.N:1, tree_interpretation[.N:1,
graphics::barplot( graphics::barplot(
...@@ -110,8 +113,8 @@ multiple.tree.plot.interpretation <- function(tree_interpretation, ...@@ -110,8 +113,8 @@ multiple.tree.plot.interpretation <- function(tree_interpretation,
cex.names = cex, cex.names = cex,
las = 1 las = 1
)] )]
# Return invisibly # Return invisibly
invisible(NULL) invisible(NULL)
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment