# Feature importance with high-cardinality categorical features for regression (numerical depdendent variable)

I was trying to use feature importances from Random Forests to perform some empirical feature selection for a regression problem where all the features are categorical and a lot of them have many levels (on the order of 100-1000). Given that one-hot encoding creates a dummy variable for each level, the feature importances are for each level and not each feature (column). What is a good way to aggregate these feature importances?

I thought about summing or getting the average importance for all levels of a feature (probably the former will be biased towards those features with more levels). Are there any references on this issue?

What else can one do to decrease the number of features? I am aware of group lasso, could not find anything easy to use for scikit-learn.

• Can anyone answer the question of whether summing the variable importance of each level of the categorical variable makes sense? Jul 30, 2018 at 15:35
• @see24 No you can't just sum them: stats.stackexchange.com/questions/314567/…
– Dan
Apr 24, 2019 at 14:35

It depends on how you're one-hot encoding them. Many automated solutions for that will name all the converted booleans with a pattern so that a categorical variable called "letter" with values A-Z would end up like:

letter_A, letter_B, letter_C, letter_D,....

If after you've figured out feature importance you've got an array of feature and the associated weight/importance, I would analyze the array and perhaps sum up the feature importance weights for anything starting with "letter%".

• Isn't the sum giving an advantage to those features with more levels? Apr 6, 2017 at 14:34
• Hmm, good point. Maybe sum it up then divide by the number of levels/one-hot encoded variables to get an "average" importance.
– CalZ
Apr 6, 2017 at 15:11
• I thought about this some more and it depends on how the importance is scored. In some cases, the value for each feature is a relative weight where the whole set totals to 1. In that case, I think it would make sense to sum up the one-hot features. If the score for the feature was more like a regression coefficient and not weighted relative to the net effect, then averaging would probably be better.
– CalZ
Apr 6, 2017 at 23:42
• Thank you for the reply. Given that I am quite new to the area, I thought that this was a standard thing for people in data science but either it is not what I should be doing to assess feature importance of a column or this post did not get enough views. In any case, thank you! Apr 7, 2017 at 12:12
• Many people advocate looking at the model's internals as a black box and evaluating the performance instead. In certain cases (e.g. neural networks) this is because you can't really examine it deeply. For some where you can easily get a view of which features are important (e.g. linear regression), you can easily be mislead (see: stats.stackexchange.com/questions/105114/…). I think that's why people sometimes shy away from looking at individual feature importance.
– CalZ
Apr 7, 2017 at 13:45