Boruta Algorithm | What is Boruta Algorithm | Boruta Algorithm in R

The Curse of Dimensionality: A Challenge for Machine Learning
As a data scientist, you‘ve likely heard of the "curse of dimensionality." But what exactly does it mean, and why is it such a problem in machine learning? In essence, the curse of dimensionality refers to the phenomenon where increasing the number of features or dimensions in your data can actually lead to worse model performance, rather than better.

It seems counterintuitive at first. After all, shouldn‘t giving your model more information help it make better predictions? The issue is that not all features are created equal. Some are highly relevant to the target variable you‘re trying to predict, while others may be irrelevant or even misleading. As the number of features grows, the risk of including unhelpful or noisy dimensions increases.

The consequences can be severe. Models trained on high-dimensional data often suffer from overfitting, where they memorize noise and fail to generalize well to new data. Training times can also balloon as the model struggles to find signal amidst the sea of irrelevant features. In the worst cases, the curse of dimensionality can render successful modeling practically impossible.

Fortunately, there are techniques to combat this curse. Chief among them is feature selection: identifying and keeping only the most informative features while discarding the rest. In this article, we‘ll dive deep into one particularly powerful feature selection algorithm called Boruta, and walk through exactly how to implement it in R on your own machine learning projects.

What is Boruta?
Boruta is a feature selection algorithm designed to work with random forest models. Originally proposed by Miron B. Kursa and colleagues in 2010, the name comes from a mythical Slavic forest spirit said to protect the woods.

The key idea behind Boruta is to compare the importance of the real features in the data against "shadow features," which are shuffled copies of the real features. Over multiple iterations, the algorithm checks whether each real feature has a higher importance score than the best of the shadow features. Features that are consistently more important are deemed relevant and kept, while the rest are progressively eliminated.

Because it uses the random forest importance measure, Boruta is particularly adept at handling correlations and interactions between features. It‘s a wrapper method, meaning it uses the model itself to evaluate feature subsets, as opposed to filter methods which rely on simpler statistical tests independent of the model. And by comparing to shadowed copies, Boruta provides a clear, statistically-grounded threshold for deciding which features matter and which don‘t.

How Boruta Works: A Step-by-Step Walkthrough
Let‘s make the Boruta algorithm more concrete with a step-by-step overview of how it works under the hood:

  1. Expand the feature set by creating shuffled copies of all features. These are called shadow features.
  2. Train a random forest model on the combined dataset containing both original and shadow features.
  3. Calculate the importance measure (mean decrease accuracy or mean decrease impurity) for each feature.
  4. For each real feature, count how many times its importance is higher than the max importance among shadow features (the hit count). Also count how many times it‘s lower (the miss count).
  5. Discard any features with a hit count significantly lower than expected by chance. Typically, this uses a binomial test with a significance level of p <= 0.01 after Bonferroni correction.
  6. Repeat steps 2-5 until one of the following stopping criteria is met:
  • All original features have been confirmed or rejected.
  • A max number of iterations have been performed (specified by the maxRuns parameter).
  • An importance source has been exhausted, i.e. none of the shadow features are deemed important (the RandomFeaturePlus heuristic, enabled by default).

By the end, we‘re left with a subset of confirmed important features, rejected unimportant ones, and possibly a few that remain tentative if the algorithm didn‘t confidently decide either way. The confirmed features represent a powerful, noise-reduced set of predictors we can use to train a more accurate and efficient model.

Implementing Boruta Feature Selection in R
Theory is important, but nothing beats seeing a technique in action. Let‘s walk through a complete example of using Boruta to reduce the dimensionality of a real dataset in R.

We‘ll be working with the famous UCI Bank Marketing dataset, which contains information about direct marketing contacts by a Portuguese bank. The goal is to predict whether the client will subscribe to a term deposit or not. With 17 features, this dataset isn‘t huge, but it provides a clear case where focusing on the most relevant dimensions could improve our modeling results.

First, let‘s load the required packages and the data itself:

library(Boruta)
library(caret)
library(randomForest)

data <- read.csv("bank-full.csv", stringsAsFactors = TRUE)
dim(data)  # [1] 45211    17

Next, we run the Boruta feature selection procedure. We specify the dependent variable (y) and give the algorithm a maximum of 50 iterations to decide the final feature set:

set.seed(123)  # for reproducibility
boruta_output <- Boruta(y ~ ., data = data, doTrace = 2, maxRuns = 50)
print(boruta_output)

