-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add model-based explainability
- Loading branch information
Showing
6 changed files
with
64 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from .shap import ShapFeatureImportance | ||
from .pfi import PFIFeatureImportance | ||
from .model_based import ModelBasedFeatureImportance | ||
|
||
__all__ = [ | ||
'ShapFeatureImportance', | ||
'PFIFeatureImportance', | ||
'ModelBasedFeatureImportance', | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from .base import BaseFeatureImportance | ||
import pandas as pd | ||
import numpy as np | ||
|
||
|
||
class ModelBasedFeatureImportance(BaseFeatureImportance): | ||
""" | ||
Model-Based Feature Importance calculator. | ||
This class uses model-specific attributes to compute feature importance scores. | ||
Parameters | ||
---------- | ||
See base class BaseFeatureImportance for parameter details. | ||
""" | ||
|
||
def compute_feature_importance(self) -> pd.DataFrame: | ||
""" | ||
Compute Model-Based Feature Importance. | ||
This method uses model-specific attributes to calculate feature importance. | ||
Returns | ||
------- | ||
pd.DataFrame | ||
A DataFrame with features and their model-based importance scores, sorted in descending order. | ||
Raises | ||
------ | ||
AttributeError: | ||
If the model does not have the required attribute for feature importance. | ||
""" | ||
if hasattr(self.model, "feature_importances_"): | ||
importances = self.model.feature_importances_ | ||
elif hasattr(self.model, "coef_"): | ||
importances = np.abs(self.model.coef_[0]) | ||
else: | ||
raise AttributeError( | ||
"No attribute 'feature_importances_' or 'coef_' found for the model." | ||
) | ||
importance = pd.DataFrame({ | ||
'feature': self.X_train.columns, | ||
'importance': importances | ||
}) | ||
return importance.sort_values('importance', ascending=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters