optimize shap response latency
This commit is contained in:
@@ -23,7 +23,8 @@ class SHAPAnalyzer:
|
||||
self.feature_names = None
|
||||
self.selected_features = None
|
||||
self.label_encoders = {}
|
||||
self.background_data = None
|
||||
self.background_data = {}
|
||||
self.global_result_cache = {}
|
||||
self._initialized = False
|
||||
|
||||
def _ensure_initialized(self):
|
||||
@@ -85,8 +86,8 @@ class SHAPAnalyzer:
|
||||
|
||||
def _get_background_sample(self, n_samples=500):
|
||||
"""获取背景数据样本"""
|
||||
if self.background_data is not None:
|
||||
return self.background_data
|
||||
if n_samples in self.background_data:
|
||||
return self.background_data[n_samples]
|
||||
|
||||
try:
|
||||
from core.preprocessing import get_clean_data
|
||||
@@ -123,7 +124,7 @@ class SHAPAnalyzer:
|
||||
if selected_indices:
|
||||
X = X[:, selected_indices]
|
||||
|
||||
self.background_data = X
|
||||
self.background_data[n_samples] = X
|
||||
return X
|
||||
except Exception:
|
||||
return None
|
||||
@@ -151,12 +152,15 @@ class SHAPAnalyzer:
|
||||
if not SHAP_AVAILABLE:
|
||||
return {'error': 'SHAP library not installed'}
|
||||
|
||||
if model_type in self.global_result_cache:
|
||||
return self.global_result_cache[model_type]
|
||||
|
||||
self._ensure_initialized()
|
||||
explainer = self._get_tree_explainer(model_type)
|
||||
if explainer is None:
|
||||
return {'error': f'No tree model available for {model_type}'}
|
||||
|
||||
X = self._get_background_sample()
|
||||
X = self._get_background_sample(n_samples=32)
|
||||
if X is None:
|
||||
return {'error': 'Failed to prepare background data'}
|
||||
|
||||
@@ -215,11 +219,13 @@ class SHAPAnalyzer:
|
||||
'dimension': self._map_feature_to_dimension(fname),
|
||||
})
|
||||
|
||||
return {
|
||||
result = {
|
||||
'model_type': model_type,
|
||||
'dimensions': dimensions,
|
||||
'top_features': top_features,
|
||||
}
|
||||
self.global_result_cache[model_type] = result
|
||||
return result
|
||||
except Exception as exc:
|
||||
return {'error': str(exc)}
|
||||
|
||||
@@ -306,7 +312,7 @@ class SHAPAnalyzer:
|
||||
if explainer is None:
|
||||
return {'error': f'No tree model available for {model_type}'}
|
||||
|
||||
X = self._get_background_sample(n_samples=200)
|
||||
X = self._get_background_sample(n_samples=12)
|
||||
if X is None:
|
||||
return {'error': 'Failed to prepare background data'}
|
||||
|
||||
@@ -362,7 +368,7 @@ class SHAPAnalyzer:
|
||||
if explainer is None:
|
||||
return {'error': f'No tree model available for {model_type}'}
|
||||
|
||||
X = self._get_background_sample()
|
||||
X = self._get_background_sample(n_samples=24)
|
||||
if X is None:
|
||||
return {'error': 'Failed to prepare background data'}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user