calc_importance
calc_importance(
data=None,
outcome=None,
predictors=None,
method='rwa',
recode=True,
likert_levels=None,
)Driver importance.
Ranks how much each predictor drives an outcome such as the NPS rating, as importance scores summing to 100. All three methods answer the same question and are scaled the same way, so each feature’s importance reads as “this feature accounts for X% of what drives the outcome” and any of them can be fed to plot_ipm(). They differ in what they can see:
- “rwa”, relative weights analysis (Johnson’s epsilon), shares out the linear model’s explained variance between correlated predictors, which ordinary regression coefficients handle poorly. This is the default because rating batteries are almost always heavily correlated. The port computes it with numpy exactly as R’s
rwapackage does. - “forest” grows a random forest and measures how much prediction error rises when each predictor is permuted, so it picks up non-linear effects and interactions that relative weights cannot. It is the useful cross-check: if a feature ranks high here and low under “rwa”, its effect is not linear. Being a random method, it moves a little between runs; call
numpy.random.seed()first for a figure you intend to publish. Python draws different random numbers from R, so the values differ from R’s while the ranking agrees. - “correlation” is the crude one, ignoring the other predictors entirely, so correlated features double-count. It needs no optional package and cannot fail to converge, which makes it a reasonable sanity check.
Only complete cases are used. Worded rating columns are recoded to 1-5 automatically (recode=True). Negative importances are floored at zero before rescaling. Most users call ipm_model(), which pairs this with performance for the importance/performance matrix.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | pandas.DataFrame | A data frame. If omitted, the session default is used. | None |
| outcome | str | The outcome column, e.g. “nps_value”. | None |
| predictors | (str, selector or list) | The predictor columns, e.g. starts_with("ratings_"). |
None |
| method | str | “rwa” (default), “forest” (needs scikit-learn) or “correlation”. | 'rwa' |
| recode | bool | If True (default), text predictor columns are mapped to 1-5 with recode_likert(); numeric columns are left as-is. |
True |
| likert_levels | list of str | Scale wordings passed to recode_likert() when recoding. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| pandas.DataFrame | feature and importance, the latter rescaled to sum to 100, strongest driver first. |
See Also
ipm_model, plot_ipm
Examples
>>> calc_importance(podracing_survey, "nps_value", starts_with("ratings_"))
>>> import numpy as np
>>> np.random.seed(1)
>>> calc_importance(podracing_survey, "nps_value", starts_with("ratings_"), method="forest")