How to Effectively Use mRMR for Feature Selection

Apr 7, 2021 | Data Science

In the realm of machine learning, selecting the right features from a dataset is paramount. This blog article will guide you through the concepts of mRMR (minimum Redundancy – Maximum Relevance), how to install and use it, and troubleshooting tips!

What is mRMR?

mRMR stands for minimum Redundancy – Maximum Relevance and is a feature selection algorithm designed to identify the smallest set of relevant features necessary for a given task. Think of it as a chef selecting only the most essential ingredients for a dish, ensuring the flavors shine without overpowering each other.

Why is mRMR Unique?

The uniqueness of mRMR lies in its minimal-optimal approach, aimed at finding the least number of useful features. Here’s why this approach is appealing:

  • Reduces memory usage.
  • Decreases the time required for training.
  • Enhances overall performance.
  • Improves explainability of results.

In contrast, many other methods, like Boruta or Positive-Feature-Importance, are classified as all-relevant. They identify every feature that has a relationship with the target variable, which can result in excessive complexity.

When to Use mRMR?

Due to its efficiency, mRMR is particularly suitable for practical ML applications, where automated feature selection is frequently necessary. A notable example of its application is seen in a **2019** paper by **Uber** engineers, detailing its implementation in their marketing machine learning platform. You can find the paper here.

How to Install mRMR

To get started with mRMR, you can easily install the package via pip:

pip install mrmr_selection

After installation, you can import it in your Python environment using:

import mrmr

How to Use mRMR

The mRMR selection can be performed through various supported tools. Below going to walk you through examples using Pandas, Polars, Spark, and Google BigQuery.

1. Using Pandas

Imagine you have a dataset in a Pandas DataFrame (X) and a series which is your target variable (y). To select the best K features, use the following:


import pandas as pd
from sklearn.datasets import make_classification

# Create a sample Pandas DataFrame
X, y = make_classification(n_samples=1000, n_features=50, n_informative=10, n_redundant=40)
X = pd.DataFrame(X)
y = pd.Series(y)

# Select top 10 features using mRMR
from mrmr import mrmr_classif
selected_features = mrmr_classif(X=X, y=y, K=10)

The output is a ranking list of the top K selected features.

2. Using Polars

Now, let’s imagine using Polars:


import polars

data = [(1.0, 1.0, 1.0, 7.0, 1.5, -2.3), (2.0, None, 2.0, 7.0, 8.5, 6.7)]
columns = ['target', 'some_null', 'feature', 'constant', 'other_feature', 'another_feature']
df_polars = polars.DataFrame(data=data, schema=columns)

# Select top 2 features using mRMR
import mrmr
selected_features = mrmr.polars.mrmr_regression(df=df_polars, target_column='target', K=2)

3. Using Spark

Using Spark is analogous to gathering a larger team to tackle a problem together:


import pyspark

session = pyspark.sql.SparkSession(pyspark.context.SparkContext())
data = [(1.0, 1.0, 1.0, 7.0, 1.5, -2.3), (2.0, float(NaN), 2.0, 7.0, 8.5, 6.7)]
columns = ['target', 'some_null', 'feature', 'constant', 'other_feature', 'another_feature']
df_spark = session.createDataFrame(data=data, schema=columns)

# Select top 2 features using mRMR
import mrmr
selected_features = mrmr.spark.mrmr_regression(df=df_spark, target_column='target', K=2)

4. Using Google BigQuery

Lastly, if you’re diving into BigQuery:


from google.cloud.bigquery import Client

# Initialize BigQuery client
bq_client = Client(credentials='your_credentials')

# Select top 20 features using mRMR
import mrmr
selected_features = mrmr.bigquery.mrmr_regression(
    bq_client=bq_client,
    table_id='bigquery-public-data.covid19_open_data.covid19_open_data',
    target_column='new_deceased',
    K=20
)

Troubleshooting Tips

When implementing mRMR, you may run into some common issues. Here are a few troubleshooting ideas:

  • Ensure that all required packages, including pandas, polars, and pyspark, are installed in your environment.
  • Double-check that the target variable and feature data are in the correct format (e.g., DataFrame or specified column).
  • If any functions return errors, consult the mRMR documentation for proper usage guidelines.
  • Lastly, if you encounter persistent problems, consider reaching out for help from the community or forums focused on machine learning.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

mRMR offers an efficient method for feature selection, proven effective in many applications. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox