rfflearn.cpu.permutation_feature_importance
Calculate permutation importance, and set the feature importance (mean of permutation importance for each trial) as model.feature_importances_
.
def rfflearn.cpu.permutation_feature_importance(model, Xs, ys, **kwargs):
Parameters:
model: object
An estimator that has already been fitted.
Xs: np.ndarray
Input data of shape (n_samples, n_features), on which permutation importance will be computed.
ys: np.ndarray
Input label of shape (n_samples,) or (n_samples, n_classes). Specify None for unsupervised model.
kwargs: dict (default={})
Additional keyword arguments for sklearn.inspection.permutation_importance
.
Returns:
np.ndarray
Raw permutation importance scores of shape (n_features, n_repeats).
Notes:
- This function add an attribute
feature_importances_
to themodel
instance.
rfflearn.cpu.permutation_plot
Visualize permutation importance as a box diagram.
def rfflearn.cpu.permutation_plot(permutation_importances, feature_names, show=True):
Parameters:
permutation_importances: np.ndarray
Raw scores of shape (num_features, num_repeats).
feature_names: list
List of feature names.
show: bool
Shows plot if True.
rfflearn.cpu.shap_feature_importance
def rfflearn.cpu.shap_feature_importance(model, Xs):
Parameters:
model: object
An estimator that has already been fitted.
Xs: np.ndarray
Input data of shape (n_samples, n_features), on which permutation importance will be computed.
Returns:
np.ndarray
SHAP values of shape (n_samples, n_features).
Notes:
- This function add an attribute
feature_importances_
to themodel
instance.
rfflearn.cpu.shap_plot
Calculate SHAP values using shap library, and set the feature importance (absolute of SHAP values) as model.feature_importances_
.
def rfflearn.cpu.shap_plot(*pargs, **kwargs):
Parameters:
pargs: tuple (default: ())
Positional arguments for shap.summary_plot
.
kwargs: dict (default: {})
Keyword arguments for shap.summary_plot
.
Notes:
- This function is just an alias of the
shap.summary_plot function
because theshap.summary_plot
is implemented very well!!
Minimal Example
>>> import numpy as np # Import Numpy
>>> import rfflearn.cpu as rfflearn # Import rfflearn
>>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) # Define input data
>>> y = np.array([1, 1, 2, 2]) # Definr label data
>>> model = rfflearn.RFFSVC().fit(X, y) # Training a model
>>> Is = rfflearn.permutation_feature_importance(model, X, y) # Compute permutation importance
>>> model.feature_importances_ # Confirm feature importance
array([0.25, 0.3 ])
>>> permutation_plot(Is, ["x1", "x2"], show=True) # Plot importance and show