Download src/explanability/shap_explainer.py from MohitRajput45/Fraud-Guard-Intelligence: direct link, hf CLI and curl.
- Browser
- Download file 669 Bytes
-
https://huggingface.co/spaces/MohitRajput45/Fraud-Guard-Intelligence/resolve/main/src/explanability/shap_explainer.py
- Command line
-
hf download hf://spaces/MohitRajput45/Fraud-Guard-Intelligence/src/explanability/shap_explainer.py
-
curl -L -o shap_explainer.py https://huggingface.co/spaces/MohitRajput45/Fraud-Guard-Intelligence/resolve/main/src/explanability/shap_explainer.py
669 Bytes
| import shap | |
| import joblib | |
| import os | |
| import pandas as pd | |
| class ShapExplainer: | |
| def __init__(self): | |
| BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) | |
| ARTIFACTS_PATH = os.path.join(BASE_DIR, "artifacts") | |
| self.model = joblib.load(os.path.join(ARTIFACTS_PATH, "xgb_model.pkl")) | |
| # 🔥 FIX: TreeExplainer is specifically built for tree-based models like XGBoost | |
| self.explainer = shap.TreeExplainer(self.model) | |
| def explain(self, data: pd.DataFrame): | |
| # Generate SHAP values for the given data | |
| shap_values = self.explainer(data) | |
| return shap_values |