Binary measures functions
Binary measures functions
(->f-beta beta)f-beta score creator, returns f-beta measure function for given beta.
beta only appears squared in the formula, so its sign doesn't matter --
beta and -beta produce the same measure function. See also f1-score
(the beta=1 case) and ->f-inv-beta (the reciprocal).
f-beta score creator, returns f-beta measure function for given `beta`. `beta` only appears squared in the formula, so its sign doesn't matter -- `beta` and `-beta` produce the same measure function. See also [[f1-score]] (the `beta=1` case) and [[->f-inv-beta]] (the reciprocal).
(->f-inv-beta beta)Inversed f-beta score creator, returns inversed f-beta measure function for given beta
Inversed f-beta score creator, returns inversed f-beta measure function for given `beta`
(adj-f-score {:keys [tp fp fn tn]})(adj-f-score tp fp fn tn)Adjusted f-score, agf
Adjusted f-score, agf
(agf {:keys [tp fp fn tn]})(agf tp fp fn tn)Adjusted f-score, agf
Adjusted f-score, agf
(binary-confusion t p)Assign category to a pair of true and predicted values.
Returned category is one of the following:
:tp - true positive:fn - false negative:fp - false positive:tn - true negativeAssign category to a pair of true and predicted values. Returned category is one of the following: * `:tp` - true positive * `:fn` - false negative * `:fp` - false positive * `:tn` - true negative
(binary-measures-all-calc {:keys [tp fp fn tn]
:or {tp 0.0 fp 0.0 fn 0.0 tn 0.0}})Calculates the full battery of binary classification measures from a confusion matrix in a single pass.
This is a bulk, self-contained calculation of nearly every measure also available
individually (accuracy, precision, mcc, kappa, etc., see measures) plus
a few convenience keys not exposed as standalone functions (:cp/:cn/:pcp/:pcn
as aliases of :p/:n/:pp/:pn, :f-measure as an alias of :f1-score, and
:f-beta, an f-beta score function of one argument beta).
Parameters:
confusion-matrix (map): A map with :tp, :fp, :fn, :tn keys (numbers,
missing keys default to 0.0), such as one returned by infer-confusion-matrix.Returns a map containing every key in measures plus :cp, :cn, :pcp, :pcn,
:f-measure, :f-beta.
Individual measures follow their own function's corner-case behavior, e.g. :mcc/:phi
are ##NaN when their denominator is zero (see mcc).
See also measures (individual measure functions), binary-measures-thr
(the same set of measures swept across score thresholds).
Calculates the full battery of binary classification measures from a confusion matrix in a single pass. This is a bulk, self-contained calculation of nearly every measure also available individually (`accuracy`, `precision`, `mcc`, `kappa`, etc., see [[measures]]) plus a few convenience keys not exposed as standalone functions (`:cp`/`:cn`/`:pcp`/`:pcn` as aliases of `:p`/`:n`/`:pp`/`:pn`, `:f-measure` as an alias of `:f1-score`, and `:f-beta`, an f-beta score function of one argument `beta`). Parameters: - `confusion-matrix` (map): A map with `:tp`, `:fp`, `:fn`, `:tn` keys (numbers, missing keys default to `0.0`), such as one returned by [[infer-confusion-matrix]]. Returns a map containing every key in [[measures]] plus `:cp`, `:cn`, `:pcp`, `:pcn`, `:f-measure`, `:f-beta`. Individual measures follow their own function's corner-case behavior, e.g. `:mcc`/`:phi` are `##NaN` when their denominator is zero (see [[mcc]]). See also [[measures]] (individual measure functions), [[binary-measures-thr]] (the same set of measures swept across score thresholds).
(binary-measures-thr labels scores)(binary-measures-thr labels scores true-value)Calculates binary classification measures at every distinct score threshold, for building ROC/PR-style curves.
For each distinct value in scores, treats it as a decision threshold (predict
positive when score >= threshold) and computes the resulting confusion matrix and
derived measures. Observations that share the same score are treated as tied and
distributed evenly within that threshold's step (fractional :tp/:fp/:fn/:tn
counts can occur as a result).
Parameters:
labels (sequence): The true class labels, converted via binary-process-list
(see true-value below).scores (sequence of numbers): A score per label; higher scores are predicted
positive at lower thresholds. Must be the same length as labels.true-value (optional): Passed through to binary-process-list to determine
which label values count as true.Returns a map with :p, :n, :total, :prevalence (scalars) and per-threshold
sequences (one entry per distinct score, from highest to lowest threshold) for
:thr, :tp, :fp, :fn, :tn, and every rate/score measure (:accuracy,
:error, :tpr/:recall/:sensitivity, :fnr/:miss-rate, :fpr/:fall-out,
:tnr/:specificity, :ppv/:precision, :fdr, :for, :npv, :mcc,
:f1-score, :kappa, :fm, :ts/:jaccard).
At the two ends of the threshold sweep, some of these sequences hit a 0/0
division; those boundary values are guarded to the nearest well-defined value
(e.g. :ppv/:fdr at the highest threshold, :for/:npv at the lowest) rather
than left as ##NaN, matching R precrec's convention for the same case.
See also binary-measures-all-calc (the same measures at a single, fixed
confusion matrix rather than swept across thresholds).
Calculates binary classification measures at every distinct score threshold, for building ROC/PR-style curves. For each distinct value in `scores`, treats it as a decision threshold (predict positive when score >= threshold) and computes the resulting confusion matrix and derived measures. Observations that share the same score are treated as tied and distributed evenly within that threshold's step (fractional `:tp`/`:fp`/`:fn`/`:tn` counts can occur as a result). Parameters: - `labels` (sequence): The true class labels, converted via [[binary-process-list]] (see `true-value` below). - `scores` (sequence of numbers): A score per label; higher scores are predicted positive at lower thresholds. Must be the same length as `labels`. - `true-value` (optional): Passed through to [[binary-process-list]] to determine which label values count as `true`. Returns a map with `:p`, `:n`, `:total`, `:prevalence` (scalars) and per-threshold sequences (one entry per distinct score, from highest to lowest threshold) for `:thr`, `:tp`, `:fp`, `:fn`, `:tn`, and every rate/score measure (`:accuracy`, `:error`, `:tpr`/`:recall`/`:sensitivity`, `:fnr`/`:miss-rate`, `:fpr`/`:fall-out`, `:tnr`/`:specificity`, `:ppv`/`:precision`, `:fdr`, `:for`, `:npv`, `:mcc`, `:f1-score`, `:kappa`, `:fm`, `:ts`/`:jaccard`). At the two ends of the threshold sweep, some of these sequences hit a `0/0` division; those boundary values are guarded to the nearest well-defined value (e.g. `:ppv`/`:fdr` at the highest threshold, `:for`/`:npv` at the lowest) rather than left as `##NaN`, matching R `precrec`'s convention for the same case. See also [[binary-measures-all-calc]] (the same measures at a single, fixed confusion matrix rather than swept across thresholds).
(binary-process-list xs)(binary-process-list xs true-value)Convert binary labels to a true/false values.
Optional true-value when is:
nil - if labels are numbers, all non-zero values are treated as true. Otherwise each label is coerced with boolean (nil/false become false, anything else becomes true).false/nil for false.Convert binary labels to a true/false values. Optional `true-value` when is: * `nil` - if labels are numbers, all non-zero values are treated as true. Otherwise each label is coerced with `boolean` (`nil`/`false` become `false`, anything else becomes `true`). * a sequence - all values in the sequence are treated as true. * a function - function is used to map true values, function should return true value (any value) and `false`/`nil` for false. * a value - just a single value.
(bm {:keys [tp fp fn tn]})(bm tp fp fn tn)Bookmaker informedness, BM
Bookmaker informedness, BM
(csi {:keys [tp fp fn tn]})(csi tp fp fn tn)Jaccard index, threat score, TS, critical success index CSI
Jaccard index, threat score, TS, critical success index CSI
(deltaP {:keys [tp fp fn tn]})(deltaP tp fp fn tn)Markedness, MK, deltaP
Markedness, MK, deltaP
(dor {:keys [tp fp fn tn]})(dor tp fp fn tn)Diagnostic odds ratio
Diagnostic odds ratio
(f1-score {:keys [tp fp fn tn]})(f1-score tp fp fn _tn)F1 score, the harmonic mean of precision and recall: 2*tp/(2*tp+fp+fn).
Equivalent to ->f-beta with beta=1. See also ->f-inv-beta.
F1 score, the harmonic mean of precision and recall: `2*tp/(2*tp+fp+fn)`. Equivalent to [[->f-beta]] with `beta=1`. See also [[->f-inv-beta]].
(fall-out {:keys [tp fp fn tn]})(fall-out _tp fp _fn tn)False positive rate, FPR, fall-out
False positive rate, FPR, fall-out
(fdr {:keys [tp fp fn tn]})(fdr tp fp fn tn)False discovery rate
False discovery rate
(fm {:keys [tp fp fn tn]})(fm tp fp fn tn)Fowlkes-Mallows index, FM
Fowlkes-Mallows index, FM
(fnr {:keys [tp fp fn tn]})(fnr tp fp fn tn)False negative rate, FNR, miss-rate
False negative rate, FNR, miss-rate
(for {:keys [tp fp fn tn]})(for _tp _fp fn tn)False omission rate, FOR
False omission rate, FOR
(fpr {:keys [tp fp fn tn]})(fpr tp fp fn tn)False positive rate, FPR, fall-out
False positive rate, FPR, fall-out
(gmean {:keys [tp fp fn tn]})(gmean tp fp fn tn)Geometric mean score, sqrt(specificity * recall)
Geometric mean score, sqrt(specificity * recall)
(hit-rate {:keys [tp fp fn tn]})(hit-rate tp fp fn tn)True positive rate, TPR, recall, sensitivity, hit-rate
True positive rate, TPR, recall, sensitivity, hit-rate
(infer-confusion-matrix confusion-matrix)Construct a confusion matrix from the input.
Returns a map with :tp, :fn, :fp and :tn keys.
Input can be one of:
Construct a confusion matrix from the input. Returns a map with `:tp`, `:fn`, `:fp` and `:tn` keys. Input can be one of: * a map with keys [:t :p], [:t :n], [:f :p] and [:f :n] * pair of pairs [[tp fn] [fp tn]] * a sequence of values [tp fn fp tn] * a confusion matrix
(informedness {:keys [tp fp fn tn]})(informedness tp fp fn tn)Bookmaker informedness, BM
Bookmaker informedness, BM
(jaccard {:keys [tp fp fn tn]})(jaccard tp fp fn _tn)Jaccard index, threat score, TS, critical success index CSI
Jaccard index, threat score, TS, critical success index CSI
(lr+ {:keys [tp fp fn tn]})(lr+ tp fp fn tn)Positive likelihood ratio
Positive likelihood ratio
(lr- {:keys [tp fp fn tn]})(lr- tp fp fn tn)Negative likelihood ratio
Negative likelihood ratio
(markedness {:keys [tp fp fn tn]})(markedness tp fp fn tn)Markedness, MK, deltaP
Markedness, MK, deltaP
(mcc {:keys [tp fp fn tn]})(mcc tp fp fn tn)Matthews correlation coefficient, MCC, phi
Returns ##NaN when any of tp+fp, tp+fn, tn+fp, tn+fn is zero (the
denominator is undefined), matching R's metrica::mcc convention. Note this
differs from scikit-learn's matthews_corrcoef, which returns 0.0 (with a
warning) in that case.
Matthews correlation coefficient, MCC, phi Returns `##NaN` when any of `tp+fp`, `tp+fn`, `tn+fp`, `tn+fn` is zero (the denominator is undefined), matching R's `metrica::mcc` convention. Note this differs from scikit-learn's `matthews_corrcoef`, which returns `0.0` (with a warning) in that case.
Map from measure keyword to its corresponding function.
Every value is a function accepting either a confusion-matrix map (:tp/:fp/:fn/:tn
keys) or the four counts as separate arguments (tp fp fn tn), returning a double.
Useful for looking up a measure function by name, e.g. ((measures :mcc) confusion-matrix).
See also binary-measures-all-calc (computes every measure in one pass) and
infer-confusion-matrix (builds the confusion-matrix map this expects).
Map from measure keyword to its corresponding function. Every value is a function accepting either a confusion-matrix map (`:tp`/`:fp`/`:fn`/`:tn` keys) or the four counts as separate arguments (`tp fp fn tn`), returning a `double`. Useful for looking up a measure function by name, e.g. `((measures :mcc) confusion-matrix)`. See also [[binary-measures-all-calc]] (computes every measure in one pass) and [[infer-confusion-matrix]] (builds the confusion-matrix map this expects).
(miss-rate {:keys [tp fp fn tn]})(miss-rate tp fp fn tn)False negative rate, FNR, miss-rate
False negative rate, FNR, miss-rate
(mk {:keys [tp fp fn tn]})(mk tp fp fn tn)Markedness, MK, deltaP
Markedness, MK, deltaP
(npv {:keys [tp fp fn tn]})(npv tp fp fn tn)Negative predictive value
Negative predictive value
(phi {:keys [tp fp fn tn]})(phi tp fp fn tn)Matthews correlation coefficient, MCC, phi. Alias of mcc -- see its
docstring for the ##NaN-on-degenerate-input behavior.
Matthews correlation coefficient, MCC, phi. Alias of [[mcc]] -- see its docstring for the `##NaN`-on-degenerate-input behavior.
(pn {:keys [tp fp fn tn]})(pn _tp _fp fn tn)Predicted negative
Predicted negative
(pp {:keys [tp fp fn tn]})(pp tp fp _fn _tn)Predicted positive
Predicted positive
(ppv {:keys [tp fp fn tn]})(ppv tp fp fn tn)Precision, positive predictive value, PPV
Precision, positive predictive value, PPV
(precision {:keys [tp fp fn tn]})(precision tp fp _fn _tn)Precision, positive predictive value, PPV
Precision, positive predictive value, PPV
(prevalence {:keys [tp fp fn tn]})(prevalence tp fp fn tn)Prevalence, p/(p+n)
Prevalence, p/(p+n)
(pt {:keys [tp fp fn tn]})(pt tp fp fn tn)Prevalence threshold
Prevalence threshold
(recall {:keys [tp fp fn tn]})(recall tp _fp fn _tn)True positive rate, TPR, recall, sensitivity, hit-rate
True positive rate, TPR, recall, sensitivity, hit-rate
(selectivity {:keys [tp fp fn tn]})(selectivity tp fp fn tn)True negative rate, TNR, specificity, selectivity
True negative rate, TNR, specificity, selectivity
(sensitivity {:keys [tp fp fn tn]})(sensitivity tp fp fn tn)True positive rate, TPR, recall, sensitivity, hit-rate
True positive rate, TPR, recall, sensitivity, hit-rate
(specificity {:keys [tp fp fn tn]})(specificity tp fp fn tn)True negative rate, TNR, specificity, selectivity
True negative rate, TNR, specificity, selectivity
(tnr {:keys [tp fp fn tn]})(tnr tp fp fn tn)True negative rate, TNR, specificity, selectivity
True negative rate, TNR, specificity, selectivity
(tpr {:keys [tp fp fn tn]})(tpr tp fp fn tn)True positive rate, TPR, recall, sensitivity, hit-rate
True positive rate, TPR, recall, sensitivity, hit-rate
(ts {:keys [tp fp fn tn]})(ts tp fp fn tn)Jaccard index, threat score, TS, critical success index CSI
Jaccard index, threat score, TS, critical success index CSI
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |