Skip to content

Commit 930191f

Browse files
0.30.11
documentation in metrics udated
1 parent 8265f57 commit 930191f

3 files changed

Lines changed: 50 additions & 29 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.30.10"
10+
version = "0.30.11"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/light/trainmodel.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,6 @@ def train_model_xai(config: dict, fun_control: dict, timestamp: bool = True) ->
726726

727727
result_xai = calculate_xai_consistency(attributions)
728728

729-
730-
731729
# -------------------------------------------------------------------------------------------------------------------
732730

733731
return result["val_loss"], result_xai
734-
735-

src/spotpython/utils/metrics.py

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,53 @@ def get_metric_sign(metric_name):
198198
raise ValueError(f"Metric '{metric_name}' not found.")
199199

200200

201+
def calculate_xai_consistency(attributions) -> float:
202+
"""Calculate the consistency between different XAI methods.
203+
Computes the pairwise correlation between different XAI methods' attributions
204+
and returns their mean correlation as a measure of consistency. A higher value
205+
indicates greater agreement between different XAI methods.
201206
202-
def calculate_xai_consistency(attributions):
203-
"""
204-
Calculates the consistency of XAI methods by computing the mean of the upper triangle
205-
of the correlation matrix of the provided attributions.
206-
207-
Args:
208-
attributions (np.ndarray): Array of shape (n_methods, n_features) containing
209-
the attributions from different XAI methods.
210-
211-
Returns:
212-
float: Mean value of the upper triangle of the correlation matrix.
213-
"""
214-
global_attr_np = np.array(attributions)
215-
corr_matrix = np.corrcoef(global_attr_np)
216-
print("Attribution Correlation Matrix:")
217-
print(corr_matrix)
218-
219-
# Calculate the mean of the upper triangle of the correlation matrix
220-
upper_triangle_indices = np.triu_indices_from(corr_matrix, k=1)
221-
upper_triangle_values = corr_matrix[upper_triangle_indices]
222-
result_xai = upper_triangle_values.mean()
223-
print("XAI Consistency (mean of upper triangle of correlation matrix):")
224-
print(result_xai)
225-
return result_xai
207+
Args:
208+
attributions (np.ndarray): Array of shape (n_methods, n_features) containing
209+
feature importance scores from different XAI methods. Each row represents
210+
a different XAI method's attributions, and each column represents a feature.
211+
212+
Returns:
213+
float: Mean correlation between XAI methods, ranging from -1 to 1.
214+
- 1: Perfect consistency between methods
215+
- 0: No consistency between methods
216+
- -1: Perfect negative consistency between methods
217+
218+
Examples:
219+
>>> import numpy as np
220+
>>> # Three XAI methods' attributions for four features
221+
>>> attributions = np.array([
222+
... [0.1, 0.2, 0.3, 0.4], # Method 1
223+
... [0.2, 0.3, 0.4, 0.5], # Method 2
224+
... [0.0, 0.1, 0.2, 0.3] # Method 3
225+
... ])
226+
>>> consistency = calculate_xai_consistency(attributions)
227+
>>> print(f"XAI Consistency: {consistency:.2f}")
228+
Attribution Correlation Matrix:
229+
[[ 1. 0.97 0.98]
230+
[ 0.97 1. 0.99]
231+
[ 0.98 0.99 1. ]]
232+
XAI Consistency: 0.98
233+
234+
Note:
235+
The correlation matrix is computed using numpy's corrcoef function, which
236+
calculates Pearson correlation coefficients. Only the upper triangle of
237+
the correlation matrix is used to avoid counting correlations twice.
238+
"""
239+
global_attr_np = np.array(attributions)
240+
corr_matrix = np.corrcoef(global_attr_np)
241+
print("Attribution Correlation Matrix:")
242+
print(corr_matrix)
243+
244+
# Calculate the mean of the upper triangle of the correlation matrix
245+
upper_triangle_indices = np.triu_indices_from(corr_matrix, k=1)
246+
upper_triangle_values = corr_matrix[upper_triangle_indices]
247+
result_xai = upper_triangle_values.mean()
248+
print("XAI Consistency (mean of upper triangle of correlation matrix):")
249+
print(result_xai)
250+
return result_xai

0 commit comments

Comments
 (0)