pydvl.valuation.dataset
¶
This module contains convenience classes to handle data and groups thereof.
Value computations with supervised models benefit from a unified interface to handle data. This module provides two classes to handle data and labels, as well as feature names and other information: Dataset and GroupedDataset. Objects of both types can be used to construct scorers and to fit (most) valuation methods.
The underlying data arrays can always be accessed (read-only) via
Dataset.data(), which returns the tuple (x, y)
.
Slicing¶
Slicing the object, e.g. dataset[0]
, will return a new Dataset
with the data
corresponding to that slice. Note however that the contents of the new object, i.e.
dataset[0].data().x
, may not be the same as dataset.data().x[0]
, which is the first
point in the original data array. This is in particular true for
GroupedDatasets where one "logical" index may
correspond to multiple data points.
Slicing with None
, i.e. dataset[None]
, will return a copy of the whole dataset.
Grouped datasets and logical indices¶
As mentioned above, it is also possible to group data points together with GroupedDataset. In order to handle groups correctly, Datasets map "logical" indices to "data" indices and vice versa. The latter correspond to indices in the data arrays themselves, while the former may map to groups of data points.
A call to GroupedDataset.data(indices)
will return the data and labels of all samples for the given groups. But
grouped_data[0]
will return the data and labels of the first group, not the first data
point and will therefore be in general different from grouped_data.data([0])
.
Grouping data can be useful to reduce computation time, e.g. for Shapley-based methods.
It is important to keep in mind the distinction between logical and data indices for valuation methods that require computation on individual data points, like KNNShapley or Data-OOB. In these cases, the logical indices are used to compute the Shapley values, while the data indices are used internally by the method.
RawData
dataclass
¶
A view on a dataset's raw data. This is not a copy.
Dataset
¶
Dataset(
x: NDArray,
y: NDArray,
feature_names: Sequence[str] | NDArray[str_] | None = None,
target_names: Sequence[str] | NDArray[str_] | None = None,
data_names: Sequence[str] | NDArray[str_] | None = None,
description: str | None = None,
multi_output: bool = False,
)
A convenience class to handle datasets.
It holds a dataset, together with info on feature names, target names, and data names. It is used to pass data around to valuation methods.
The underlying data arrays can be accessed via
Dataset.data(), which returns the tuple
(X, y)
as a read-only RawData object. The data
can be accessed by indexing the object directly, e.g. dataset[0]
will return the
data point corresponding to index 0 in dataset
. For this base class, this is the
same as dataset.data([0])
, which is the first point in the data array, but derived
classes can behave differently.
PARAMETER | DESCRIPTION |
---|---|
x
|
training data
TYPE:
|
y
|
labels for training data
TYPE:
|
feature_names
|
names of the features of x data |
target_names
|
names of the features of y data |
data_names
|
names assigned to data points. For example, if the dataset is a time series, each entry can be a timestamp which can be referenced directly instead of using a row number. |
description
|
A textual description of the dataset.
TYPE:
|
multi_output
|
set to
TYPE:
|
Changed in version 0.10.0
No longer holds split data, but only x, y.
Changed in version 0.10.0
Slicing now return a new Dataset
object, not raw data.
Source code in src/pydvl/valuation/dataset.py
indices
property
¶
Index of positions in data.x_train.
Contiguous integers from 0 to len(Dataset).
names
property
¶
Names of each individual datapoint.
Used for reporting Shapley values.
feature
¶
Returns a slice for the feature with the given name.
Source code in src/pydvl/valuation/dataset.py
data
¶
Given a set of indices, returns the training data that refer to those indices, as a read-only tuple-like structure.
This is used mainly by subclasses of UtilityBase to retrieve subsets of the data from indices.
PARAMETER | DESCRIPTION |
---|---|
indices
|
Optional indices that will be used to select points from
the training data. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RawData
|
If |
Source code in src/pydvl/valuation/dataset.py
data_indices
¶
Returns a subset of indices.
This is equivalent to using Dataset.indices[logical_indices]
but allows
subclasses to define special behaviour, e.g. when indices in Dataset
do not
match the indices in the data.
For Dataset
, this is a simple pass-through.
PARAMETER | DESCRIPTION |
---|---|
indices
|
A set of indices held by this object |
RETURNS | DESCRIPTION |
---|---|
NDArray[int_]
|
The indices of the data points in the data array. |
Source code in src/pydvl/valuation/dataset.py
logical_indices
¶
Returns the indices in this Dataset
for the given indices in the data array.
This is equivalent to using Dataset.indices[data_indices]
but allows
subclasses to define special behaviour, e.g. when indices in Dataset
do not
match the indices in the data.
PARAMETER | DESCRIPTION |
---|---|
indices
|
A set of indices in the data array. |
RETURNS | DESCRIPTION |
---|---|
NDArray[int_]
|
The abstract indices for the given data indices. |
Source code in src/pydvl/valuation/dataset.py
from_sklearn
classmethod
¶
from_sklearn(
data: Bunch,
train_size: int | float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
**kwargs,
) -> tuple[Dataset, Dataset]
Constructs two Dataset objects from a
sklearn.utils.Bunch, as returned by the load_*
functions in scikit-learn toy datasets.
Example
PARAMETER | DESCRIPTION |
---|---|
data
|
scikit-learn Bunch object. The following attributes are supported:
TYPE:
|
train_size
|
size of the training dataset. Used in |
the value is automatically set to the complement of the test size.
random_state: seed for train / test split
stratify_by_target: If True
, data is split in a stratified
fashion, using the target variable as labels. Read more in
scikit-learn's user guide.
kwargs: Additional keyword arguments to pass to the
Dataset constructor. Use this to pass e.g. is_multi_output
.
RETURNS | DESCRIPTION |
---|---|
tuple[Dataset, Dataset]
|
Object with the sklearn dataset |
Changed in version 0.6.0
Added kwargs to pass to the Dataset constructor.
Changed in version 0.10.0
Returns a tuple of two Dataset objects.
Source code in src/pydvl/valuation/dataset.py
from_arrays
classmethod
¶
from_arrays(
X: NDArray,
y: NDArray,
train_size: float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
**kwargs: Any,
) -> tuple[Dataset, Dataset]
Constructs a Dataset object from X and y numpy arrays as
returned by the make_*
functions in sklearn generated datasets.
Example
PARAMETER | DESCRIPTION |
---|---|
X
|
numpy array of shape (n_samples, n_features)
TYPE:
|
y
|
numpy array of shape (n_samples,)
TYPE:
|
train_size
|
size of the training dataset. Used in
TYPE:
|
random_state
|
seed for train / test split
TYPE:
|
stratify_by_target
|
If
TYPE:
|
kwargs
|
Additional keyword arguments to pass to the
Dataset constructor. Use this to pass
e.g.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Dataset, Dataset]
|
Object with the passed X and y arrays split across training and test sets. |
New in version 0.4.0
Changed in version 0.6.0
Added kwargs to pass to the Dataset constructor.
Changed in version 0.10.0
Returns a tuple of two Dataset objects.
Source code in src/pydvl/valuation/dataset.py
GroupedDataset
¶
GroupedDataset(
x: NDArray,
y: NDArray,
data_groups: Sequence[int] | NDArray[int_],
feature_names: Sequence[str] | NDArray[str_] | None = None,
target_names: Sequence[str] | NDArray[str_] | None = None,
data_names: Sequence[str] | NDArray[str_] | None = None,
group_names: Sequence[str] | NDArray[str_] | None = None,
description: str | None = None,
**kwargs: Any,
)
Bases: Dataset
Used for calculating values of subsets of the data considered as logical units. For instance, one can group by value of a categorical feature, by bin into which a continuous feature falls, or by label.
PARAMETER | DESCRIPTION |
---|---|
x
|
training data
TYPE:
|
y
|
labels of training data
TYPE:
|
data_groups
|
Sequence of the same length as |
feature_names
|
names of the covariates' features. |
target_names
|
names of the labels or targets y |
data_names
|
names of the data points. For example, if the dataset is a time series, each entry can be a timestamp. |
group_names
|
names of the groups. If not provided, the numerical group ids
from |
description
|
A textual description of the dataset
TYPE:
|
kwargs
|
Additional keyword arguments to pass to the Dataset constructor.
TYPE:
|
Changed in version 0.6.0
Added group_names
and forwarding of kwargs
Changed in version 0.10.0
No longer holds split data, but only x, y and group information. Added methods to retrieve indices for groups and vice versa.
Source code in src/pydvl/valuation/dataset.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
feature
¶
Returns a slice for the feature with the given name.
Source code in src/pydvl/valuation/dataset.py
data
¶
Returns the data and labels of all samples in the given groups.
PARAMETER | DESCRIPTION |
---|---|
indices
|
group indices whose elements to return. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RawData
|
Tuple of training data |
Source code in src/pydvl/valuation/dataset.py
data_indices
¶
data_indices(
indices: int | slice | Sequence[int] | NDArray[int_] | None = None,
) -> NDArray[int_]
Returns the indices of the samples in the given groups.
PARAMETER | DESCRIPTION |
---|---|
indices
|
group indices whose elements to return. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
NDArray[int_]
|
Indices of the samples in the given groups. |
Source code in src/pydvl/valuation/dataset.py
logical_indices
¶
Returns the group indices for the given data indices.
PARAMETER | DESCRIPTION |
---|---|
indices
|
indices of the data points in the data array. If |
RETURNS | DESCRIPTION |
---|---|
NDArray[int_]
|
Group indices for the given data indices. |
Source code in src/pydvl/valuation/dataset.py
from_sklearn
classmethod
¶
from_sklearn(
data: Bunch,
train_size: float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
**kwargs,
) -> tuple[GroupedDataset, GroupedDataset]
from_sklearn(
data: Bunch,
train_size: float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
data_groups: Sequence[int] | None = None,
**kwargs,
) -> tuple[GroupedDataset, GroupedDataset]
from_sklearn(
data: Bunch,
train_size: int | float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
data_groups: Sequence[int] | None = None,
**kwargs: dict[str, Any],
) -> tuple[GroupedDataset, GroupedDataset]
Constructs a GroupedDataset object, and an
ungrouped Dataset object from a
sklearn.utils.Bunch as returned by the load_*
functions in
scikit-learn toy datasets and groups
it.
Example
PARAMETER | DESCRIPTION |
---|---|
data
|
scikit-learn Bunch object. The following attributes are supported:
-
TYPE:
|
train_size
|
size of the training dataset. Used in |
random_state
|
seed for train / test split.
TYPE:
|
stratify_by_target
|
If
TYPE:
|
data_groups
|
an array holding the group index or name for each data point. The length of this array must be equal to the number of data points in the dataset. |
kwargs
|
Additional keyword arguments to pass to the Dataset constructor. |
RETURNS | DESCRIPTION |
---|---|
tuple[GroupedDataset, GroupedDataset]
|
Datasets with the selected sklearn data |
Changed in version 0.10.0
Returns a tuple of two GroupedDataset objects.
Source code in src/pydvl/valuation/dataset.py
from_arrays
classmethod
¶
from_arrays(
X: NDArray,
y: NDArray,
train_size: float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
**kwargs,
) -> tuple[GroupedDataset, GroupedDataset]
from_arrays(
X: NDArray,
y: NDArray,
train_size: float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
data_groups: Sequence[int] | None = None,
**kwargs,
) -> tuple[GroupedDataset, GroupedDataset]
from_arrays(
X: NDArray,
y: NDArray,
train_size: float = 0.8,
random_state: int | None = None,
stratify_by_target: bool = False,
data_groups: Sequence[int] | None = None,
**kwargs: Any,
) -> tuple[GroupedDataset, GroupedDataset]
Constructs a GroupedDataset object,
and an ungrouped Dataset object from X and y
numpy arrays as returned by the make_*
functions in
scikit-learn generated datasets.
Example
>>> from sklearn.datasets import make_classification
>>> from pydvl.valuation.dataset import GroupedDataset
>>> X, y = make_classification(
... n_samples=100,
... n_features=4,
... n_informative=2,
... n_redundant=0,
... random_state=0,
... shuffle=False
... )
>>> data_groups = X[:, 0] // 0.5
>>> train, test = GroupedDataset.from_arrays(X, y, data_groups=data_groups)
PARAMETER | DESCRIPTION |
---|---|
X
|
array of shape (n_samples, n_features)
TYPE:
|
y
|
array of shape (n_samples,)
TYPE:
|
train_size
|
size of the training dataset. Used in
TYPE:
|
random_state
|
seed for train / test split.
TYPE:
|
stratify_by_target
|
If
TYPE:
|
data_groups
|
an array holding the group index or name for each data point. The length of this array must be equal to the number of data points in the dataset. |
kwargs
|
Additional keyword arguments that will be passed to the GroupedDataset constructor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[GroupedDataset, GroupedDataset]
|
Dataset with the passed X and y arrays split across training and test sets. |
New in version 0.4.0
Changed in version 0.6.0
Added kwargs to pass to the GroupedDataset constructor.
Changed in version 0.10.0
Returns a tuple of two GroupedDataset objects.
Source code in src/pydvl/valuation/dataset.py
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
|
from_dataset
classmethod
¶
from_dataset(
data: Dataset,
data_groups: Sequence[int] | NDArray[int_],
group_names: Sequence[str] | NDArray[str_] | None = None,
**kwargs: Any,
) -> GroupedDataset
Creates a GroupedDataset object from a Dataset object and a mapping of data groups.
Example
PARAMETER | DESCRIPTION |
---|---|
data
|
The original data.
TYPE:
|
data_groups
|
An array holding the group index or name for each data point. The length of this array must be equal to the number of data points in the dataset. |
group_names
|
Names of the groups. If not provided, the numerical group ids
from |
kwargs
|
Additional arguments to be passed to the GroupedDataset constructor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GroupedDataset
|
A GroupedDataset with the initial
Dataset grouped by |