Skip to main content

Module table

Module table 

Source
Expand description

The Table every loader parses into.

A Table holds one named, typed Column per source column. It materializes a matrix out of the columns the caller names.

This module is always available, whichever features you pick. Column storage for a parsed dataset.

Every loader in this crate parses its source into a Table and returns that table. This module holds the table and the two types it is built from.

§Contents

  • ColumnData holds the values of one column, in the type the source uses. It has one variant per storage type: Numeric, Integer, String, and Bytes.
  • Column adds the name that the source gives those values.
  • Table holds one Column per source column. It checks the columns when it builds them, and it finds a column by name.

§How a loader fills a table

A loader builds one Column for each column of its source, then passes them all to Table::new. The table keeps them in source order. The loader stores each value in the type the source uses, and applies no encoding. The choice between an ordinal code and a one-hot code stays with the caller.

§How a caller reads a table

Table::column finds a single column by name, and the as_* methods of Column read its values in their source type. Table::numeric_matrix builds one f64 matrix out of the columns the caller names, in the order the caller names them.

Each loader lists the names of its columns in associated constants, such as Iris::FEATURE_NAMES and Iris::TARGET. Pass one of these constants to Table::numeric_matrix, or name the columns directly.

§Guarantees

Table::new checks these three conditions before it builds a table. Every method of Table relies on them:

  • The table holds at least one column.
  • Every column holds the same number of samples.
  • No two columns share a name.

§Examples

use dataset_ml::table::{Column, ColumnData, Table};
use ndarray::array;

let table = Table::new(
    "example",
    vec![
        Column::new("width", ColumnData::Numeric(array![1.0, 2.0])),
        Column::new("height", ColumnData::Numeric(array![3.0, 4.0])),
        Column::new(
            "species",
            ColumnData::String(array!["a".to_string(), "b".to_string()]),
        ),
    ],
)
.unwrap();

assert_eq!(table.n_samples(), 2);

// Name the columns you want, in the order you want them.
let matrix = table.numeric_matrix(&["height", "width"]).unwrap();
assert_eq!(matrix.shape(), &[2, 2]);
assert_eq!(matrix.row(0).to_vec(), vec![3.0, 1.0]);

// Reach one column by name, whatever its position.
let species = table.column("species").unwrap().as_string().unwrap();
assert_eq!(species[0], "a");

Structs§

Column
One named column of a Table.
Table
A parsed dataset: named columns of equal length.

Enums§

ColumnData
The values of one column, in the type the source uses.