pub struct Table { /* private fields */ }Expand description
A parsed dataset: named columns of equal length.
A table holds one Column per source column, in source order, together
with the dataset name. Table::new checks the columns, so every table a
caller receives meets the guarantees in the module documentation.
§Examples
use dataset_ml::table::{Column, ColumnData, Table};
use ndarray::array;
let table = Table::new(
"example",
vec![
Column::new("id", ColumnData::Integer(array![1, 2])),
Column::new("width", ColumnData::Numeric(array![1.5, 2.5])),
],
)
.unwrap();
assert_eq!(table.name(), "example");
assert_eq!(table.n_samples(), 2);
assert_eq!(table.n_columns(), 2);
assert_eq!(table.names().collect::<Vec<_>>(), vec!["id", "width"]);Implementations§
Source§impl Table
impl Table
Sourcepub fn new(
name: &'static str,
columns: Vec<Column>,
) -> Result<Self, DatasetError>
pub fn new( name: &'static str, columns: Vec<Column>, ) -> Result<Self, DatasetError>
Build a table and check its columns.
§Parameters
name- The dataset name. It appears in the errors this table returns.columns- The columns, in the order the source lists them.
§Returns
Self- the new table, if the columns pass every check.
§Errors
DatasetError::DataFormatErrorwithEmptyDataset- ifcolumnsis empty, or if the columns hold no sample.DatasetError::DataFormatErrorwithLengthMismatch- if two columns hold a different number of samples.DatasetError::DataFormatErrorwithInvalidValue- if two columns share a name.
Sourcepub fn n_samples(&self) -> usize
pub fn n_samples(&self) -> usize
The number of samples every column holds.
§Returns
usize- the sample count. It is always at least1.
Sourcepub fn columns_mut(&mut self) -> &mut [Column]
pub fn columns_mut(&mut self) -> &mut [Column]
Sourcepub fn names(&self) -> impl Iterator<Item = &'static str> + '_
pub fn names(&self) -> impl Iterator<Item = &'static str> + '_
Every column name, in source order.
§Returns
impl Iterator<Item = &'static str>- the names, in source order.
Sourcepub fn column_mut(&mut self, name: &str) -> Option<&mut Column>
pub fn column_mut(&mut self, name: &str) -> Option<&mut Column>
Find one column by name, for in-place editing.
§Parameters
name- The name to look for. The comparison is exact.
§Returns
Some(&mut Column)- the column of that name.None- if the table holds no column of that name.
§Notes
A change to a value keeps the table valid. Do not change the length of the column: the table’s guarantees no longer hold if you do.
Sourcepub fn numeric_matrix(
&self,
names: &[&str],
) -> Result<Array2<f64>, DatasetError>
pub fn numeric_matrix( &self, names: &[&str], ) -> Result<Array2<f64>, DatasetError>
Build one f64 matrix out of the named columns.
The matrix keeps the order of names, which does not have to be the
source order. A name may repeat, and the matrix then holds that column
once per mention.
§Parameters
names- The columns to put in the matrix, in the order you want them. AColumnData::Bytescolumn contributes its full row width, and every other column contributes one value.
§Returns
Array2<f64>- a matrix ofTable::n_samplesrows. Its width is the sum of theColumnData::widthof every named column.
§Errors
DatasetError::DataFormatErrorwithLengthMismatch- ifnamesis empty.DatasetError::DataFormatErrorwithUnknownColumn- if the table holds no column of a given name.DatasetError::DataFormatErrorwithColumnTypeMismatch- if a named column isColumnData::String, which has no numeric reading.
§Performance
This builds a new matrix on every call, and that matrix holds
n_samples × width values. Call it once and keep the result.