pub struct NumericTable { /* private fields */ }Expand description
A table whose cells have all been validated as numeric (RFC-034 §4.4).
Produced by Table::try_numeric. Holds row-major f64 data ready to become
a matten::Tensor via NumericTable::to_tensor.
Implementations§
Source§impl NumericTable
impl NumericTable
Sourcepub fn column_count(&self) -> usize
pub fn column_count(&self) -> usize
Number of columns.
Sourcepub fn column_names(&self) -> &[String]
pub fn column_names(&self) -> &[String]
Column names, in column order.
Sourcepub fn to_tensor(&self) -> Result<Tensor, MattenDataError>
pub fn to_tensor(&self) -> Result<Tensor, MattenDataError>
Build a numeric matten::Tensor with shape [rows, columns], row-major.
Errors with MattenDataError::EmptySelection if there are no columns, and
wraps any core construction failure (for example a zero-length dimension
when there are no rows) as MattenDataError::Matten.
Examples found in repository?
examples/data_00_quickstart.rs (line 32)
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}More examples
examples/csv_to_tensor.rs (line 38)
19fn main() -> Result<(), matten_data::MattenDataError> {
20 // A small, messy table: a text column and one missing numeric cell.
21 let csv = "\
22region,sales,cost,quantity
23north,100,40,5
24south,150,,7
25east,120,55,6";
26
27 let table = Table::from_csv_str(csv)?;
28
29 // Inspect what we have before converting anything.
30 println!("{}", table.schema_summary());
31
32 // Select only the numeric columns we want, fill the one missing cost with 0,
33 // convert explicitly, and produce a [rows, columns] f64 tensor.
34 let tensor = table
35 .select_columns(["sales", "cost", "quantity"])?
36 .fill_missing(0.0)?
37 .try_numeric()?
38 .to_tensor()?;
39
40 println!("tensor shape: {:?}", tensor.shape());
41 println!("tensor data : {:?}", tensor.as_slice());
42
43 // 3 rows x 3 columns; the missing south/cost became 0.0.
44 assert_eq!(tensor.shape(), &[3, 3]);
45 assert_eq!(
46 tensor.as_slice(),
47 &[100.0, 40.0, 5.0, 150.0, 0.0, 7.0, 120.0, 55.0, 6.0]
48 );
49 println!("csv_to_tensor: OK");
50 Ok(())
51}examples/data_04_to_tensor.rs (line 29)
19fn main() -> Result<(), matten_data::MattenDataError> {
20 let csv = "\
21region,sales,cost
22north,100,40
23south,150,45
24east,120,55";
25
26 let tensor: Tensor = Table::from_csv_str(csv)?
27 .select_columns(["sales", "cost"])?
28 .try_numeric()?
29 .to_tensor()?;
30
31 // [rows, columns] = [3, 2].
32 assert_eq!(tensor.shape(), &[3, 2]);
33
34 // Row-major: [north.sales, north.cost, south.sales, south.cost, ...].
35 assert_eq!(tensor.as_slice(), &[100.0, 40.0, 150.0, 45.0, 120.0, 55.0]);
36
37 // It is a plain Tensor, so core `matten` operations apply. Mean over axis 0
38 // (down the rows) gives the per-column mean: [mean(sales), mean(cost)].
39 let column_means = tensor.mean_axis(0);
40 println!("shape : {:?}", tensor.shape());
41 println!("column means : {:?}", column_means.as_slice());
42
43 assert_eq!(column_means.shape(), &[2]);
44 assert_eq!(
45 column_means.as_slice(),
46 &[(100.0 + 150.0 + 120.0) / 3.0, (40.0 + 45.0 + 55.0) / 3.0]
47 );
48
49 println!("data_04_to_tensor: OK");
50 Ok(())
51}examples/data_03_missing_values.rs (line 38)
17fn main() -> Result<(), MattenDataError> {
18 let csv = "\
19region,sales,cost
20north,100,40
21south,150,
22east,120,55";
23
24 let table = Table::from_csv_str(csv)?;
25 let numeric_cols = table.select_columns(["sales", "cost"])?;
26
27 // Converting with a missing cell still present is rejected — no silent zero.
28 match numeric_cols.try_numeric() {
29 Err(MattenDataError::MissingValue { column, row }) => {
30 println!("missing value blocked conversion: column={column}, csv_line={row}");
31 assert_eq!(column, "cost");
32 assert_eq!(row, 3); // header is line 1, so the south row is line 3
33 }
34 other => panic!("expected MissingValue, got {other:?}"),
35 }
36
37 // Decide explicitly what a missing cost means here, then convert.
38 let tensor = numeric_cols.fill_missing(0.0)?.try_numeric()?.to_tensor()?;
39
40 println!("filled shape: {:?}", tensor.shape());
41 println!("filled data : {:?}", tensor.as_slice());
42
43 assert_eq!(tensor.shape(), &[3, 2]);
44 // Only the missing south/cost was filled; the other cells are untouched.
45 assert_eq!(tensor.as_slice(), &[100.0, 40.0, 150.0, 0.0, 120.0, 55.0]);
46
47 println!("data_03_missing_values: OK");
48 Ok(())
49}Trait Implementations§
Source§impl Clone for NumericTable
impl Clone for NumericTable
Source§fn clone(&self) -> NumericTable
fn clone(&self) -> NumericTable
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for NumericTable
impl RefUnwindSafe for NumericTable
impl Send for NumericTable
impl Sync for NumericTable
impl Unpin for NumericTable
impl UnsafeUnpin for NumericTable
impl UnwindSafe for NumericTable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more