refactoring of importance function
This commit is contained in:
parent
d532f04394
commit
3d0bbae2c2
@ -6,7 +6,6 @@
|
|||||||
#' @importFrom data.table data.table
|
#' @importFrom data.table data.table
|
||||||
#' @importFrom magrittr %>%
|
#' @importFrom magrittr %>%
|
||||||
#' @importFrom data.table :=
|
#' @importFrom data.table :=
|
||||||
#' @importFrom stringr str_extract
|
|
||||||
#' @param feature_names names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be \code{NULL}.
|
#' @param feature_names names of each feature as a character vector. Can be extracted from a sparse matrix (see example). If model dump already contains feature names, this argument should be \code{NULL}.
|
||||||
#' @param filename_dump the path to the text file storing the model. Model dump must include the gain per feature and per tree (\code{with.stats = T} in function \code{xgb.dump}).
|
#' @param filename_dump the path to the text file storing the model. Model dump must include the gain per feature and per tree (\code{with.stats = T} in function \code{xgb.dump}).
|
||||||
#'
|
#'
|
||||||
@ -21,7 +20,8 @@
|
|||||||
#' There are 3 columns :
|
#' There are 3 columns :
|
||||||
#' \itemize{
|
#' \itemize{
|
||||||
#' \item \code{Features} name of the features as provided in \code{feature_names} or already present in the model dump.
|
#' \item \code{Features} name of the features as provided in \code{feature_names} or already present in the model dump.
|
||||||
#' \item \code{Gain} contribution of each feature to the model. For boosted tree model, each gain of each feature of each tree is taken into account, then average per feature to give a vision of the entire model. Highest percentage means most important feature regarding the \code{label} used for the training.
|
#' \item \code{Gain} contribution of each feature to the model. For boosted tree model, each gain of each feature of each tree is taken into account, then average per feature to give a vision of the entire model. Highest percentage means important feature to predict the \code{label} used for the training ;
|
||||||
|
#' \item \code{Cover} metric of the number of observation related to this feature (only available for tree models) ;
|
||||||
#' \item \code{Weight} percentage representing the relative number of times a feature have been taken into trees. \code{Gain} should be prefered to search the most important feature. For boosted linear model, this column has no meaning.
|
#' \item \code{Weight} percentage representing the relative number of times a feature have been taken into trees. \code{Gain} should be prefered to search the most important feature. For boosted linear model, this column has no meaning.
|
||||||
#' }
|
#' }
|
||||||
#'
|
#'
|
||||||
@ -59,21 +59,10 @@ xgb.importance <- function(feature_names = NULL, filename_dump = NULL){
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
treeDump <- function(feature_names, text){
|
treeDump <- function(feature_names, text){
|
||||||
featureVec <- c()
|
result <- xgb.model.dt.tree(feature_names = feature_names, text = text)[Feature!="Leaf",][,.(sum(Quality), sum(Cover), .N),by = Feature][,V1:=V1/sum(V1)][,V2:=V2/sum(V2)][,N:=N/sum(N)][order(-rank(V1))]
|
||||||
gainVec <- c()
|
setnames(result, c("Feature", "Gain", "Cover", "Frequence"))
|
||||||
for(line in text){
|
result
|
||||||
p <- str_extract(line, "\\[f.*<")
|
|
||||||
if (!is.na(p)) {
|
|
||||||
featureVec <- substr(p, 3, nchar(p)-1) %>% c(featureVec)
|
|
||||||
gainVec <- str_extract(line, "gain.*,") %>% substr(x = ., 6, nchar(.)-1) %>% as.numeric %>% c(gainVec)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!is.null(feature_names)) {
|
|
||||||
featureVec %<>% as.numeric %>% {c =.+1; feature_names[c]} #+1 because in R indexing start with 1 instead of 0.
|
|
||||||
}
|
|
||||||
#1. Reduce, 2. %, 3. reorder - bigger top, 4. remove temp col
|
|
||||||
data.table(Feature = featureVec, Weight = gainVec)[,list(sum(Weight), .N), by = Feature][, Gain:= V1/sum(V1)][,Weight:= N/sum(N)][order(-rank(Gain))][,-c(2,3), with = F]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
linearDump <- function(feature_names, text){
|
linearDump <- function(feature_names, text){
|
||||||
|
|||||||
@ -51,19 +51,29 @@
|
|||||||
#' xgb.model.dt.tree(agaricus.train$data@@Dimnames[[2]], 'xgb.model.dump')
|
#' xgb.model.dt.tree(agaricus.train$data@@Dimnames[[2]], 'xgb.model.dump')
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
xgb.model.dt.tree <- function(feature_names = NULL, filename_dump = NULL, n_first_tree = NULL){
|
xgb.model.dt.tree <- function(feature_names = NULL, filename_dump = NULL, text = NULL, n_first_tree = NULL){
|
||||||
|
|
||||||
if (!class(feature_names) %in% c("character", "NULL")) {
|
if (!class(feature_names) %in% c("character", "NULL")) {
|
||||||
stop("feature_names: Has to be a vector of character or NULL if the model dump already contains feature name. Look at this function documentation to see where to get feature names.")
|
stop("feature_names: Has to be a vector of character or NULL if the model dump already contains feature name. Look at this function documentation to see where to get feature names.")
|
||||||
}
|
}
|
||||||
if (class(filename_dump) != "character" || !file.exists(filename_dump)) {
|
if (!class(filename_dump) %in% c("character", "NULL")) {
|
||||||
stop("filename_dump: Has to be a path to the model dump file.")
|
stop("filename_dump: Has to be a character vector representing the path to the model dump file.")
|
||||||
|
} else if (class(filename_dump) == "character" && !file.exists(filename_dump)) {
|
||||||
|
stop("filename_dump: path to the model doesn't exist.")
|
||||||
|
} else if(is.null(filename_dump) & is.null(text)){
|
||||||
|
stop("filename_dump: no path and no string version of the model dump have been provided.")
|
||||||
|
}
|
||||||
|
if (!class(text) %in% c("character", "NULL")) {
|
||||||
|
stop("text: Has to be a vector of character or NULL if a path to the model dump has already been provided.")
|
||||||
}
|
}
|
||||||
if (!class(n_first_tree) %in% c("numeric", "NULL") | length(n_first_tree) > 1) {
|
if (!class(n_first_tree) %in% c("numeric", "NULL") | length(n_first_tree) > 1) {
|
||||||
stop("n_first_tree: Has to be a numeric vector of size 1.")
|
stop("n_first_tree: Has to be a numeric vector of size 1.")
|
||||||
}
|
}
|
||||||
|
|
||||||
text <- readLines(filename_dump) %>% str_trim(side = "both")
|
if(is.null(text)){
|
||||||
|
text <- readLines(filename_dump) %>% str_trim(side = "both")
|
||||||
|
}
|
||||||
|
|
||||||
position <- str_match(text, "booster") %>% is.na %>% not %>% which %>% c(length(text)+1)
|
position <- str_match(text, "booster") %>% is.na %>% not %>% which %>% c(length(text)+1)
|
||||||
|
|
||||||
extract <- function(x, pattern) str_extract(x, pattern) %>% str_split("=") %>% lapply(function(x) x[2] %>% as.numeric) %>% unlist
|
extract <- function(x, pattern) str_extract(x, pattern) %>% str_split("=") %>% lapply(function(x) x[2] %>% as.numeric) %>% unlist
|
||||||
|
|||||||
@ -27,7 +27,8 @@ Results are returned for both linear and tree models.
|
|||||||
There are 3 columns :
|
There are 3 columns :
|
||||||
\itemize{
|
\itemize{
|
||||||
\item \code{Features} name of the features as provided in \code{feature_names} or already present in the model dump.
|
\item \code{Features} name of the features as provided in \code{feature_names} or already present in the model dump.
|
||||||
\item \code{Gain} contribution of each feature to the model. For boosted tree model, each gain of each feature of each tree is taken into account, then average per feature to give a vision of the entire model. Highest percentage means most important feature regarding the \code{label} used for the training.
|
\item \code{Gain} contribution of each feature to the model. For boosted tree model, each gain of each feature of each tree is taken into account, then average per feature to give a vision of the entire model. Highest percentage means most important feature regarding the \code{label} used for the training ;
|
||||||
|
\item \code{Cover} metric of the number of observation related to this feature (only available for tree models) ;
|
||||||
\item \code{Weight} percentage representing the relative number of times a feature have been taken into trees. \code{Gain} should be prefered to search the most important feature. For boosted linear model, this column has no meaning.
|
\item \code{Weight} percentage representing the relative number of times a feature have been taken into trees. \code{Gain} should be prefered to search the most important feature. For boosted linear model, this column has no meaning.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
\alias{xgb.model.dt.tree}
|
\alias{xgb.model.dt.tree}
|
||||||
\title{Convert tree model dump to data.table}
|
\title{Convert tree model dump to data.table}
|
||||||
\usage{
|
\usage{
|
||||||
xgb.model.dt.tree(feature_names = NULL, filename_dump = NULL,
|
xgb.model.dt.tree(feature_names = NULL, filename_dump = NULL, text = NULL,
|
||||||
n_first_tree = NULL)
|
n_first_tree = NULL)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user