Spaces:
Sleeping
Sleeping
Download ml_model/explainability.py from VandnaShukla/bluealert-api: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/VandnaShukla/bluealert-api/resolve/main/ml_model/explainability.py
- Command line
-
hf download hf://spaces/VandnaShukla/bluealert-api/ml_model/explainability.py
-
curl -L -o explainability.py https://huggingface.co/spaces/VandnaShukla/bluealert-api/resolve/main/ml_model/explainability.py
3.67 kB
| """Model explainability module using feature importance for Blue Alert predictions.""" | |
| import numpy as np | |
| import json | |
| class ModelExplainer: | |
| """Generates feature importance-based explanations for water quality predictions.""" | |
| def __init__(self, model, scaler=None): | |
| self.model = model | |
| self.scaler = scaler | |
| self.feature_names = [ | |
| 'ph', 'Hardness', 'Solids', 'Chloramines', 'Sulfate', | |
| 'Conductivity', 'Organic_carbon', 'Trihalomethanes', 'Turbidity', | |
| ] | |
| def get_feature_importance(self): | |
| """Get feature importance from the model.""" | |
| if hasattr(self.model, 'feature_importances_'): | |
| importances = self.model.feature_importances_ | |
| return dict(zip(self.feature_names, importances.tolist())) | |
| if hasattr(self.model, 'estimator'): | |
| base = self.model.estimator | |
| if hasattr(base, 'feature_importances_'): | |
| importances = base.feature_importances_ | |
| return dict(zip(self.feature_names, importances.tolist())) | |
| if hasattr(self.model, 'calibrated_classifiers_'): | |
| for cc in self.model.calibrated_classifiers_: | |
| base = cc.estimator | |
| if hasattr(base, 'feature_importances_'): | |
| importances = base.feature_importances_ | |
| return dict(zip(self.feature_names, importances.tolist())) | |
| return None | |
| def _get_tree_based_estimator(self): | |
| """Navigate through model wrappers to find a tree-based estimator.""" | |
| base_model = self.model | |
| if hasattr(self.model, 'calibrated_classifiers_'): | |
| base_model = self.model.calibrated_classifiers_[0].estimator | |
| if hasattr(base_model, 'named_estimators_'): | |
| for est_name, est in base_model.named_estimators_.items(): | |
| if hasattr(est, 'feature_importances_'): | |
| return est | |
| if hasattr(base_model, 'feature_importances_'): | |
| return base_model | |
| return None | |
| def explain_prediction(self, features, feature_names=None): | |
| """Explain a single prediction using feature importance. | |
| SHAP is skipped entirely due to numexpr compatibility issues. | |
| Feature importance provides sufficient model transparency for water quality predictions. | |
| """ | |
| if feature_names is None: | |
| feature_names = self.feature_names | |
| features_array = np.array(features).reshape(1, -1) | |
| try: | |
| # Get feature importance from model | |
| importance = self.get_feature_importance() | |
| if importance: | |
| # Rank features by importance | |
| impact_ranking = sorted( | |
| importance.items(), | |
| key=lambda x: abs(x[1]), | |
| reverse=True, | |
| ) | |
| return { | |
| 'method': 'FeatureImportance', | |
| 'feature_importance': importance, | |
| 'features': dict(zip(feature_names, features.tolist() if hasattr(features, 'tolist') else features)), | |
| 'impact_ranking': [ | |
| {'feature': f, 'importance': round(imp, 4)} | |
| for f, imp in impact_ranking | |
| ], | |
| 'note': 'Using feature importance for model transparency', | |
| } | |
| else: | |
| return { | |
| 'error': 'No feature importance available from model', | |
| } | |
| except Exception as e: | |
| return { | |
| 'error': f'Explanation generation failed: {str(e)}', | |
| } | |