New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix issue #91 based on discussion in the comments. #140
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0071022
fix issue #91 based on discussion in the comments.
MrAE bc63d31
remove need for library(Matrix) and update function parameteres.
MrAE 9e411b1
update test-FeatureImportance
MrAE 4c8cfb5
update Feature Importance to be more readable [@Ben].
MrAE cd191c0
Merge branch 'staging' into jlp-issue91
MrAE File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' Compute Feature Importance of a single RerF tree | ||
#' | ||
#' Computes feature importance of every unique feature used to make a split in a single tree. | ||
#' | ||
#' @param tree a single tree from a trained RerF model with argument store.impurity = TRUE. | ||
#' @param unique.projections a list of all of the unique split projections used in the RerF model. | ||
#' | ||
#' @return feature.imp | ||
#' | ||
#' @examples | ||
#' library(rerf) | ||
#' X <- iris[, -5] | ||
#' Y <- iris[[5]] | ||
#' store.impurity <- TRUE | ||
#' FUN <- RandMatBinary | ||
#' forest <- RerF(X, Y, FUN = FUN, num.cores = 1L, store.impurity = store.impurity) | ||
#' FeatureImportance(forest, num.cores = 1L) | ||
|
||
RunFeatureImportanceBinary <- function(tree, unique.projections) { | ||
|
||
## compute the 180 rotations of the projections | ||
neg.up <- lapply(unique.projections, flipWeights) | ||
num.proj <- length(unique.projections) | ||
|
||
feature.imp <- double(num.proj) | ||
for (nd in tree$treeMap[tree$treeMap > 0L]) { | ||
index.low <- tree$matAindex[nd] + 1L | ||
index.high <- tree$matAindex[nd + 1L] | ||
projection.idx <- | ||
which(unique.projections %in% | ||
list(tree$matAstore[index.low:index.high]) | | ||
neg.up %in% list(tree$matAstore[index.low:index.high])) | ||
feature.imp[projection.idx] <- | ||
feature.imp[projection.idx] + tree$delta.impurity[nd] | ||
} | ||
return(feature.imp) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Tabulate the unique feature combinations used in a single RerF tree | ||
#' | ||
#' Computes feature importance of every unique feature used to make a split in a single tree. | ||
#' | ||
#' @param tree a single tree from a trained RerF model with argument store.impurity = TRUE. | ||
#' @param unique.projections a list of all of the unique split projections used in the RerF model. | ||
#' | ||
#' @return feature.counts | ||
#' | ||
#' @examples | ||
#' @examples | ||
#' library(rerf) | ||
#' X <- iris[, -5] | ||
#' Y <- iris[[5]] | ||
#' store.impurity <- TRUE | ||
#' FUN <- RandMatContinuous | ||
#' forest <- RerF(X, Y, FUN = FUN, num.cores = 1L, store.impurity = store.impurity) | ||
#' FeatureImportance(forest, num.cores = 1L, featureCounts = TRUE) | ||
|
||
RunFeatureImportanceCounts <- function(tree, unique.projections) { | ||
num.proj <- length(unique.projections) | ||
feature.counts <- double(num.proj) | ||
|
||
for (nd in tree$treeMap[tree$treeMap > 0L]) { | ||
index.low <- tree$matAindex[nd] + 1L | ||
index.high <- tree$matAindex[nd + 1L] | ||
projection.idx <- | ||
which(unique.projections %in% | ||
lapply(list(tree$matAstore[index.low:index.high]), getFeatures)) | ||
feature.counts[projection.idx] <- | ||
feature.counts[projection.idx] + 1 | ||
} | ||
return(feature.counts) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#' Extract feature indicies from the sparse projection vector. | ||
#' | ||
#' A helper function to extract the feature indices from the projection | ||
#' vector stored in a tree object. | ||
#' | ||
#' @param x a list of unique.projections from the intermediate steps of | ||
#' the FeatureImportance function. | ||
#' | ||
#' @return list of unique feature combinations | ||
#' | ||
|
||
getFeatures <- function(x) { | ||
s <- seq(1, length(x), by = 2) | ||
return(x[s]) | ||
} | ||
|
||
#' Extract feature weights from the sparse projection vector. | ||
#' | ||
#' A helper function to extract the feature weights from the projection | ||
#' vector stored in a tree object. | ||
#' | ||
#' @param x a list of unique.projections from the intermediate steps of | ||
#' the FeatureImportance function. | ||
#' | ||
#' @return list of unique feature weights | ||
#' | ||
|
||
getWeights <- function(x) { | ||
s <- seq(2, length(x), by = 2) | ||
return(x[s]) | ||
} | ||
|
||
#' Change the sign of the weights | ||
#' | ||
#' A helper function to extract the feature weights from the projection | ||
#' vector stored in a tree object. Used in | ||
#' \code{RunFeatureImportanceBinary}. | ||
#' | ||
#' @param x a list of unique.projections from the intermediate steps of | ||
#' the FeatureImportance function. | ||
#' | ||
#' @return x with sign of weights flipped. | ||
#' | ||
|
||
|
||
flipWeights <- function(x) { | ||
s <- seq(2, length(x), by = 2) | ||
x[s] <- -x[s] | ||
return(x) | ||
} | ||
|
||
|
||
#' Remove unique projections that are equivalent due to a rotation of 180 | ||
#' degrees. | ||
#' | ||
#' This function finds the projections that are equivalent via a 180 | ||
#' degree rotation and removes the duplicates. | ||
#' | ||
#' @param p the number of features in the original data. This can be | ||
#' obtained from a forest object via \code{forest$params$paramList$p}. | ||
#' @param unique.projections a list of projections from intermediate | ||
#' steps of the \code{\link{FeatureImportance}} function. | ||
#' | ||
#' @return unique.projections a list which is a subset of the input. | ||
#' | ||
#' @seealso \code{\link{FeatureImportance}} | ||
#' | ||
#' | ||
|
||
uniqueByEquivalenceClass <- function(p, unique.projections) { | ||
|
||
## the matrix of weights (w) | ||
w <- matrix(0, | ||
ncol = p, | ||
nrow = length(unique.projections) | ||
) | ||
|
||
for (i in 1:length(unique.projections)) { | ||
for (j in seq(1, length(unique.projections[[i]]), by = 2)) { | ||
w[i, unique.projections[[i]][j]] <- unique.projections[[i]][j + 1] | ||
} | ||
} | ||
|
||
out <- vector("list", 1 / 2 * (length(unique.projections) - 1) * | ||
length(unique.projections)) | ||
k <- 1 | ||
for (i in 1:nrow(w)) { | ||
for (j in i:nrow(w)) { | ||
if (all(w[i, ] == -w[j, ])) { | ||
out[[k]] <- c(i, j) | ||
k <- k + 1 | ||
} | ||
} | ||
} | ||
|
||
ind <- !sapply(out, is.null) | ||
out <- Reduce(rbind, out) | ||
|
||
unique.projections[out[, 2]] <- NULL | ||
|
||
return(unique.projections) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a style thing, but this piece of code would be far easier to read and maintain if we don't deal with indices and reassigning stuff and just append lists together.
pseudo code: