data_00_quickstart/data_00_quickstart.rs
1//! # data_00 — Quickstart: CSV string -> numeric `Tensor` (matten-data)
2//!
3//! Run: `cargo run -p matten-data --example data_00_quickstart`
4//!
5//! ## What this shows
6//! The whole `matten-data` happy path in one place:
7//!
8//! ```text
9//! CSV string -> Table -> select columns -> fill missing -> try_numeric -> Tensor
10//! ```
11//!
12//! ## Teaching points
13//! - every step is explicit: you choose the columns, the fill value, and the
14//! moment of numeric conversion;
15//! - the output is a plain `[rows, columns]` f64 [`matten::Tensor`];
16//! - `matten-data` is a small conversion helper, **not** a dataframe library.
17
18use matten_data::Table;
19
20fn main() -> Result<(), matten_data::MattenDataError> {
21 // A small table with one text column and one missing numeric cell.
22 let csv = "\
23region,sales,cost
24north,100,40
25south,150,
26east,120,55";
27
28 let tensor = Table::from_csv_str(csv)?
29 .select_columns(["sales", "cost"])? // keep only the numeric columns
30 .fill_missing(0.0)? // the missing south/cost becomes 0.0, explicitly
31 .try_numeric()? // strict, explicit conversion to f64
32 .to_tensor()?; // a normal [rows, columns] Tensor
33
34 println!("shape: {:?}", tensor.shape());
35 println!("data : {:?}", tensor.as_slice());
36
37 assert_eq!(tensor.shape(), &[3, 2]);
38 assert_eq!(tensor.as_slice(), &[100.0, 40.0, 150.0, 0.0, 120.0, 55.0]);
39
40 println!("data_00_quickstart: OK");
41 Ok(())
42}