Lets implement this feature engineering pipeline on numerai data using jupyter notebook and other package since kxy is not availiable anymore. this article, we explain why effective feature selection matters, why popular approaches such as RFE, Boruta, and SHAP values aren't good enough, and we propose an information-theoretical solution with code.
Table of Contents
Why feature selection matters
Limits of RFE, Boruta, and Shapley values
An information-theoretical alternative, with code
One of the first lessons a machine learning engineer learns at the beginning of his career is that, when it comes to features, sometimes less is more.
The Limits of Using Too Many Features
While the temptation is high to generate as many features as possible and to let the model do the rest, training a model with redundant or uninformative features can be both ineffective and inefficient.
Non-informative features, simply put, are features that cannot contribute to improving the performance of a predictive model. As an illustration, whether a customer is left-handed or right-handed is likely non-informative about whether or/not he will churn a banking product.
Formally, non-informative features are features that are statistically independent from the target (unconditionally), and also statistically independent from the target conditional on any other feature(s) in the set of candidate features.
Redundant features on the other hand are features that, while they might be informative about the target, cannot contribute to improving the performance of a predictive model when used in conjunction with some other features in the set of candidates.
For instance, in problems where geolocation matters, the postcode can potentially be informative about the target. However, when GPS coordinates are included to the set of features, the postcode becomes redundant as it can be inferred from GPS coordinates.
Formally, redundant features are features for which there exists a subset of other features conditional on/given which they are stastically independent from the target.
Using non-informative or redundant features can cause several problems in the training phase of a machine learning project, as well as after a predictive model is deployed to production.
Before Production
Non-informative and redundant features make the training phase less effective (i.e. poorer performance) and less efficient (i.e. longer and costlier).
Increased risk of overfitting: By training a model with non-informative features, we run the risk that it gives a lot of importance to those non-informative features based on spurious patterns detected in the training dataset, which is a form of overfitting. By definition, a model shouldn't rely on non-informative features. Redundant features can also cause degeneracy while training certain models (e.g. linear regression), which could result in overfitting.
Assistant
Poorer performance: Even when overfitting does not occur, an optimizer might still give small but non-negligible importance to non-informative features. This would result in poorer performance compared to not including said non-informative features.
Costlier to train: When training a model, runtime, memory usage, and CPU usage grow with the number of features. Because they are dispensable, redundant features and non-informative features make model training unnecessarily longer and costlier.
Harder to explain: The presence of redundant features makes it harder to explain a model, as it will be harder to properly attribute feature importance to redundant features.
In Production
Using non-informative or redundant features in production increases maintenance costs.
Increased outage surface: Different features could be served in production by different pipelines (e.g. the raw inputs might come from different databases or tables, processing could be done by different processes, etc.). In such a case, the more features a deployed model uses, the more feature delivery system outages the model will be exposed to, which will eventually increase downtime as any feature outage will likely take down prediction capabilities.
Faster data drift: Data distributions drift over time, causing production models to be retrained. The higher the number of features, the quicker the drift will arise, and the more often production models will need to be retrained.
Review of Existing Feature Selection Approaches & Their Limitations
Pairwise Mutual Information
A popular feature selection approach consists of quantifying how informative each feature is about the target and retaining features that have a high degree of statistical dependency on the target. This is often done using measures of association such as Pearson correlation, Spearman rank correlation, or (pairwise) mutual information.
There are two major problems with this approach. First, a feature need not be statistically dependent on the target to be useful! Because this is a common misconception, let's take a concrete illustrative example to clear this out.
Imagine we want to pin down the location of a person or an object (e.g. by predicting GPS coordinates) using elements of an address (i.e. street name, house number, city, zip code, and country). Intuitively, we would expect house numbers to be statistically independent from GPS coordinates, as most house numbers are found on every street, in every city, and in every country. Simply knowing a house number can hardly tell us anything about the associated GPS coordinates (on average).
Clearly, without house numbers, we will not be able to predict GPS coordinates with an error smaller than a few hundred meters (typical street lengths). That said, even though house numbers are expected to be statistically independent from GPS coordinates, using house numbers, we will be able to reduce the error from a few hundred meters to a few tens of meters (typical house dimensions); this is very significant!
Without the street name, the house number is indeed useless. The house number becomes more useful when the street name is known. But even then, because some street names are very common (e.g. First Street, Second Street, Third Street, etc. in the US), we need either the city or the zip code to remove any ambiguity. Said differently, house numbers can be greatly useful, but only if we also know the street name and city or the zip code. Pairwise feature selection approaches are unable to select such features as house numbers, as they never consider interactions between features.
The second problem with pairwise approaches to feature selection is that they are inherently unable to detect and remove redundant features. To detect redundant features, an approach needs to consider more than one feature at a time.
Recursive Feature Elimination
Another popular feature selection algorithm is the Recursive Feature Elimination (RFE) algorithm. It proceeds as follows:
Train a classification or regression model.
Quantify the importance of each feature used to the trained model.
Remove the least important feature.
Repeat 1-3. until we are left with the desired number of features.
RFE does a good job at removing features that are not useful to a model. However, RFE is unable to detect redundant features, and it can be very slow.
Intuitively, if we duplicate the most important feature and retrain our model, both said feature and its duplicate will likely still be deemed very important by the newly trained model. Thus, the duplicated feature will be among the last ones to be considered for removal by RFE. More generally, RFE cannot remove feature a feature on the basis that it is redundant, which effective feature selection algorithms should be able to.
As for runtime and resources usage of RFE, they grow with the number of features to remove, not the number of features to keep, which is counter-intuitive. This is made worse by the fact that each elimination round requires fully re-training our model.
To take a concrete example, if we want to select 10 features out of thousands of candidates, we need to fully train our model thousands of times. It is bad enough to have to retrain our model thousands of times, but each time we also need to calculate feature importance scores.
While feature importance scores are a byproduct of model training for some classes of models (e.g. linear regression and decision trees), for many more classes of models they are very costly to evaluate, to the point of being impractical.
Boruta
Another fundamental limitation of RFE is that the choice of the number of features to keep is left to the user. The Boruta feature selection algorithm addresses this limitation by providing a statistical framework for testing whether a feature is non-informative.
The intuitive idea underpinning Boruta is that, by randomly shuffling rows of a feature column, we effectively destroy any potential association between the feature, the target, and other features, thereby rendering it useless.
By randomly shuffling all features, we generate an additional set of useless features referred to as 'shadow features', whose importance scores we may use as benchmarks to form a threshold above which an importance score can be deemed high enough (and below which it can be deemed typical of useless features). In the case of Boruta, the threshold is the highest importance score out of all shadow features.
In Boruta, a model is trained using a combination of real features and shadow features, and feature importance scores are calculated for real and shadow features. Any real feature whose importance score is higher than the highest importance score of shadow features is said to have triggered a 'hit'. Our expectation is that useful features will trigger hits, while useless won't.
Boruta models our lack of knowledge about whether a feature is useful or not using the (null) hypothesis H0
H0 that there is a 1/2 probability that the feature will trigger a hit. If we repeat the experiment kk times then, the number of hits hkhk of any real feature for which H0H0 holds (i.e. when we don't know whether the feature is useful or not) ought to follow the binomial distribution B(k,1/2)B(k,1/2), of which we can evaluate confidence bounds [hmk,hMk]
[hmk,hMk] at a level of our choosing (say 95%) analytically.
We conclude about the usefulness of a feature as follows:
hk>hMk
hk>hMk: we may reject the hypothesis H0H0 in favor of the alternative H1
H1 that the feature is useful.
hk<hmk
hk<hmk: we may reject the hypothesis H0H0 in favor of the alternative H2
H2 that the feature is useless.
hk∈[hmk,hMk]
hk∈[hmk,hMk]: we still do not know whether the feature is useful. We need to increase k
k to conclude.
Because models need to be trained with real and shadow features combined, an iteration of Boruta can be much more computationally demanding than an iteration of RFE. When the number of features is large, however, Boruta will typically need far fewer iterations to detect all useless features than RFE.
That said, like RFE, Boruta is unable to detect and eliminate redundant features, and for the same reasons.
RFE-SHAP and Boruta-SHAP
SHAP values offer a principled game-theoretic approach to feature importance scoring and, as such, can be combined with both RFE and Boruta.
In a nutshell, the SHAP value of a feature corresponding to a specific model prediction represents the contribution to the deviation of the specific decision made from the baseline decision, which can be attributed to the specific value of the said feature.
By taking the absolute value and averaging across all decisions made, we obtain a score that quantifies the contribution of each feature in driving model decisions away from the baseline decision (i.e. the best decision we can make without using any feature): this the SHAP feature importance score.
The appeal of SHAP values stems in large part from the fact that Shapley values, upon which they are based, satisfy a desirable uniqueness property. However, because SHAP values of identical features are the same, using RFE or Boruta with SHAP feature importance cannot detect and remove redundant features.
Feature Importance Based Approaches Are Fundamentally Flawed
More generally, in addition to the fact that they cannot eliminate redundant features, feature importance-based approaches to feature selection are fundamentally flawed.
A feature importance score (SHAP value included) quantifies the contribution or 'influence' of a feature towards generating model decisions or, said differently, the extent to which model decisions are due to the feature. However, what we really need, which a feature importance score does not and cannot do, is to quantify the contribution of a feature to the overall performance of a model.
Features with high importance scores are features that drive model decisions the most. Useful features on the other hand are features that account for model performance the most! For the two notions to coincide, the trained model has to be very accurate, an assumption rarely met but which RFE and Boruta take for granted.
Whenever a model is overfitted, chances are that the features with the highest feature importance scores are to blame, and as such should be removed. However, these are precisely the features that a feature importance-based feature selection algorithm such as RFE and Boruta will keep. Let's take a concrete example.
Consider the following toy regression problem.
In black is a fitted linear model relating input x
x to yy, and in blue is a fitted polynomial model, which can be regarded as a linear model relating features x,x2,x3,x4,...x,x2,x3,x4,... to y
y.
From the plot above, we can tell that the nonlinear model, which is obviously overfitted, relies heavily on at least one monomial term whose degree is higher than 1
Thus, we expect the corresponding feature to have a high importance score, no matter the scoring method used. Hence, by applying a feature importance-based selection algorithm to the nonlinear model, we will never be able to remove all nonlinear monomials, which would be the right thing to do in this example.
Had we estimated the contribution of each feature to model performance instead, we would have been able to remove all nonlinear monomials on the basis that they are responsible for overfitting.
Effective Information-Theoretic Feature Selection With KXY
To be effective, a feature selection algorithm should do two things right: 1) discard redundant features, and 2) keep features that contribute the most to model performance.
Algorithm
Building on our previous blog post, we propose the following algorithm:
Select as the first feature the feature f1
f1 that, when used by itself to predict the target yy, has the highest achievable performance a1
a1, as described in this blog.
For i
i ranging from 1 to the total number kk of candidate features, out of all (k−i)(k−i) features not yet selected, select as (i+1)(i+1)-th feature fi+1fi+1 the feature that, when used in conjunction with all ii previously selected features, yields the highest increase in the achievable performance (ai+1−ai)(ai+1−ai). Said differently, fi+1fi+1 is the feature that complements previously selected features (f1,…,fi)(f1,…,fi) the most to predict the target y
y.
Note that this algorithm is model-free. It only considers the potential effect a feature can have on model performance.
At each step i
i, any feature that is redundant relative to the i
i features already selected is the least likely to be selected as, by definition of it being redundant, it cannot increase the highest performance achievable.
Similarly, any feature that is non-informative cannot increase the highest performance achievable and, as such, is bound to be among the last features selected.
Formally, if we denote j
j the smallest index such that aj=akaj=ak then, if j<kj<k, all features {aj+1,…,ak}
{aj+1,…,ak} are either redundant or useless and, as such, can be safely discarded.
More generally, the sequence {(f1,a1),…,(fj,aj)}
{(f1,a1),…,(fj,aj)} allows us to strike the right balance between the number of features used and the potential performance they can bring about. Even if a feature isn't completely useless or redundant, the performance boost it may bring about can be so small that using the feature wouldn't be worth it.
Code Snippet
We use the UCI Bank Marketing dataset as an example.
But first, we generate some features. The code snippet below generates 103 features out of the 20 raw explanatory variables, and 1-hot encodes the target.
0. As a one-off, run 'pip install kxy', then 'kxy configure'
As we can see, the top-8 explanatory variables collectively contribute to an increase of the achievable classification accuracy above the baseline by 6%, while the remaining 90+ features can only generate a 1% accuracy boost.
Sure! Let's implement an information-theoretic feature selection pipeline for Numerai data using a Jupyter Notebook. Since the kxy package is no longer available, we'll utilize other powerful Python libraries such as pandas, numpy, and scikit-learn to achieve similar functionality.
This notebook will guide you through:
Understanding the Importance of Feature Selection
Loading and Preparing Numerai Data
Generating Candidate Features
Implementing the Information-Theoretic Feature Selection Algorithm
Feature selection is a critical step in the machine learning pipeline. Selecting the right subset of features can lead to:
Improved Model Performance: By removing irrelevant or redundant features, the model can generalize better.
Reduced Overfitting: Fewer features mean less complexity, which reduces the chances of the model capturing noise.
Enhanced Interpretability: Simpler models with fewer features are easier to understand and explain.
Reduced Computational Cost: Fewer features lead to faster training times and lower memory usage.
Traditional feature selection methods like Recursive Feature Elimination (RFE), Boruta, and SHAP values have their limitations, such as inability to handle feature redundancy effectively. Here, we propose an information-theoretic approach to overcome these challenges.
2. Setup and Installation
First, ensure that all necessary libraries are installed. You can install any missing libraries using pip.
Numerai provides a public dataset that you can use for experimentation. If you haven't downloaded the dataset yet, you can obtain it from Numerai's official website.
For the purpose of this notebook, we'll assume you have the dataset in a CSV file named numerai_dataset.csv.
import pandas as pd
import numpy as np
# Load Numerai data
# Replace 'numerai_dataset.csv' with the path to your Numerai dataset
df = pd.read_csv('numerai_dataset.csv')
# Display the first few rows
df.head()
Note: Numerai's dataset typically includes numerous features (feature_1 to feature_N), along with target variables like target and era.
Let's perform some basic preprocessing.
# Check for missing values
missing_values = df.isnull().sum()
print("Missing Values:\n", missing_values[missing_values > 0])
# For simplicity, let's drop rows with missing values
df.dropna(inplace=True)
# Encode the target if it's categorical
# Numerai uses a regression target, but adjust accordingly
# For classification, you'd encode labels as needed
4. Generating Candidate Features
Feature engineering is pivotal in enhancing model performance. While the original article used kxy for feature generation, we'll manually create a set of candidate features using domain knowledge and statistical transformations.
4.1 Basic Statistical Features
We can create statistical features such as mean, median, standard deviation, etc., for numerical features.
# Identify numerical features (excluding the target and era)
numeric_features = [col for col in df.columns if col.startswith('feature_')]
# Example: Generating statistical features
for feature in numeric_features:
df[f'{feature}_mean'] = df[feature].mean()
df[f'{feature}_median'] = df[feature].median()
df[f'{feature}_std'] = df[feature].std()
df[f'{feature}_abs'] = df[feature].abs()
4.2 Interaction Features
Creating interaction terms can help capture the relationship between pairs of features.
from itertools import combinations
# Generate interaction features between pairs of features
for f1, f2 in combinations(numeric_features, 2):
df[f'{f1}_x_{f2}'] = df[f1] * df[f2]
4.3 Lag Features
If the data is time-series or has a temporal component (era in Numerai), lag features can be insightful.
# Sort data by 'era' to ensure proper lagging
df.sort_values('era', inplace=True)
# Generate lag features
lags = [1, 2, 3]
for feature in numeric_features:
for lag in lags:
df[f'{feature}_lag_{lag}'] = df.groupby('era')[feature].shift(lag)
# After creating lag features, drop rows with NaNs introduced by shifting
df.dropna(inplace=True)
Note: Adjust lag generation according to the data's temporal structure.
5. Implementing the Information-Theoretic Feature Selection Algorithm
We'll implement the feature selection algorithm as described:
Initialize: Start with the feature that has the highest individual performance.
Iterative Selection: At each step, add the feature that provides the highest incremental improvement in performance when combined with the already selected features.
Termination: Stop when no significant improvement is observed or all features are selected.
We'll use cross-validated model performance (e.g., accuracy for classification or R² for regression) as the performance metric.
5.1 Define the Performance Metric
For this example, we'll assume a regression problem and use R² as the performance metric.
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import Ridge # As an example model
def evaluate_performance(X, y, model):
"""Evaluate model performance using cross-validation."""
scores = cross_val_score(model, X, y, cv=5, scoring='r2')
return scores.mean()
5.2 Feature Selection Algorithm
# Define the target variable
target = 'target' # Replace with the actual target column name
# List of candidate features (excluding target and era)
candidate_features = [col for col in df.columns if col.startswith('feature_') or '_' in col]
# Initialize variables
selected_features = []
remaining_features = candidate_features.copy()
performance_history = []
# Initialize the model
model = Ridge(alpha=1.0)
# Extract target variable
y = df[target].values
# Iterative Feature Selection
while remaining_features:
best_feature = None
best_performance = -np.inf
for feature in remaining_features:
# Current feature set to evaluate
current_features = selected_features + [feature]
X = df[current_features].values
# Evaluate performance
performance = evaluate_performance(X, y, model)
# Debug: Print feature and its performance
print(f"Evaluating feature: {feature}, Performance: {performance:.4f}")
# Check if this is the best performance so far
if performance > best_performance:
best_performance = performance
best_feature = feature
if best_feature is None:
print("No improvement observed. Stopping feature selection.")
break
# Add the best feature to the selected list
selected_features.append(best_feature)
remaining_features.remove(best_feature)
performance_history.append(best_performance)
print(f"Selected Feature: {best_feature}, Current Performance: {best_performance:.4f}\n")
# Optional: Define a threshold for minimum improvement to continue
if len(performance_history) > 1:
improvement = performance_history[-1] - performance_history[-2]
if improvement < 0.001: # Example threshold
print("Minimal improvement observed. Stopping feature selection.")
break
Explanation:
Initialization:
selected_features: Starts empty.
remaining_features: All candidate features initially.
performance_history: Keeps track of performance after each feature addition.
Iterative Process:
For each remaining feature, temporarily add it to the selected_features and evaluate the model's performance.
Select the feature that results in the highest performance improvement.
Repeat until no significant improvement is observed or all features are selected.
Note: This process can be computationally intensive, especially with a large number of features. Consider parallelizing the evaluation or reducing the number of candidate features if needed.
6. Evaluating and Selecting Features
After running the feature selection algorithm, let's analyze the selected features and their contributions.
import matplotlib.pyplot as plt
import seaborn as sns
# Display selected features and their performance
feature_performance = pd.DataFrame({
'Feature': selected_features,
'Performance': performance_history
})
print(feature_performance)
# Plot the performance history
plt.figure(figsize=(12, 6))
sns.lineplot(x=range(1, len(performance_history)+1), y=performance_history, marker='o')
plt.title('Feature Selection Progress')
plt.xlabel('Number of Features Selected')
plt.ylabel('Cross-Validated R²')
plt.grid(True)
plt.show()
Interpreting the Results:
Selected Features: The order in which features were selected reflects their individual and combined contributions to the model's performance.
Performance Plot: Visualizes how adding each feature impacts the model's performance. A plateau indicates diminishing returns, suggesting that additional features offer minimal improvement.
7. Analyzing the Results
Let's delve deeper into the selected features and understand their significance.
# Final performance with all selected features
final_performance = performance_history[-1]
print(f"Final Performance with {len(selected_features)} Features: R² = {final_performance:.4f}")
# Analyze feature importances (if using models that provide them)
# For example, using coefficients from the Ridge model
model.fit(df[selected_features], y)
coefficients = pd.Series(model.coef_, index=selected_features).sort_values(ascending=False)
print("Feature Coefficients:")
print(coefficients)
# Plot feature coefficients
plt.figure(figsize=(12, 6))
coefficients.plot(kind='bar')
plt.title('Feature Coefficients in Ridge Regression')
plt.xlabel('Features')
plt.ylabel('Coefficient Value')
plt.show()
Insights:
Model Coefficients: Indicate the direction and magnitude of each feature's influence on the target variable.
Significant Features: Features with higher absolute coefficient values have a more substantial impact.
8. Conclusion
In this notebook, we implemented an information-theoretic feature selection pipeline for Numerai data without relying on the kxy package. By iteratively selecting features that maximize the model's performance, we addressed the limitations of traditional feature selection methods like RFE and Boruta, especially regarding feature redundancy.
Key Takeaways:
Custom Feature Selection: Building a tailored feature selection algorithm allows for greater flexibility and alignment with specific project goals.
Performance-Based Selection: Focusing on the actual impact of features on model performance ensures that selected features genuinely contribute to predictive power.
Scalability Considerations: While effective, the iterative selection process can be resource-intensive. Optimizing the evaluation process or reducing candidate features can help manage computational costs.
Feel free to adapt and expand upon this pipeline to suit your specific needs and datasets!