pydvl.valuation.scorers
¶
Scorer
¶
Bases: ABC
A scoring callable that takes a model and returns a scalar.
Added in version 0.10.0
ABC added
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
ClasswiseSupervisedScorer
¶
ClasswiseSupervisedScorer(
scoring: str
| SupervisedScorerCallable[SupervisedModelT]
| SupervisedModelT,
test_data: Dataset,
default: float = 0.0,
range: tuple[float, float] = (0, 1),
in_class_discount_fn: Callable[[float], float] = lambda x: x,
out_of_class_discount_fn: Callable[[float], float] = exp,
rescale_scores: bool = True,
name: str | None = None,
)
Bases: SupervisedScorer[SupervisedModelT]
A Scorer designed for evaluation in classification problems.
The final score is the combination of the in-class and out-of-class scores, which are e.g. the accuracy of the trained model over the instances of the test set with the same, and different, labels, respectively. See the module's documentation for more on this.
These two scores are computed with an "inner" scoring function, which must be provided upon construction.
Multi-class support
The inner score must support multiple class labels if you intend to apply them
to a multi-class problem. For instance, 'accuracy' supports multiple classes,
but f1
does not. For a two-class classification problem, using f1_weighted
is essentially equivalent to using accuracy
.
PARAMETER | DESCRIPTION |
---|---|
scoring
|
Name of the scoring function or a callable that can be passed to SupervisedScorer.
TYPE:
|
default
|
Score to use when a model fails to provide a number, e.g. when too little was used to train it, or errors arise.
TYPE:
|
range
|
Numerical range of the score function. Some Monte Carlo methods
can use this to estimate the number of samples required for a
certain quality of approximation. If not provided, it can be read
from the |
in_class_discount_fn
|
Continuous, monotonic increasing function used to discount the in-class score. |
out_of_class_discount_fn
|
Continuous, monotonic increasing function used to discount the out-of-class score. |
rescale_scores
|
If set to True, the scores will be denormalized. This is particularly useful when the inner score function \(a_S\) is calculated by an estimator of the form $rac{1}{N} \sum_i x_i$.
TYPE:
|
name
|
Name of the scorer. If not provided, the name of the inner scoring
function will be prefixed by
TYPE:
|
New in version 0.7.1
Source code in src/pydvl/valuation/scorers/classwise.py
compute_in_and_out_of_class_scores
¶
compute_in_and_out_of_class_scores(
model: SupervisedModelT, rescale_scores: bool = True
) -> tuple[float, float]
Computes in-class and out-of-class scores using the provided inner scoring function. The result is
In this context, for label \(c\) calculations are executed twice: once for \(D_c\) and once for \(D_{-c}\) to determine the in-class and out-of-class scores, respectively. By default, the raw scores are multiplied by \(\frac{|D_c|}{|D|}\) and \(\frac{|D_{-c}|}{|D|}\), respectively. This is done to ensure that both scores are of the same order of magnitude. This normalization is particularly useful when the inner score function \(a_S\) is calculated by an estimator of the form \(\frac{1}{N} \sum_i x_i\), e.g. the accuracy.
PARAMETER | DESCRIPTION |
---|---|
model
|
Model used for computing the score on the validation set.
TYPE:
|
rescale_scores
|
If set to True, the scores will be denormalized. This is particularly useful when the inner score function \(a_S\) is calculated by an estimator of the form \(\frac{1}{N} \sum_i x_i\).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[float, float]
|
Tuple containing the in-class and out-of-class scores. |
Source code in src/pydvl/valuation/scorers/classwise.py
SupervisedScorer
¶
SupervisedScorer(
scoring: str
| SupervisedScorerCallable[SupervisedModelT]
| SupervisedModelT,
test_data: Dataset,
default: float,
range: tuple[float, float] = (-inf, inf),
name: str | None = None,
)
Bases: Generic[SupervisedModelT]
, Scorer
A scoring callable that takes a model, data, and labels and returns a scalar.
PARAMETER | DESCRIPTION |
---|---|
scoring
|
Either a string or callable that can be passed to get_scorer.
TYPE:
|
test_data
|
Dataset where the score will be evaluated.
TYPE:
|
default
|
score to be used when a model cannot be fit, e.g. when too little data is passed, or errors arise.
TYPE:
|
range
|
numerical range of the score function. Some Monte Carlo
methods can use this to estimate the number of samples required for a
certain quality of approximation. If not provided, it can be read from
the |
name
|
The name of the scorer. If not provided, the name of the function passed will be used.
TYPE:
|
New in version 0.5.0
Changed in version 0.10.0
This is now SupervisedScorer
and holds the test data used to evaluate the
model.
Source code in src/pydvl/valuation/scorers/supervised.py
compose_score
¶
compose_score(
scorer: SupervisedScorer,
transformation: Callable[[float], float],
name: str,
) -> SupervisedScorer
Composes a scoring function with an arbitrary scalar transformation.
Useful to squash unbounded scores into ranges manageable by data valuation methods.
Example
PARAMETER | DESCRIPTION |
---|---|
scorer
|
The object to be composed.
TYPE:
|
transformation
|
A scalar transformation |
name
|
A string representation for the composition, for
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SupervisedScorer
|
The composite SupervisedScorer. |