1. Feature importance
1.1. RF feature importance
All tree-based models have feature_importances_
, like RandomForestClassifier (xgboost, lightgbm). For classification, it is typically either Gini impurity or information gain/entropy and for regression trees it is variance. Thus when training a tree, it can be computed how much each feature decreases the weighted impurity in a tree. For a forest, the impurity decrease from each feature can be averaged and the features are ranked according to this measure. [2]
There are a few things to keep in mind when using the impurity based ranking: [2]
feature selection based on impurity reduction is biased towards preferring variables with more categories (see Bias in random forest variable importance measures).
when the dataset has two (or more) correlated features, then from the point of view of the model, any of these correlated features can be used as the predictor, with no concrete preference of one over the others. But once one of them is used, the importance of others is significantly reduced since effectively the impurity they can remove is already removed by the first feature. As a consequence, they will have a lower reported importance.
1.2. Boruta
[3] shadow feature
1.3. Permutation Importance (Mean decrease accuracy)
The general idea is to permute the values of each feature and measure how much the permutation decreases the accuracy of the model.
- Get a trained model
- Shuffle the values in a single column, make predictions using the resulting dataset. Use these predictions and the true target values to calculate how much the loss function suffered from shuffling. That performance deterioration measures the importance of the variable you just shuffled.
- Return the data to the original order (undoing the shuffle from step 2.) Now repeat step 2 with the next column in the dataset, until you have calculated the importance of each column.
Permutation importance is calculated after a model has been fitted. So we won’t change the model or change what predictions we’d get for a given value of height, sock-count, etc. [1]
1.3.0.1. Code example [1]
1 | import numpy as np |
The values towards the top are the most important features, and those towards the bottom matter least.
1.3.0.2. Code example [2]
1 | from sklearn.cross_validation import ShuffleSplit |
LSTAT
and RM
are two features that strongly impact model performance: permuting them decreases model performance by ~73% and ~57% respectively. Keep in mind that these measurements are made only after the model has been trained (and is depending) on all of these features.
1.4. Null Importance (target permutation) [4]
The original paper. 较Boruta的优势:Boruta只适合用sklearn的RandomForest、需要填充缺失值、特征数量扩大一倍也会更加消耗资源。
将label的数据顺序打乱,使用树模型训练,得到每个特征对于label顺序打乱之后的feature importance;
重复以上操作多次(e.g. 80次),每个特征都有80个label随机打乱顺序后得到的feature importance,这80个feature importance构成一个分布;
- 对于每个特征,不打乱顺序的actual feature importance在这个分布下的显著性水平
为什么叫Null importance呢?考虑原假设(Null hypothesis):特征与label不相关。随机打乱label顺序,特征的feature importance的分布称之为Null importance distribution。
examples: [4]
蓝色hist为label顺序打乱之后的feature impotance distribution,红色线为实际的feature importance值。
Under the null hypothesis and normal distribution if for a feature the red actual importance is within the blue distribution then chances are the feature is not correlated with the target. If it’s within the 5% right part of the blue distribution or outside then it’s correlated.
第一个特征的Gain importance落在了分布之内,显著性水平肯定>0.05,所以可以认为这个特征与实际的label不相关。第二个特征的actual feature importance与null feature importance distribution相距甚远,所以认为这个特征与actual label有相关性。
Score features:,加入1e-10是因为np.log(0)
会报错,同理分母+1也是防止报错。
Assess correlation to the target:. Build this metric to show hwo far the actual importance is from the noise (null importance) distribution. 通过设定不同的阈值做feature selection,得到不同的feature集合,再用这些feature训练lgb模型,通过cv的结果来比较不同的feature集合:
1 | def score_feature_selection(df=None, train_features=None, cat_feats=None, target=None): |
1.5. Reference:
Feature Importance Measures for Tree Models — Part I
https://www.kaggle.com/ogrellier/noise-analysis-of-porto-seguro-s-features
https://www.kaggle.com/ogrellier/feature-selection-target-permutations (use target permutation instead of feature permutation)
http://danielhomola.com/2015/05/08/borutapy-an-all-relevant-feature-selection-method/
Feature Selection with Null Importances
Note: convert all category features to
.astype('category')
, not usingpd.factorize
. Because the latter will convert all unknown or NaN values to -1, but lgb usesLabelEncoder
which cannot handle negative values.