main.bend checks
raw source on the hub · import mlbend-metrics@0.1.0.0/main.bend as Main
mlbend-metrics: the evaluation surface of machine learning, natively in Bend 2.
import mlbend-metrics@0.1.0.0/main.bend as M
A metric compares two sequences of the same length. The length is not trusted: every metric answers a Result, and a mismatch of lengths (or an empty input) is a value, never a crash.
What is proved: the counts. What is tested: the F32 numbers, against NumPy and scikit-learn (reference/test_metrics.py). F32 is not a real number, so a metric's value is never proved, only its shape.
2 imports
import Base import bend-ml-nat-lemmas@0.1.1.0/main.bend as NL
Laws
law count_length provedsource · line 99 · raw
@ys:List<&2, Nat> -> {count(ys) == List.length(&2, Nat, ys) : Nat}LAW 1 (count_length): the number of samples is the length of the list of labels. (The count is this package's own walk, so the law pins it to the length Base counts.)
Types
type Bad source · line 21 · raw
Data
Mismatch@expected:Nat -> @got:Nat -> Bad
EmptyBad
type Sum source · line 118 · raw
Data
Sum@n:Nat -> @s:F32 -> Sum
Definitions
def same_of source · line 33 · raw
@s:Bool -> Nat
1 when a and b are the same label, 0 otherwise
def same source · line 40 · raw
@a:Nat -> @b:Nat -> Nat
def count source · line 44 · raw
@ys:List<&2, Nat> -> Nat
the number of samples
def count_true.go source · line 52 · raw
@ys:List<&2, Nat> -> @+k:Nat -> @n:Nat -> Nat
the number of samples whose true label is k
def count_true source · line 59 · raw
@ys:List<&2, Nat> -> @k:Nat -> Nat
def count_pred.go source · line 63 · raw
@ps:List<&2, Nat> -> @+k:Nat -> @n:Nat -> Nat
the number of samples whose predicted label is k
def count_pred source · line 70 · raw
@ps:List<&2, Nat> -> @k:Nat -> Nat
def count_both.go source · line 75 · raw
@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @+k:Nat -> @n:Nat -> Nat
the number of samples whose true label is k and whose predicted label is k too: the true positives of the class k
def count_both source · line 82 · raw
@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @k:Nat -> Nat
def count_right.go source · line 86 · raw
@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @n:Nat -> Nat
the number of samples predicted right
def count_right source · line 93 · raw
@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> Nat
def sum_add source · line 121 · raw
@a:Sum -> @x:F32 -> Sum
def sq_sum.go source · line 127 · raw
@ys:List<&2, F32> -> @ps:List<&2, F32> -> @acc:Sum -> Sum
the sum of the squared differences of the pairs walked
def abs_sum.go source · line 136 · raw
@ys:List<&2, F32> -> @ps:List<&2, F32> -> @acc:Sum -> Sum
the sum of the absolute differences of the pairs walked
def ratio_of source · line 152 · raw
@z:Bool -> @+a:Nat -> @+b:Nat -> F32
the ratio a over b, or 0.0 when b is zero
def ratio source · line 159 · raw
@+a:Nat -> @+b:Nat -> F32
def mean source · line 163 · raw
@s:Sum -> @n:Nat -> F32
the mean of a sum over n pairs
def same_len_of source · line 174 · raw
@s:Bool -> @+n:Nat -> @+m:Nat -> Result<&1, &1, Bad, Nat>
the number of pairs to walk, or why the two sequences do not agree
def same_len source · line 181 · raw
@+n:Nat -> @+m:Nat -> Result<&1, &1, Bad, Nat>
def not_empty_of source · line 185 · raw
@z:Bool -> @n:Nat -> Result<&1, &1, Bad, Nat>
the number of pairs to walk, or why there are none
def not_empty source · line 193 · raw
@r:Result<&1, &1, Bad, Nat> -> Result<&1, &1, Bad, Nat>
the number of pairs, or why there is no answer
def pairs source · line 203 · raw
@-a:Quant -> @-A:Kind(a) -> @ys:List<a, A> -> @ps:List<a, A> -> Result<&1, &1, Bad, Nat>
the number of pairs the two sequences hold, or why there is no answer
def mse.go source · line 207 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, F32> -> @+ps:List<&2, F32> -> Result<&1, &1, Bad, F32>
the mean squared error between the targets and the predictions
def mse source · line 216 · raw
@+ys:List<&2, F32> -> @+ps:List<&2, F32> -> Result<&1, &1, Bad, F32>
def mae.go source · line 220 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, F32> -> @+ps:List<&2, F32> -> Result<&1, &1, Bad, F32>
the mean absolute error between the targets and the predictions
def mae source · line 229 · raw
@+ys:List<&2, F32> -> @+ps:List<&2, F32> -> Result<&1, &1, Bad, F32>
def rmse.go source · line 233 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, F32> -> @+ps:List<&2, F32> -> Result<&1, &1, Bad, F32>
the root mean squared error between the targets and the predictions
def rmse source · line 242 · raw
@+ys:List<&2, F32> -> @+ps:List<&2, F32> -> Result<&1, &1, Bad, F32>
def accuracy.go source · line 246 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> Result<&1, &1, Bad, F32>
the fraction of the samples predicted right
def accuracy source · line 255 · raw
@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> Result<&1, &1, Bad, F32>
def precision.go source · line 260 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, Bad, F32>
the fraction of the samples predicted as k that are really k: the precision of the class k
def precision source · line 269 · raw
@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, Bad, F32>
def recall.go source · line 274 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, Bad, F32>
the fraction of the samples really k that are predicted as k: the recall of the class k
def recall source · line 283 · raw
@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, Bad, F32>
def f1_num_of source · line 287 · raw
@z:Bool -> @+p:F32 -> @+r:F32 -> F32
the harmonic mean of two scores
def f1_num source · line 294 · raw
@+p:F32 -> @+r:F32 -> F32
def f1_of source · line 298 · raw
@p:Result<&1, &1, Bad, F32> -> @r:Result<&1, &1, Bad, F32> -> Result<&1, &1, Bad, F32>
the harmonic mean of a precision and a recall
def f1.go source · line 314 · raw
@r:Result<&1, &1, Bad, Nat> -> @+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, Bad, F32>
the harmonic mean of the precision and the recall of the class k
def f1 source · line 323 · raw
@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, Bad, F32>