This tutorial explains how to use feature importance plots from pyrasgo to perform feature selection. The feature importance importance is calculated from SHAP values from catboost.
This notebook will calculate the SHAP feature importance when predicting arrival delay for flights in and out of NYC in 2013.
This tutorial uses:
Enter your email and password to create an account. This account gives you free access to the Rasgo API which will calculate dataframe profiles, generate feature importance score, and produce feature explainability for you analysis. In addition, this account allows you to maintain access to your analysis and share with your colleagues.
Note: This only needs to be run the first time you use pyrasgo.
#pyrasgo.register(email='<your email>', password='<your password>')
Enter the email and password you used at registration to connect to Rasgo.
rasgo = pyrasgo.login(email='<your email>', password='<your password>')
The data is from rdatasets imported using the Python package statsmodels.
This should return a table resembling something like this:
<class 'pandas.core.frame.DataFrame'>RangeIndex: 336776 entries, 0 to 336775Data columns (total 19 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 year 336776 non-null int64 1 month 336776 non-null int64 2 day 336776 non-null int64 3 dep_time 328521 non-null float64 4 sched_dep_time 336776 non-null int64 5 dep_delay 328521 non-null float64 6 arr_time 328063 non-null float64 7 sched_arr_time 336776 non-null int64 8 arr_delay 327346 non-null float64 9 carrier 336776 non-null object 10 flight 336776 non-null int64 11 tailnum 334264 non-null object 12 origin 336776 non-null object 13 dest 336776 non-null object 14 air_time 327346 non-null float64 15 distance 336776 non-null int64 16 hour 336776 non-null int64 17 minute 336776 non-null int64 18 time_hour 336776 non-null object dtypes: float64(5), int64(9), object(5)memory usage: 48.8+ MB
As this model will predict arrival delay, the Null values are caused by flights did were cancelled or diverted. These can be excluded from this analysis.
Remove variables that are not of interest to this analysis with the exclude_columns parameter.
First, pyrasgo needs to generate the feature importance scores that will be used during feature selection. This will open another browser window with the feature importance and return to raw values in response.
Select the top 5 features based on the feature importance score using the top_5 parameter. Alternatively, the parameter top_n_pct allows you to keep the top N percent of features, and the parameter pct_of_top_feature allows you to keep all features that have a feature importance score relative to the score of the top feature above N percent.