DiscreteDataset#
- class causalbase.data.DiscreteDataset(values, cardinalities, names, categories)[source]#
A fully-observed dataset over discrete (categorical) variables.
- Parameters:
- valuesnp.ndarray
Integer-coded data, shape
(n_rows, n_vars). Cast toint32. Entryvalues[i, j]is the code of row i’s level of variable j, an index intocategories[j].- cardinalitiesnp.ndarray
Number of levels per variable, shape
(n_vars,). Cast toint32.- namesSequence[str]
Variable names, length
n_vars. Coerced to an immutabletuple[str, ...].- categoriesSequence[Sequence[str]]
Level labels per variable;
categories[j][i]is the label of level i of variable j, solen(categories[j]) == cardinalities[j]. Coerced to an immutabletuple[tuple[str, ...], ...].
- Attributes:
- Raises:
- ValueError
If values is not 2-D, if cardinalities, names, or categories do not each have exactly n_vars entries, if two variables share a name, if some
categories[j]does not have exactlycardinalities[j]entries, or if any entry of values falls outside[0, cardinalities[j])for its column.
Notes
The loaders code each column’s levels in sorted order, whatever order they appear in, so the same data always gets the same codes.
values is stored column-major, because the BDeu kernel reads one column at a time.
names, cardinalities and categories are immutable and shared by every subset, so all sites split from one dataset agree on the variables.
- property n_rows#
int: Number of rows (observations) in the dataset.
- property n_vars#
int: Number of variables (columns) in the dataset.
- subset(rows)[source]#
Select a subset of rows, keeping the full parent variable domains.
- Parameters:
- rowsnp.ndarray
Row indices (integer array) or a boolean mask of length n_rows, selecting the rows to keep.
- Returns:
- DiscreteDataset
The selected rows over the same variables. A level missing from those rows still counts toward its variable’s cardinality, so every site agrees on the domains.
- classmethod from_pandas(df, categories=None)[source]#
Build a dataset from a DataFrame of categorical columns.
Values are read as strings, so
1and"1"are the same level.- Parameters:
- dfpandas.DataFrame
One column per variable, one row per observation.
- categoriessequence of sequences of str, optional
The levels of each column. By default, the distinct values of each column, sorted. Pass them to give several datasets one encoding.
- Returns:
- DiscreteDataset
- Raises:
- ValueError
If two columns share a name, a column has missing values, or a value is outside categories.
- classmethod from_csv(path)[source]#
Build a dataset from a CSV file of categorical columns.
A header row of variable names, then one row per observation. Every value is read as a string label, including
"None"and"NA", which some networks use as real levels. Only an empty or blank field counts as missing, and missing data is rejected.- Parameters:
- pathstr, pathlib.Path or binary file object
Path to the CSV file (
.csv.gzis decompressed), or the file itself, opened in binary mode.
- Returns:
- DiscreteDataset
The encoded dataset.
- Raises:
- ValueError
If any column contains an empty or whitespace-only field.