v2026.10.0
All Bundles
Bundle

Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

TableEncoder

Turns a CsvTable into the numeric matrix a model can train on, learning its encoding once so the same encoding can be reapplied to other tables. MatrixReader already loads a CSV, but only one that is numeric throughout, and it reads the header row as another record whose non-numeric cells become 0.0. Real tabular data is mixed: NSL-KDD's protocol, service and flag columns are strings among thirty-eight numeric ones. This skips the header, one-hot encodes the string columns and passes the numeric ones through. The vocabulary is fixed at Fit. Transform reuses it, so a training table and a test table produce the same columns in the same order even when the test split never contains some category, and a category that appears only in test encodes as all-zeros rather than silently shifting every column after it.

Example

enc := TableEncoder->New("class");
train_x := enc->FitTransform(train);
train_y := enc->TransformLabelsBinary(train, "normal");
test_x := enc->Transform(test);

Operations

Fit #

Learns the feature layout: which columns are numeric, and the value vocabulary of each column that is not. A column counts as numeric only when every data value in it parses as a number. One non-numeric value makes the whole column categorical, which is the safe direction: encoding a number as a category costs columns, while reading a category as a number invents an ordering that is not there.

method : public : Fit(table:CsvTable) ~ Bool

Parameters

NameTypeDescription
tableCsvTabletable to learn from, normally the training split only

Return

TypeDescription
Booltrue when the layout was learned, false for an unparsed or empty table

FitTransform #

Fits on this table and encodes it. For the training split only.

method : public : FitTransform(table:CsvTable) ~ Float[,]

Parameters

NameTypeDescription
tableCsvTabletable to fit and encode

Return

TypeDescription
Floatencoded matrix, or Nil when the table could not be fitted

GetFeatureNames #

The name of each encoded column: the source column for a numeric feature, and "column=value" for each one-hot column.

method : public : GetFeatureNames() ~ String[]

Return

TypeDescription
Stringnames in output order, or Nil when unfitted

GetLabelClasses #

The label classes seen at Fit, in the order TransformLabels indexes them.

method : public : GetLabelClasses() ~ String[]

Return

TypeDescription
Stringclass values, or Nil when unfitted

GetWidth #

The number of columns Transform produces.

method : public : GetWidth() ~ Int

Return

TypeDescription
Intencoded width, or 0 when unfitted

IsFitted #

Whether Fit has been called successfully.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when this encoder holds a layout

New # constructor

Constructor for a table with no label column: every column is a feature.

New()

New # constructor

Constructor

New(label_col:String)

Parameters

NameTypeDescription
label_colStringname of the label column, which is excluded from the features

Transform #

Encodes a table using the layout learned by Fit.

method : public : Transform(table:CsvTable) ~ Float[,]

Parameters

NameTypeDescription
tableCsvTabletable to encode; its header row is skipped

Return

TypeDescription
Floatmatrix of (data rows) x (encoded width), or Nil when unfitted or the table cannot be read

Example

test_x := enc->Transform(test);
if(test_x = Nil) {
  "unfitted, or the table did not parse"->PrintLine();
};

TransformLabels #

Encodes the label column as class indices, using the classes seen at Fit.

method : public : TransformLabels(table:CsvTable) ~ Float[]

Parameters

NameTypeDescription
tableCsvTabletable to read labels from

Return

TypeDescription
Floatone index per data row; a class unseen at Fit becomes -1.0

TransformLabelsBinary #

Encodes the label column as a two-class target: 0.0 for the named value, 1.0 for everything else. Intrusion detection labels are usually one benign value against many attack names, and this avoids enumerating the attacks.

method : public : TransformLabelsBinary(table:CsvTable, negative:String) ~ Float[]

Parameters

NameTypeDescription
tableCsvTabletable to read labels from
negativeStringthe label value that maps to 0.0

Return

TypeDescription
Floatone value per data row, or Nil when unfitted or the label is missing