I wrapped up three mutual information based feature selection methods in a scikit-learn like module. You can find it on my GitHub. It is very easy to use, you can run the example.py or import it into your project and apply it to your data like any other scikit-learn method.
import pandas as pd
# load X and y
X = pd.read_csv('my_X_table.csv', index_col=0).values
y = pd.read_csv('my_y_vector.csv', index_col=0).values
# define MI_FS feature selection method
feat_selector = mifs.MutualInformationFeatureSelector()
# find all relevant features
# check selected features
# check ranking of features
# call transform() on X to filter it down to selected features
X_filtered = feat_selector.transform(X)
Mutual information based filter methods
The following bit is adopted from my late stage PhD report.
In the past twenty years, a large variety of information theory based filter methods were developed. Filter methods represent a subclass of feature selection algorithms which are classifier independent and capture the discriminating power of each feature by calculating some sort of relevance statistics with respect to the outcome variable. These statistics are used in a heuristic scoring criterion which acts as a proxy measure for classification accuracy. Therefore filter methods can rank the features by their relevance computationally cheaply, without the need of training classifiers on the data.
Information theory based filter methods assess the mutual information between the features of and . To follow the conventional notation of introductory texts in information theory however, will have a different meaning in this post.
Information theory is concerned about quantifying the uncertainty present in distributions, by measuring their entropy. The entropy of the distribution of variable , denoted as , is defined as:
where denotes a possible value of variable , that it can take from a set of values , and is the distribution of . This equation quantifies the uncertainty in , and for discrete variables it could be computed by estimating as the fraction of observations taking on value from the total :
If peaks around a certain value, than the entropy of it will be low, while if it is uniform, meaning all events in are equally likely, it will be high. Furthermore, conditional entropy of two distributions could be defined as:
which represents the amount of uncertainty remaining in after we have seen .
Mutual information (Shannon 1948) between and is then defined as:
In this difference the first term represents the uncertainty before is known, while the second term captures the uncertainty after is known. Thus mutual information could also be thought of, as the amount of uncertainty in that is removed by knowing . Mutual information is symmetric and is zero if and only if and are statistically independent.
Building on the these concepts, and given we have three discrete random variables , the conditional mutual information could be defined as:
where is the joint mutual information.
As described earlier, filter methods attempt to rank the features based on statistical relevance measures. Therefore the simplest information theoretic filter techniques simply calculate the mutual information between all features and independently, rank them, and select the best ones. This is called Mutual Information Maximisation and it has been used in many early algorithms (Lewis 1992). This method is known to be suboptimal however, when the features are interdependent, as it will end up selecting highly correlated thus redundant features.
Using the Joint Mutual Information (JMI) Moody Moody 1999) and Meyer et al. (Meyer 2008) proposed a method that focuses on increasing the complementary information between the selected features:
where is the JMI score for feature under consideration, and represents all features that were selected in previous iterations of the algorithm. This selection criterion ensures, that the candidate feature has to be complementary with all the previously selected features in order to be added to .
Brown et al. (Brown 2012) in their extensive review, systematically benchmarked 17 information theoretic filter methods including the widely used Mutual Information Feature Selection (Battiti 1994) and Max-Relevance Min-Redundancy (Peng 2005) algorithms. They performed a large empirical study to rank these methods by their accuracy, stability and flexibility, and the JMI criterion based feature selection methods were picked as overall winners.
How to select features using mutual information?
OK, see we have these mathematical concepts of entropy and mutual information and joint mutual information. They are very useful as they don’t really assume anything about our data, yet they can somehow capture if two random variables have any sort of similarity or “connection”. But how could we use these concepts to actually perform feature selection?
Let’s introduce some notation. We have a matrix of data and an outcome variable , which could be discrete or continuous. We want to select a set of features from , such that . Let’s call the set of all features . We do this btw to understand which features govern the system mostly and/or to reduce the chance of over-fitting the noise in our data.
There are two problems to address with mutual information based feature selection:
- As the number of features in our dataset () grows, the possible combinations to consider for grows exponentially and becomes intractable even for a few dozen features.
- Although it is simple to compute entropy and consequently mutual information for discrete random variables, most of the time we have continuous measurements in real life datasets. Rounding them to the nearest integer, might seem tempting but it introduces a bias into our MI estimates. Using binning or histogram based methods are better but still suffer from the same bias issue.
The first problem
To overcome the first problem we could calculate the MI between each feature and our outcome variable and select an arbitrary amount of the top-scoring ones. This would be a univariate approach, and require us to assume that all of our features are completely independent from each other. As we have discussed in our earlier post about Boruta, this is rarely the scenario we find ourselves in.
A better way would be to calculate the MI between each feature and , . Then select with the largest , remove it from and add it to .
Then in each consecutive round we would perform a greedy search and find the feature , which has the maximum joint mutual information with the previously selected features and :
This is the selection criteria of the JMI method and it was show to perform best out of 17 information theory based filter methods. As with every greedy search algorithm it can happen that we end up with suboptimal set of .
A recent alteration of this criterion is the Joint Mutual Information Maximisation (JMIM) which uses the maximum of the minimum of the JMI:
Beyond these two methods, I also implemented a third MI based filter method the Minimum Redundancy Maximum Relevance algorithm, which is quite famous and heavily used in life sciences.
The second problem
To overcome the second problem I used the well established kNN based MI estimation methods in the case when both and are continuous. I used the excellent sklearn based Python implementation of Gael Varoquaux on GitHub to get started.
Most of the times in life sciences however, we have continuous measurements in and a discrete denoting some class membership like “treatment” vs “control”. None of the previously described MI estimating procedures apply to this scenario and until recently there was no accepted way to deal with this (at least to the best of my knowledge).
Luckily an extension of the above described kNN method got published recently which deals with this problem. I coded up the method in Python and compared the MI estimates I got from it, with the Matlab implementation that came with the paper. The numbers matched up nicely and the Python version was 5-10 times faster, thanks to the fantastic kNN algorithm in scikit-learn. So the mifs module can be used to select features from a continuous matrix against a categorical or a discrete outcome variable, using mutual information based metrics.
Since these filter methods spend most of the CPU time on calculating MI between columns of and , which is embarrassingly parallel, I used joblib to make the mifs module run in parallel. It utilizes all the cores the user has and this can easily cut the time spent on feature selection in half or even make it shorter.
Hopefully this easy to use implementation will make these information theory based feature selection methods more appealing to researchers. Please let me know if I cocked something up or if you think I could improve this work. I’m planning to adopt a chapter from my late stage PhD report into another blog post and show some benchmarking results where I compare the MI based filter methods, the Boruta method and other better established FS methods, so stay tuned..