Skip to main content

csv_to_tensor/
csv_to_tensor.rs

1//! Canonical `matten-data` workflow: CSV string -> clean -> numeric `Tensor`.
2//!
3//! Run with: `cargo run -p matten-data --example csv_to_tensor`
4
5use matten_data::Table;
6
7fn main() -> Result<(), matten_data::MattenDataError> {
8    // A small, messy table: mixed types and a missing cell.
9    let csv = "\
10region,sales,cost,quantity
11north,100,40,5
12south,150,,7
13east,120,55,6";
14
15    let table = Table::from_csv_str(csv)?;
16
17    // Inspect what we have before converting anything.
18    println!("{}", table.schema_summary());
19
20    // Select only the numeric columns we want, fill the one missing cost with 0,
21    // convert explicitly, and produce a [rows, columns] f64 tensor.
22    let tensor = table
23        .select_columns(["sales", "cost", "quantity"])?
24        .fill_missing(0.0)?
25        .try_numeric()?
26        .to_tensor()?;
27
28    println!("tensor shape: {:?}", tensor.shape());
29    println!("tensor data : {:?}", tensor.as_slice());
30
31    assert_eq!(tensor.shape(), &[3, 3]);
32    Ok(())
33}