The printout shows us the algorithm‘s progress over the 50 iterations. We can visualize the results using the built-in plot method:

plot(boruta_output, xlab = "", xaxt = "n")
lz <- lapply(1:ncol(boruta_output$ImpHistory), function(i) 
  boruta_output$ImpHistory[is.finite(boruta_output$ImpHistory[, i]), i])
names(lz) <- colnames(boruta_output$ImpHistory)
Labels <- sort(sapply(lz, median))
axis(side = 1, las = 2, labels = names(Labels), 
  at = 1:ncol(boruta_output$ImpHistory), cex.axis = 0.7)

Boruta output plot

In this plot, the blue boxplots represent the distribution of importance scores for each feature across all iterations. The red and green boxplots show the minimum and maximum importance of the shadow features.

We can see that several features, like employment variation rate, clearly score higher than the shadow features and are thus confirmed important. Others, like the number of employees, consistently score lower and are rejected. A few, like the consumer price index, remain tentative.

To access the final selection, we use the getSelectedAttributes() function:

final_features <- getSelectedAttributes(boruta_output, withTentative = FALSE)
print(final_features)
# [1] "duration"     "campaign"     "pdays"        "previous"     "euribor3m"   
# [6] "nr.employed"

This gives us the 6 confirmed important features out of the original 17. Now let‘s see whether a model trained on this reduced feature set performs better than using all features:

# Split into train and test sets
trainIndex <- createDataPartition(data$y, p = 0.7, list = FALSE, times = 1)
train_data <- data[trainIndex, ]
test_data <- data[-trainIndex, ]

# Train models and evaluate performance
rf_all <- randomForest(y ~ ., data = train_data)
rf_reduced <- randomForest(y ~ ., data = train_data[, c(final_features, "y")])

all_preds <- predict(rf_all, newdata = test_data)
reduced_preds <- predict(rf_reduced, newdata = test_data)

confusionMatrix(all_preds, test_data$y)$overall["Accuracy"]
#  Accuracy 
# 0.9014431

confusionMatrix(reduced_preds, test_data$y)$overall["Accuracy"] 
#  Accuracy 
# 0.9036678

The results speak for themselves. The model using only the 6 Boruta-selected features achieves slightly higher accuracy (90.37%) compared to the model using all 17 features (90.14%). We‘ve improved performance while drastically reducing complexity, computation, and the risk of overfitting. The curse of dimensionality has been lifted!

Pros and Cons of Boruta
As we‘ve seen, Boruta is a powerful tool for feature selection, especially when dealing with high-dimensional data. Some of its key strengths include:

  • Robustness to feature interactions and correlations, thanks to the use of random forests
  • Automatic threshold for confirming important features based on statistical testing
  • Easy to implement and interpret, with solid documentation and visualization options in R

However, Boruta is not without limitations. Some potential drawbacks to keep in mind:

  • It can be computationally expensive, especially on very high-dimensional data, since it involves repeatedly training random forests
  • The final feature set may vary slightly across runs due to the random nature of the shadow feature generation
  • It‘s a heuristic method, and thus doesn‘t guarantee finding the absolutely optimal feature subset

Despite these caveats, Boruta remains a go-to method for many data scientists when faced with the curse of dimensionality. Its combination of robustness, interpretability, and ease-of-use make it a valuable addition to any machine learning practitioner‘s toolkit.

Conclusion
Wrapping up, we‘ve covered a lot of ground in our deep dive into Boruta feature selection with R. We started by understanding the curse of dimensionality and how extraneous features can cripple machine learning models. We then introduced Boruta as a solution, walking through its key concepts and inner workings.

Most importantly, we saw Boruta in action on a real dataset, from implementation to interpretation to measuring its impact on model performance. The substantial accuracy gain from using only Boruta‘s confirmed features perfectly illustrated the power of effective dimensionality reduction.

Of course, Boruta is just one of many feature selection techniques out there. Others, like recursive feature elimination or genetic algorithms, may be better suited to certain problems. And in some cases, dimensionality reduction via feature extraction methods like PCA can be a useful alternative approach.

Nonetheless, when it comes to balancing simplicity and effectiveness, Boruta is hard to beat. Especially for R users, the ease of implementation with the Boruta package makes it a compelling choice. By focusing your models on the features that truly matter, Boruta can help you conquer the curse of dimensionality and unlock the full potential of your data. Happy feature selecting!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts