~/bend-docscommunity

main.bend checks

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

mlbend-pipeline: fit on the training rows, transform both sides.

import mlbend-pipeline@0.1.0.0/main.bend as Pipeline

The leakage an experiment layer exists to prevent: standardising the whole dataset before the folds are made, so the scaler has already seen the test rows. Here fit takes the training rows and nothing else, and transform applies the fitted means and deviations to any rows.

+scaler = Pipeline.fit(train_rows) # train only +train = Pipeline.transform(scaler, train_rows) +test = Pipeline.transform(scaler, test_rows)

or, for one fold, Pipeline.fold(train_rows, test_rows), which returns both sides already transformed.

What is proved: transforming the rows keeps one output row for every input row (pipeline_preserves_rows) — no row is dropped and none is invented. What is checked: the means, the deviations and the transformed values against scikit-learn's StandardScaler fitted on the same training rows (reference/test_pipeline.py), and the cross-validation against scikit-learn's Pipeline(StandardScaler, LogisticRegression) (reference/test_crossval.py).

The no-leakage property is the API itself: the only rows fit can see are the ones it is given, and fold gives it the training rows. A law cannot prove what a caller passes; the tests and the cross-validation comparison are what hold that end.

1 import
import Base

Laws

law pipeline_preserves_rows provedsource · line 44 · raw

@+scaler:Scaler -> @rows:List<&2, List<&2, F32>> -> {count_rows(transform(scaler, rows)) == count_rows(rows) : Nat}

LAW (pipeline_preserves_rows): transforming the rows keeps one output row for every input row — no row is dropped and none is invented.

Types

type Scaler source · line 34 · raw

Data

type Prepared source · line 39 · raw

Data

Definitions

def count_rows source · line 50 · raw

@rows:List<&2, List<&2, F32>> -> Nat

How many rows there are.

def zip_add source · line 58 · raw

@a:List<&2, F32> -> @b:List<&2, F32> -> List<&2, F32>

Two rows, added cell by cell.

def zip_sub source · line 70 · raw

@+a:List<&2, F32> -> @b:List<&2, F32> -> List<&2, F32>

Two rows, cell by cell, the first minus the second.

def zip_div source · line 82 · raw

@a:List<&2, F32> -> @b:List<&2, F32> -> List<&2, F32>

Two rows, cell by cell, the first divided by the second.

def zip_scale source · line 94 · raw

@a:List<&2, F32> -> @+k:F32 -> List<&2, F32>

One row and a number, cell by cell.

def squares source · line 102 · raw

@+a:List<&2, F32> -> List<&2, F32>

The cells of a row, squared.

def column_sums_go source · line 110 · raw

@rows:List<&2, List<&2, F32>> -> @+acc:List<&2, F32> -> List<&2, F32>

The sum of every column, over the rows.

def column_sums source · line 118 · raw

@rows:List<&2, List<&2, F32>> -> List<&2, F32>

The sum of every column, over the rows.

def means_of source · line 126 · raw

@+rows:List<&2, List<&2, F32>> -> List<&2, F32>

The mean of every column.

def deviation_sums_go source · line 135 · raw

@rows:List<&2, List<&2, F32>> -> @+means:List<&2, F32> -> @+acc:List<&2, F32> -> List<&2, F32>

The sum of the squared deviations of every column.

def deviation_sums source · line 144 · raw

@rows:List<&2, List<&2, F32>> -> @+means:List<&2, F32> -> List<&2, F32>

The sum of the squared deviations of every column.

def roots source · line 152 · raw

@a:List<&2, F32> -> List<&2, F32>

The square root of every cell of a row.

def stds_of source · line 161 · raw

@+rows:List<&2, List<&2, F32>> -> @+means:List<&2, F32> -> List<&2, F32>

The standard deviation of every column: the population one, as scikit-learn's StandardScaler uses.

def fit source · line 166 · raw

@+rows:List<&2, List<&2, F32>> -> Scaler

The scaler of the training rows, and nothing else.

def transform_row source · line 172 · raw

@scaler:Scaler -> @row:List<&2, F32> -> List<&2, F32>

One row, centred and scaled by the fitted values.

def transform source · line 178 · raw

@+scaler:Scaler -> @rows:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>

The rows, centred and scaled by the fitted values.

def fold source · line 187 · raw

@+train:List<&2, List<&2, F32>> -> @test:List<&2, List<&2, F32>> -> Prepared

One fold: fit on the training rows, transform both sides.