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
- New
- Fit
- FitTransform
- GetFeatureNames
- GetLabelClasses
- GetWidth
- IsFitted
- Transform
- TransformLabels
- TransformLabelsBinary
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) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| table | CsvTable | table to learn from, normally the training split only |
Return
| Type | Description |
|---|---|
| Bool | true 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
| Name | Type | Description |
|---|---|---|
| table | CsvTable | table to fit and encode |
Return
| Type | Description |
|---|---|
| Float | encoded 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
| Type | Description |
|---|---|
| String | names 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
| Type | Description |
|---|---|
| String | class values, or Nil when unfitted |
GetWidth #
The number of columns Transform produces.
method : public : GetWidth() ~ IntReturn
| Type | Description |
|---|---|
| Int | encoded width, or 0 when unfitted |
IsFitted #
Whether Fit has been called successfully.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true when this encoder holds a layout |
New # constructor
Constructor
New(label_col:String)Parameters
| Name | Type | Description |
|---|---|---|
| label_col | String | name 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
| Name | Type | Description |
|---|---|---|
| table | CsvTable | table to encode; its header row is skipped |
Return
| Type | Description |
|---|---|
| Float | matrix 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
| Name | Type | Description |
|---|---|---|
| table | CsvTable | table to read labels from |
Return
| Type | Description |
|---|---|
| Float | one 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
| Name | Type | Description |
|---|---|---|
| table | CsvTable | table to read labels from |
| negative | String | the label value that maps to 0.0 |
Return
| Type | Description |
|---|---|
| Float | one value per data row, or Nil when unfitted or the label is missing |