How to detect constant, quasi-constant features in your dataset | by Samarth Agrawal | Towards Data Science

Two things distinguish top data scientists from others in most cases: Feature Creation and Feature Selection. i.e., creating features that capture deeper/hidden insights about the business or customer and then making the right choices about which features to choose for your model.
Quality of Machine Learning model depends upon your data — Garbage in, Garbage out. (Garbage here would mean bad data/noise in data).
After an extensive Feature Engineering step, you would end up with a large number of features. You may not use all the features in your model. You would be interested in feeding your model only those significant features or remove the ones that do not have any predictive power.
This post is for identifying all those features that are Constant. It can be of two types:
Constant Feature: Same value in all the records.Quasi Constant Feature: One of the values is dominant 99.9%.
Constant Feature: Same value in all the records.
Quasi Constant Feature: One of the values is dominant 99.9%.
As shown in the example image, you only have Toyota as a value in Car Make for all the records in your data. Your machine learning model won’t learn anything insightful by keeping this feature in training. You are better off dropping this feature. Likewise, there can be many more such features, and you need a more automatic way of identifying this.
1) Use the get_constant_features functions to get all the constant features.
2) Store all the constant features as a list for removing from the dataset.
3) Drop all such features from the dataset. We can see that the number of features has dropped to 268 from 301.
As shown in the example image — in your data, ‘Camry’ accounts for 99.9% of records. Your machine learning model won’t learn anything insightful by keeping this feature in training. Or worse, your model can learn from the fringe cases and cause overfitting. You are better off dropping this feature. Likewise, there can be many more such features, and you need a more automatic way of identifying this.
1) Use the get_constant_features functions to get all the constant features.
threshold: cut-off value for defining the quasi-ness of the data you want to eliminate. 99%, 99.9%, 98% etc.
2) Store all the quasi constant features as a list for removing from the dataset.
3) Drop all such features from the dataset. We can see that the number of features has dropped to 123 from 268.
Code Snippet for both Constant & Quasi Constant features:
from fast_ml.utilities import display_allfrom fast_ml.feature_selection import get_constant_features# Use the function to get the results in dataframeconstant_features = get_constant_features(df)display_all(constant_features)# All the constant features stored in a listconstant_features_list = constant_features['Var'].to_list()# Drop all the constant features from the datasetdf.drop(columns = constant_features_list, inplace=True)
If you enjoyed this, follow me on medium for more.
Interested in collaborating? Let’s connect on Linkedin.
Please feel free to write your thoughts/suggestions/feedback.
Kaggle link
Fast_ml link
Notebook is available at the following location with fully functional code: