-
Notifications
You must be signed in to change notification settings - Fork 9
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
ENH: calculating d-prime from confusion matrices and samples #8
Open
hahong
wants to merge
56
commits into
npinto:master
Choose a base branch
from
dicarlolab:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
340f5d3
MISC: updating .gitignore
hahong fa13692
ENH: added d-prime calculation from a confusion matrix
hahong 9db9829
Merge branch 'master' of https://github.com/npinto/bangmetric
hahong 547d490
Merge branch 'feature_dprime'
hahong 0b00116
MISC: small cosmetics changes and assertions to check positives and n…
hahong 69f0974
ENH: added d-prime calcualtion function that directly takes sample va…
hahong 43cabf5
MISC: small chanages for 2x2 confusion matrix d' calculation
hahong 5f8f071
MISC: no need to "balance" data for d' calculation
hahong a056d02
Merge branch 'feature_dprime'
hahong d6a9cf6
MISC: addressing most stuffs in github.com/npinto/bangmetric/pull/8 (…
hahong 6f5cbac
DOC: small retouches
hahong afa86fe
COSMIT
hahong 8babb28
COSMIT
hahong d6ab20b
ENH: more general dprime_from_confusion (thanks, @npinto!)
hahong 3f5eb03
Merge branch 'feature_dprime'
hahong 60814d8
COSMIT
hahong 1d926a2
Merge branch 'feature_dprime'
hahong 056aa5e
ENH: refactoring out a function that computes stats of a confu matrix.
hahong 396224b
COSMIT: refactoring confusion matrix handling part
hahong dd59101
Merge branch 'feature_utils' into feature_dprime
hahong c2f53ee
Merge branch 'feature_dprime'
hahong ad8e3af
COSMIT
hahong 1339ae2
Merge branch 'feature_utils' into feature_dprime
hahong b1d8b77
DOC: small changes
hahong 99f5354
Merge branch 'feature_utils' into feature_dprime
hahong b0d58c1
DOC: small changes
hahong b28f6f3
Merge branch 'feature_utils' into feature_dprime
hahong 0deae47
Merge branch 'feature_dprime'
hahong 15295c5
COSMIT: combined dprime() and dprime_from_samp()
hahong 69f89ec
COSMIT
hahong f515857
Merge branch 'feature_utils' into feature_dprime
hahong 341d29a
COSMIT
hahong 6d48df3
Merge branch 'feature_dprime'
hahong de48e46
MISC: small errors and cosmetic changes
hahong cad170b
MISC: merge dprime_from_confusion_matrix and dprime
hahong 7c47499
Merge branch 'feature_dprime'
hahong f0a4f1b
DOC: small changes
hahong 2df5287
Merge branch 'master' into feature_utils
hahong 5b07e4c
COSMIT
hahong 962885b
Merge branch 'feature_utils'
hahong 0748c3a
ENH: added metrics for human data
hahong 75e3679
Merge branch 'feature_humans'
hahong 95ed1fc
COSMIT
hahong 8b2f3e6
Merge branch 'feature_humans'
hahong ab6df56
ENH: added confusion matrix support to accuracy()
hahong 608f869
Merge branch 'feature_machlearning'
hahong b1dedff
DOC: misc changes
hahong a5357fc
Merge branch 'feature_dprime'
hahong 2e34b76
TST: fixed bugs in reference value
hahong dbd5326
Merge branch 'feature_dprime'
hahong f3fd043
MISC: small changes to clip ppf values in dprime()
hahong a198d44
Merge branch 'feature_dprime'
hahong b58a0fe
DOC: small typos..
hahong e614a6f
Merge branch 'feature_humans'
hahong 9f0cbd7
fixed a bug: np.sort makes a copy while array.sort is inplace
cadieu d575111
updated installation
qbilius 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 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 |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
__pycache__ | ||
.idea | ||
build | ||
*.DS_Store | ||
*~ | ||
.*swp |
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# bangmetric | ||
|
||
# License | ||
|
||
New BSD |
This file contains 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 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 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 |
---|---|---|
|
@@ -2,61 +2,176 @@ | |
|
||
# Authors: Nicolas Pinto <[email protected]> | ||
# Nicolas Poilvert <[email protected]> | ||
# Ha Hong <[email protected]> | ||
# | ||
# License: BSD | ||
|
||
__all__ = ['dprime'] | ||
|
||
import numpy as np | ||
from scipy.stats import norm | ||
from .utils import confusion_matrix_stats | ||
|
||
DEFAULT_DPRIME_MODE = 'binary' | ||
|
||
def dprime(y_pred, y_true): | ||
"""Computes the d-prime sensitivity index of the predictions. | ||
|
||
def dprime(A, B=None, mode=DEFAULT_DPRIME_MODE,\ | ||
max_value=np.inf, min_value=-np.inf,\ | ||
max_ppf_value=np.inf, min_ppf_value=-np.inf,\ | ||
**kwargs): | ||
"""Computes the d-prime sensitivity index of predictions | ||
from various data formats. Depending on the choice of | ||
`mode`, this function can take one of the following format: | ||
|
||
* Binary classification outputs (`mode='binary'`; default) | ||
* Positive and negative samples (`mode='sample'`) | ||
* True positive and false positive rate (`mode='rate'`) | ||
* Confusion matrix (`mode='confusionmat'`) | ||
|
||
Parameters | ||
---------- | ||
y_true: array, shape = [n_samples] | ||
True values, interpreted as strictly positive or not | ||
(i.e. converted to binary). | ||
Could be in {-1, +1} or {0, 1} or {False, True}. | ||
A, B: | ||
If `mode` is 'binary' (default): | ||
|
||
A: array, shape = [n_samples], | ||
True values, interpreted as strictly positive or not | ||
(i.e. converted to binary). | ||
Could be in {-1, +1} or {0, 1} or {False, True}. | ||
|
||
B: array, shape = [n_samples], | ||
Predicted values (real). | ||
|
||
If `mode` is 'sample': | ||
|
||
A: array-like, | ||
Positive sample values (e.g., raw projection values | ||
of the positive classifier). | ||
|
||
B: array-like, | ||
Negative sample values. | ||
|
||
If `mode` is 'rate': | ||
|
||
A: array-like, shape = [n_groupings] | ||
True positive rates | ||
|
||
B: array-like, shape = [n_groupings] | ||
False positive rates | ||
|
||
if `mode` is 'confusionmat': | ||
|
||
A: array-like, shape = [n_classes (true), n_classes (pred)] | ||
Confusion matrix, where the element M_{rc} means | ||
the number of times when the classifier or subject | ||
guesses that a test sample in the r-th class | ||
belongs to the c-th class. | ||
|
||
B: ignored | ||
|
||
mode: {'binary', 'sample', 'rate'}, optional, (default='binary') | ||
Directs the interpretation of A and B. | ||
|
||
max_value: float, optional (default=np.inf) | ||
Maximum possible d-prime value. | ||
|
||
min_value: float, optional (default=-np.inf) | ||
Minimum possible d-prime value. | ||
|
||
max_ppf_value: float, optional (default=np.inf) | ||
Maximum possible ppf value. | ||
Used only when mode is 'rate' or 'confusionmat'. | ||
|
||
min_ppf_value: float, optional (default=-np.inf). | ||
Minimum possible ppf value. | ||
Used only when mode is 'rate' or 'confusionmat'. | ||
|
||
y_pred: array, shape = [n_samples] | ||
Predicted values (real). | ||
kwargs: named arguments, optional | ||
Passed to ``confusion_matrix_stats()`` and used only when `mode` | ||
is 'confusionmat'. By assigning ``collation``, | ||
``fudge_mode``, ``fudge_factor``, etc. one can | ||
change the behavior of d-prime computation | ||
(see ``confusion_matrix_stats()`` for details). | ||
|
||
Returns | ||
------- | ||
dp: float or None | ||
d-prime, None if d-prime is undefined | ||
dp: float or array of shape = [n_groupings] | ||
A d-prime value or array of d-primes, where each element | ||
corresponds to each grouping of positives and negatives | ||
(when `mode` is 'rate' or 'confusionmat') | ||
|
||
References | ||
---------- | ||
http://en.wikipedia.org/wiki/D' | ||
http://en.wikipedia.org/wiki/Confusion_matrix | ||
""" | ||
|
||
# -- basic checks and conversion | ||
assert len(y_true) == len(y_pred) | ||
assert np.isfinite(y_true).all() | ||
assert np.isfinite(y_pred).all() | ||
|
||
y_true = np.array(y_true) | ||
assert y_true.ndim == 1 | ||
|
||
y_pred = np.array(y_pred) | ||
assert y_pred.ndim == 1 | ||
|
||
# -- actual computation | ||
pos = y_true > 0 | ||
neg = ~pos | ||
pos_mean = y_pred[pos].mean() | ||
neg_mean = y_pred[neg].mean() | ||
pos_var = y_pred[pos].var(ddof=1) | ||
neg_var = y_pred[neg].var(ddof=1) | ||
|
||
num = pos_mean - neg_mean | ||
div = np.sqrt((pos_var + neg_var) / 2.) | ||
if div == 0: | ||
dp = None | ||
if mode == 'sample': | ||
pos, neg = np.array(A), np.array(B) | ||
|
||
elif mode == 'binary': | ||
y_true, y_pred = A, B | ||
|
||
assert len(y_true) == len(y_pred) | ||
assert np.isfinite(y_true).all() | ||
|
||
y_true = np.array(y_true) | ||
assert y_true.ndim == 1 | ||
|
||
y_pred = np.array(y_pred) | ||
assert y_pred.ndim == 1 | ||
|
||
i_pos = y_true > 0 | ||
i_neg = ~i_pos | ||
|
||
pos = y_pred[i_pos] | ||
neg = y_pred[i_neg] | ||
|
||
elif mode == 'rate': | ||
TPR, FPR = np.array(A), np.array(B) | ||
assert TPR.shape == FPR.shape | ||
|
||
elif mode == 'confusionmat': | ||
# A: confusion mat | ||
# row means true classes, col means predicted classes | ||
P, N, TP, _, FP, _ = confusion_matrix_stats(A, **kwargs) | ||
|
||
TPR = TP / P | ||
FPR = FP / N | ||
|
||
else: | ||
raise ValueError('Invalid mode') | ||
|
||
# -- compute d' | ||
if mode in ['sample', 'binary']: | ||
assert np.isfinite(pos).all() | ||
assert np.isfinite(neg).all() | ||
|
||
if pos.size <= 1: | ||
raise ValueError('Not enough positive samples'\ | ||
'to estimate the variance') | ||
if neg.size <= 1: | ||
raise ValueError('Not enough negative samples'\ | ||
'to estimate the variance') | ||
|
||
pos_mean = pos.mean() | ||
neg_mean = neg.mean() | ||
pos_var = pos.var(ddof=1) | ||
neg_var = neg.var(ddof=1) | ||
|
||
num = pos_mean - neg_mean | ||
div = np.sqrt((pos_var + neg_var) / 2.) | ||
|
||
dp = num / div | ||
|
||
else: # mode is rate or confusionmat | ||
ppfTPR = norm.ppf(TPR) | ||
ppfFPR = norm.ppf(FPR) | ||
ppfTPR = np.clip(ppfTPR, min_ppf_value, max_ppf_value) | ||
ppfFPR = np.clip(ppfFPR, min_ppf_value, max_ppf_value) | ||
dp = ppfTPR - ppfFPR | ||
|
||
# from Dan's suggestion about clipping d' values... | ||
dp = np.clip(dp, min_value, max_value) | ||
|
||
return dp |
Oops, something went wrong.
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.
Hu? Why is accuracy changing here?
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.
I added a support for confusion matrices in
accuracy()
as indprime()
. There are some changes/rearrangements, so might be good to take a look at the whole code.