File size: 13,143 Bytes
8c80331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""
SHAP and LIME Explainability Module
Provides interpretable AI explanations for predictions.

Based on the blueprint for model explainability.
"""

import numpy as np
import pandas as pd
from typing import Dict, List, Tuple, Optional, Any
import logging
from pathlib import Path

logger = logging.getLogger(__name__)

# Check for explainability libraries
try:
    import shap
    HAS_SHAP = True
except ImportError:
    HAS_SHAP = False
    logger.warning("SHAP not installed. Install with: pip install shap")

try:
    import lime
    import lime.lime_tabular
    HAS_LIME = True
except ImportError:
    HAS_LIME = False
    logger.warning("LIME not installed. Install with: pip install lime")


class SHAPExplainer:
    """
    SHAP-based model explainability.
    Provides feature importance and prediction explanations.
    """
    
    def __init__(self, model: Any, feature_names: List[str] = None):
        self.model = model
        self.feature_names = feature_names
        self.explainer = None
        self.background_data = None
        
    def fit(self, background_data: np.ndarray, sample_size: int = 100):
        """Initialize explainer with background data."""
        if not HAS_SHAP:
            logger.warning("SHAP not available")
            return
        
        # Sample background data
        if len(background_data) > sample_size:
            indices = np.random.choice(len(background_data), sample_size, replace=False)
            self.background_data = background_data[indices]
        else:
            self.background_data = background_data
        
        # Create explainer
        try:
            # Try TreeExplainer for tree-based models
            self.explainer = shap.TreeExplainer(self.model)
            logger.info("Using TreeExplainer")
        except Exception:
            try:
                # Fall back to KernelExplainer
                self.explainer = shap.KernelExplainer(
                    self.model.predict_proba if hasattr(self.model, 'predict_proba') else self.model.predict,
                    self.background_data
                )
                logger.info("Using KernelExplainer")
            except Exception as e:
                logger.error(f"Failed to create SHAP explainer: {e}")
    
    def explain_prediction(
        self,
        features: np.ndarray,
        class_index: int = None
    ) -> Dict:
        """
        Explain a single prediction.
        
        Returns:
            Dictionary with feature importances and base value
        """
        if self.explainer is None:
            return self._fallback_explanation(features)
        
        try:
            shap_values = self.explainer.shap_values(features.reshape(1, -1))
            
            # Handle multi-class
            if isinstance(shap_values, list):
                if class_index is not None:
                    values = shap_values[class_index][0]
                else:
                    values = shap_values[1][0]  # Default to positive class
            else:
                values = shap_values[0]
            
            # Get feature importance ranking
            importance = np.abs(values)
            sorted_idx = np.argsort(importance)[::-1]
            
            top_features = []
            for idx in sorted_idx[:10]:  # Top 10 features
                if self.feature_names and idx < len(self.feature_names):
                    name = self.feature_names[idx]
                else:
                    name = f"Feature_{idx}"
                
                top_features.append({
                    'feature': name,
                    'importance': float(importance[idx]),
                    'contribution': float(values[idx]),
                    'direction': 'positive' if values[idx] > 0 else 'negative'
                })
            
            return {
                'top_features': top_features,
                'base_value': float(self.explainer.expected_value[0] if isinstance(self.explainer.expected_value, np.ndarray) else self.explainer.expected_value),
                'total_contribution': float(np.sum(values))
            }
            
        except Exception as e:
            logger.error(f"SHAP explanation failed: {e}")
            return self._fallback_explanation(features)
    
    def get_global_importance(self, X: np.ndarray) -> pd.DataFrame:
        """Get global feature importance across dataset."""
        if self.explainer is None:
            return pd.DataFrame()
        
        try:
            shap_values = self.explainer.shap_values(X)
            
            if isinstance(shap_values, list):
                values = shap_values[1]  # Positive class
            else:
                values = shap_values
            
            importance = np.abs(values).mean(axis=0)
            
            df = pd.DataFrame({
                'feature': self.feature_names if self.feature_names else [f'Feature_{i}' for i in range(len(importance))],
                'importance': importance
            })
            
            return df.sort_values('importance', ascending=False)
            
        except Exception as e:
            logger.error(f"Global importance failed: {e}")
            return pd.DataFrame()
    
    def _fallback_explanation(self, features: np.ndarray) -> Dict:
        """Simple fallback when SHAP unavailable."""
        # Use feature magnitudes as proxy
        importance = np.abs(features)
        sorted_idx = np.argsort(importance)[::-1]
        
        top_features = []
        for idx in sorted_idx[:10]:
            if self.feature_names and idx < len(self.feature_names):
                name = self.feature_names[idx]
            else:
                name = f"Feature_{idx}"
            
            top_features.append({
                'feature': name,
                'importance': float(importance[idx]),
                'contribution': float(features[idx]),
                'direction': 'positive' if features[idx] > 0 else 'negative'
            })
        
        return {
            'top_features': top_features,
            'base_value': 0.0,
            'total_contribution': 0.0,
            'note': 'Fallback explanation (SHAP unavailable)'
        }


class LIMEExplainer:
    """
    LIME-based model explainability.
    Provides local interpretable model-agnostic explanations.
    """
    
    def __init__(
        self,
        model: Any,
        feature_names: List[str] = None,
        class_names: List[str] = None
    ):
        self.model = model
        self.feature_names = feature_names
        self.class_names = class_names or ['Away', 'Draw', 'Home']
        self.explainer = None
        
    def fit(self, training_data: np.ndarray):
        """Initialize LIME explainer with training data."""
        if not HAS_LIME:
            logger.warning("LIME not available")
            return
        
        self.explainer = lime.lime_tabular.LimeTabularExplainer(
            training_data,
            feature_names=self.feature_names,
            class_names=self.class_names,
            mode='classification'
        )
        logger.info("LIME explainer initialized")
    
    def explain_prediction(
        self,
        features: np.ndarray,
        num_features: int = 10
    ) -> Dict:
        """
        Explain a single prediction using LIME.
        """
        if self.explainer is None:
            return self._fallback_explanation(features)
        
        try:
            # Get prediction function
            if hasattr(self.model, 'predict_proba'):
                predict_fn = self.model.predict_proba
            else:
                predict_fn = lambda x: self.model.predict(x)
            
            explanation = self.explainer.explain_instance(
                features,
                predict_fn,
                num_features=num_features
            )
            
            # Extract feature contributions
            feature_weights = explanation.as_list()
            
            top_features = []
            for feature_desc, weight in feature_weights:
                top_features.append({
                    'feature': feature_desc,
                    'importance': abs(weight),
                    'contribution': weight,
                    'direction': 'positive' if weight > 0 else 'negative'
                })
            
            return {
                'top_features': top_features,
                'local_prediction': explanation.local_pred[0] if hasattr(explanation, 'local_pred') else None,
                'score': explanation.score
            }
            
        except Exception as e:
            logger.error(f"LIME explanation failed: {e}")
            return self._fallback_explanation(features)
    
    def _fallback_explanation(self, features: np.ndarray) -> Dict:
        """Simple fallback when LIME unavailable."""
        importance = np.abs(features)
        sorted_idx = np.argsort(importance)[::-1]
        
        top_features = []
        for idx in sorted_idx[:10]:
            if self.feature_names and idx < len(self.feature_names):
                name = self.feature_names[idx]
            else:
                name = f"Feature_{idx}"
            
            top_features.append({
                'feature': name,
                'importance': float(importance[idx]),
                'contribution': float(features[idx]),
                'direction': 'positive' if features[idx] > 0 else 'negative'
            })
        
        return {
            'top_features': top_features,
            'note': 'Fallback explanation (LIME unavailable)'
        }


class PredictionExplainer:
    """
    Combined explainability system using SHAP and LIME.
    Provides comprehensive prediction explanations.
    """
    
    def __init__(
        self,
        model: Any,
        feature_names: List[str] = None,
        use_shap: bool = True,
        use_lime: bool = True
    ):
        self.model = model
        self.feature_names = feature_names
        
        self.shap_explainer = None
        self.lime_explainer = None
        
        if use_shap and HAS_SHAP:
            self.shap_explainer = SHAPExplainer(model, feature_names)
        
        if use_lime and HAS_LIME:
            self.lime_explainer = LIMEExplainer(model, feature_names)
    
    def fit(self, training_data: np.ndarray):
        """Initialize all explainers."""
        if self.shap_explainer:
            self.shap_explainer.fit(training_data)
        
        if self.lime_explainer:
            self.lime_explainer.fit(training_data)
    
    def explain(
        self,
        features: np.ndarray,
        prediction: Dict = None
    ) -> Dict:
        """
        Generate comprehensive explanation for a prediction.
        """
        result = {
            'prediction': prediction,
            'feature_values': {}
        }
        
        # Add feature values
        if self.feature_names:
            for i, name in enumerate(self.feature_names[:20]):  # Top 20
                if i < len(features):
                    result['feature_values'][name] = float(features[i])
        
        # SHAP explanation
        if self.shap_explainer:
            result['shap'] = self.shap_explainer.explain_prediction(features)
        
        # LIME explanation
        if self.lime_explainer:
            result['lime'] = self.lime_explainer.explain_prediction(features)
        
        # Generate human-readable summary
        result['summary'] = self._generate_summary(result)
        
        return result
    
    def _generate_summary(self, explanation: Dict) -> str:
        """Generate human-readable summary."""
        summary_parts = []
        
        if 'shap' in explanation and explanation['shap'].get('top_features'):
            top_3 = explanation['shap']['top_features'][:3]
            
            positive_factors = [f['feature'] for f in top_3 if f['direction'] == 'positive']
            negative_factors = [f['feature'] for f in top_3 if f['direction'] == 'negative']
            
            if positive_factors:
                summary_parts.append(f"Key positive factors: {', '.join(positive_factors)}")
            if negative_factors:
                summary_parts.append(f"Key negative factors: {', '.join(negative_factors)}")
        
        return '. '.join(summary_parts) if summary_parts else "No explanation available"


# Global instance
_explainer: Optional[PredictionExplainer] = None


def get_explainer(model: Any = None, feature_names: List[str] = None) -> PredictionExplainer:
    """Get or create prediction explainer."""
    global _explainer
    
    if _explainer is None and model is not None:
        _explainer = PredictionExplainer(model, feature_names)
    
    return _explainer


def explain_prediction(features: np.ndarray, prediction: Dict = None, model: Any = None) -> Dict:
    """Quick function to explain a prediction."""
    explainer = get_explainer(model)
    
    if explainer:
        return explainer.explain(features, prediction)
    
    return {'error': 'No explainer available'}