-
-
Notifications
You must be signed in to change notification settings - Fork 379
[GSK-1279] Fisher's exact test and permutation test for slice metrics significance testing #1671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6ea0ff8
[GSK-1279] Fisher's exact test and permutation test for slice metrics…
luca-rossi 238609f
[GSK-1279] Refactor: isolate p-value methods for easier testing
luca-rossi cf0e61f
[GSK-1279] Test statistical significance testing
luca-rossi 2240dd4
Merge branch 'main' into task/GSK-1279-statistical-tests
luca-rossi e68aeec
Merge branch 'main' into task/GSK-1279-statistical-tests
mattbit 895c693
[GSK-1279] Significance testing refactor and small fixes
luca-rossi 69c1159
Merge branch 'task/GSK-1279-statistical-tests' of https://github.com/…
luca-rossi c6a7c81
Merge branch 'main' into task/GSK-1279-statistical-tests
mattbit e3c7c9b
[GSK-1279] Minor fixes
luca-rossi badac09
Merge branch 'main' into task/GSK-1279-statistical-tests
mattbit 2a3e41d
Minor fix in the issue template (p-value formatting)
mattbit 1bb773e
Small fix to issue template (do not print significance if p-value is …
mattbit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ class MetricResult: | |
| value: float | ||
| affected_samples: int | ||
| raw_values: Optional[np.ndarray] = None | ||
| binary_counts: Optional[list[int]] = None | ||
|
|
||
| @property | ||
| def name(self): | ||
|
|
@@ -27,6 +28,7 @@ def __str__(self): | |
| class PerformanceMetric(ABC): | ||
| name: str | ||
| greater_is_better = True | ||
| has_binary_counts = False | ||
|
|
||
| @abstractmethod | ||
| def __call__(self, model: BaseModel, dataset: Dataset) -> MetricResult: | ||
|
|
@@ -43,7 +45,9 @@ def __call__(self, model: BaseModel, dataset: Dataset) -> MetricResult: | |
|
|
||
| value = self._calculate_metric(y_true, y_pred, model) | ||
| num_affected = self._calculate_affected_samples(y_true, y_pred, model) | ||
| return MetricResult(self, value, num_affected) | ||
| binary_counts = self._calculate_binary_counts(value, num_affected) if self.has_binary_counts else None | ||
|
|
||
| return MetricResult(self, value, num_affected, binary_counts=binary_counts) | ||
|
|
||
| @abstractmethod | ||
| def _calculate_metric(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel) -> MetricResult: | ||
|
|
@@ -52,10 +56,16 @@ def _calculate_metric(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseM | |
| def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel) -> int: | ||
| return len(y_true) | ||
|
|
||
| def _calculate_binary_counts(self, value, num_affected) -> list[int]: | ||
| x = round(value * num_affected) | ||
| y = num_affected - x | ||
| return [x, y] | ||
|
|
||
|
|
||
| class Accuracy(ClassificationPerformanceMetric): | ||
| name = "Accuracy" | ||
| greater_is_better = True | ||
| has_binary_counts = True | ||
|
|
||
| def _calculate_metric(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel): | ||
| return sklearn.metrics.accuracy_score(y_true, y_pred) | ||
|
|
@@ -64,6 +74,7 @@ def _calculate_metric(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseM | |
| class BalancedAccuracy(ClassificationPerformanceMetric): | ||
| name = "Balanced Accuracy" | ||
| greater_is_better = True | ||
| has_binary_counts = False | ||
|
|
||
| def _calculate_metric(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel): | ||
| return sklearn.metrics.balanced_accuracy_score(y_true, y_pred) | ||
|
|
@@ -89,6 +100,7 @@ def _calculate_metric(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseM | |
| class F1Score(SklearnClassificationScoreMixin, ClassificationPerformanceMetric): | ||
| name = "F1 Score" | ||
| greater_is_better = True | ||
| has_binary_counts = False | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
| _sklearn_metric = "f1_score" | ||
|
|
||
| def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel) -> int: | ||
|
|
@@ -104,6 +116,7 @@ def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, mo | |
| class Precision(SklearnClassificationScoreMixin, ClassificationPerformanceMetric): | ||
| name = "Precision" | ||
| greater_is_better = True | ||
| has_binary_counts = True | ||
| _sklearn_metric = "precision_score" | ||
|
|
||
| def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel) -> int: | ||
|
|
@@ -116,6 +129,7 @@ def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, mo | |
| class Recall(SklearnClassificationScoreMixin, ClassificationPerformanceMetric): | ||
| name = "Recall" | ||
| greater_is_better = True | ||
| has_binary_counts = True | ||
| _sklearn_metric = "recall_score" | ||
|
|
||
| def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, model: BaseModel) -> int: | ||
|
|
@@ -128,6 +142,7 @@ def _calculate_affected_samples(self, y_true: np.ndarray, y_pred: np.ndarray, mo | |
| class AUC(PerformanceMetric): | ||
| name = "ROC AUC" | ||
| greater_is_better = True | ||
| has_binary_counts = False | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
|
|
||
| def __call__(self, model: BaseModel, dataset: Dataset) -> MetricResult: | ||
| y_true = dataset.df[dataset.target] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed