~/bend-docscommunity

main.bend checks

raw source on the hub · import mlbend-report@0.1.0.0/main.bend as Main

mlbend-report: the confusion matrix and the per-class report, natively in Bend 2.

import mlbend-report@0.1.0.0/main.bend as R

What an estimator's predictions look like: the confusion matrix of a classification, and the per-class report scikit-learn's classification_report prints — the precision, the recall, the F1 and the support of every class, with the macro and the weighted averages. The labels are Nat: the class 0, the class 1, and so on up to k-1.

The per-class numbers rest on mlbend-metrics' counts and on its zero-division convention: a class nobody predicted scores 0.0, the way scikit-learn's zero_division=0 does.

What is proved: the shape of the matrix. The matrix of k classes has k rows (matrix_rows) and every row k cells (row_length). What is tested: the F32 numbers, against scikit-learn's confusion_matrix and classification_report (reference/test_report.py). F32 is not a real number, so a report's value is never proved, only its shape.

2 imports
import Base
import mlbend-metrics@0.1.0.0/main.bend as M

Laws

law matrix_rows provedsource · line 102 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> @+n:Nat -> @+i:Nat -> {rows_of(matrix_from(ys, ps, k, n, i)) == n : Nat}

LAW 1 (matrix_rows): n rows of the matrix are n rows.

law row_length provedsource · line 121 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> @+i:Nat -> @+n:Nat -> @+j:Nat -> {cells_of(row_from(ys, ps, k, i, n, j)) == n : Nat}

LAW 2 (row_length): n cells of a row are n cells.

Types

type Cls source · line 26 · raw

Data

type Report source · line 32 · raw

Data

type Tot source · line 174 · raw

Data

The sums the averages rest on: the per-class scores added up, and the support beside them.

Definitions

def cell.go source · line 44 · raw

@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @+i:Nat -> @+j:Nat -> @n:Nat -> Nat

The cell of the true label i and the predicted label j: how many samples have both.

def cell source · line 51 · raw

@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @+i:Nat -> @+j:Nat -> Nat

def rows_of source · line 55 · raw

@m:List<&2, List<&2, Nat>> -> Nat

How many rows a matrix has.

def cells_of source · line 63 · raw

@row:List<&2, Nat> -> Nat

How many cells a row has.

def row_from source · line 74 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> @+i:Nat -> @n:Nat -> @+j:Nat -> List<&2, Nat>

The row of the class i, from the column j on: n cells, the cell of every class from j to j+n-1, ascending. The counted form is the public one, because the law walks it: the counter of the cells left is the argument that shrinks.

def row source · line 83 · raw

@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @+k:Nat -> @+i:Nat -> List<&2, Nat>

The row of the class i: k cells, ascending.

def matrix_from source · line 87 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> @n:Nat -> @+i:Nat -> List<&2, List<&2, Nat>>

The rows of the classes i to i+n-1: n rows, each of k cells, ascending.

def matrix source · line 97 · raw

@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @+k:Nat -> List<&2, List<&2, Nat>>

The confusion matrix of k classes: k rows of k cells, the row of the class 0 first.

def cls_of source · line 149 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+i:Nat -> Cls

The report of one class: its precision, its recall, its F1 and its support.

def classes_of.go source · line 157 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @n:Nat -> @+i:Nat -> List<&2, Cls>

The report of every class, from the class i on: n of them, ascending.

def classes_of source · line 166 · raw

@ys:List<&2, Nat> -> @ps:List<&2, Nat> -> @+k:Nat -> List<&2, Cls>

The report of every class: k of them, the class 0 first.

def tot_add source · line 178 · raw

@t:Tot -> @c:Cls -> Tot

One class more, added into the sums.

def tot_of.go source · line 186 · raw

@classes:List<&2, Cls> -> @acc:Tot -> Tot

The sums over the classes.

def tot_of source · line 193 · raw

@classes:List<&2, Cls> -> Tot

def divide source · line 197 · raw

@+a:F32 -> @+b:F32 -> F32

a divided by b, or 0.0 when b is zero — the zero_division convention

def macro_of.go source · line 200 · raw

@t:Tot -> @+k:Nat -> Cls

def macro_of source · line 208 · raw

@classes:List<&2, Cls> -> @+k:Nat -> Cls

The macro average: the mean of the per-class scores, every class weighing the same, the way scikit-learn's macro average does.

def weighted_sums source · line 212 · raw

@classes:List<&2, Cls> -> @+p:F32 -> @+r:F32 -> @+f:F32 -> Tot

The weighted sums: every per-class score multiplied by its support.

def weighted_of.go source · line 222 · raw

@t:Tot -> @+total:Nat -> Cls

def weighted_of source · line 231 · raw

@classes:List<&2, Cls> -> @+total:Nat -> Cls

The weighted average: the per-class scores, each weighed by its support, over the total support, the way scikit-learn's weighted average does.

def report.go source · line 240 · raw

@r:Result<&1, &1, 0x9afaa60ee4d17706cb14d6ec20d1d4ae/main.Bad, Nat> -> @+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, 0x9afaa60ee4d17706cb14d6ec20d1d4ae/main.Bad, Report>

The whole report, or why there is none: the labels and the predictions must agree in length, and there must be at least one sample.

def report source · line 252 · raw

@+ys:List<&2, Nat> -> @+ps:List<&2, Nat> -> @+k:Nat -> Result<&1, &1, 0x9afaa60ee4d17706cb14d6ec20d1d4ae/main.Bad, Report>