Package: lofo-importance
LOFO (Leave One Feature Out) Importance calculates the importances of a set of features based on a metric of choice, for a model of choice, by iteratively removing each feature from the set, and evaluating the performance of the model, with a validation scheme of choice, based on the chosen metric.LOFO first evaluates the performance of the model with all the input features included, then iteratively removes one feature at a time, retrains the model, and evaluates its performance on a validation set. The mean and standard deviation (across the folds) of the importance of each feature is then reported.If a model is not passed as an argument to LOFO Importance, it will run LightGBM as a default model.InstallLOFO Importance can be installed usingpip install lofo-importanceAdvantages of LOFO ImportanceLOFO has several advantages compared to other importance types:It does not favor granular featuresIt generalises well to unseen test setsIt is model agnosticIt gives negative importance to features that hurt performance upon inclusionIt can group the features. Especially useful for high dimensional features like TFIDF or OHE features.It can automatically group highly correlated features to avoid underestimating their importance.Example on Kaggle's Microsoft Malware Prediction CompetitionIn this Kaggle competition, Microsoft provides a malware dataset to predict whether or not a machine will soon be hit with malware. One of the features, Centos_OSVersion is very predictive on the training set, since some OS versions are probably more prone to bugs and failures than others. However, upon splitting the data out of time, we obtain validation sets with OS versions that have not occurred in the training set. Therefore, the model will not have learned the relationship between the target and this seasonal feature. By evaluating this feature's importance using other importance types, Centos_OSVersion seems to have high importance, because its importance was evaluated using only the training set. However, LOFO Importance depends on a validation scheme, so it will not only give this feature low importance, but even negative importance.importpandasaspdfromsklearn.model_selectionimportKFoldfromlofoimportLOFOImportance,Dataset,plot_importance%matplotlibinline# import datatrain_df=pd.read_csv("../input/train.csv",dtype=dtypes)# extract a sample of the datasample_df=train_df.sample(frac=0.01,random_state=0)sample_df.sort_values("AvSigVersion",inplace=True)# Sort by time for time split validation# define the validation schemecv=KFold(n_splits=4,shuffle=False,random_state=None)# Don't shuffle to keep the time split split validation# define the binary target and the featuresdataset=Dataset(df=sample_df,target="HasDetections",features=[colforcolintrain_df.columnsifcol!="HasDetections"])# define the validation scheme and scorer. The default model is LightGBMlofo_imp=LOFOImportance(dataset,cv=cv,scoring="roc_auc")# get the mean and standard deviation of the importances in pandas formatimportance_df=lofo_imp.get_importance()# plot the means and standard deviations of the importancesplot_importance(importance_df,figsize=(12,20))Another Example: Kaggle's TReNDS CompetitionIn this Kaggle competition, pariticipants are asked to predict some cognitive properties of patients.
Independent component features (IC) from sMRI and very high dimensional correlation features (FNC) from 3D fMRIs are provided.
LOFO can group the fMRI correlation features into one.defget_lofo_importance(target):cv=KFold(n_splits=7,shuffle=True,random_state=17)dataset=Dataset(df=df[df[target].notnull()],target=target,features=loading_features,feature_groups={"fnc":df[df[target].notnull()][fnc_features].values})model=Ridge(alpha=0.01)lofo_imp=LOFOImportance(dataset,cv=cv,scoring="neg_mean_absolute_error",model=model)returnlofo_imp.get_importance()plot_importance(get_lofo_importance(target="domain1_var1"),figsize=(8,8),kind="box")Flofo ImportanceIf running the LOFO Importance package is too time-costly for you, you can use Fast LOFO. Fast LOFO, or FLOFO takes, as inputs, an already trained model and a validation set, and does a pseudo-random permutation on the values of each feature, one by one, then uses the trained model to make predictions on the validation set. The mean of the FLOFO importance is then the difference in the performance of the model on the validation set over several randomised permutations.
The difference between FLOFO importance and permutation importance is that the permutations on a feature's values are done within groups, where groups are obtained by grouping the validation set by k=2 features. These k features are chosen at random n=10 times, and the mean and standard deviation of the FLOFO importance are calculated based on these n runs.
The reason this grouping makes the measure of importance better is that permuting a feature's value is no longer completely random. In fact, the permutations are done within groups of similar samples, so the permutations are equivalent to noising the samples. This ensures that:The permuted feature values are very unlikely to be replaced by unrealistic values.A feature that is predictable by features among the chosen n*k features will be replaced by very similar values during permutation. Therefore, it will only slightly affect the model performance (and will yield a small FLOFO importance). This solves the correlated feature overestimation problem.