Skip to content

pydvl.valuation.methods.random

This module implements a trivial random valuation method.

It exists mainly for convenience when running experiments, e.g. when comparing methods with point removal, random values are a simple (albeit weak) baseline.

RandomValuation

RandomValuation(random_state: Seed)

Bases: Valuation

A trivial valuation method that assigns random values to each data point.

Values are in the range [0, 1), as generated by ValuationResult.from_random.

Successive calls to fit() will generate different values.

PARAMETER DESCRIPTION
random_state

Random seed for reproducibility.

TYPE: Seed

Source code in src/pydvl/valuation/methods/random.py
def __init__(self, random_state: Seed):
    super().__init__()
    self.random_state = np.random.default_rng(random_state)

result property

The current valuation result (not a copy).

fit

fit(data: Dataset, continue_from: ValuationResult | None = None) -> Self

Dummy fitting that generates a set of random values.

Successive calls will generate different values.

PARAMETER DESCRIPTION
data

used to determine the size of the valuation result

TYPE: Dataset

continue_from

(For consistency with other valuation methods) If this argument provided, the result is initialized with this object. The random values are added to the existing values.

TYPE: ValuationResult | None DEFAULT: None

Source code in src/pydvl/valuation/methods/random.py
def fit(self, data: Dataset, continue_from: ValuationResult | None = None) -> Self:
    """Dummy fitting that generates a set of random values.

    Successive calls will generate different values.

    Args:
        data: used to determine the size of the valuation result
        continue_from: (For consistency with other valuation methods) If this
            argument provided, the result is initialized with this object. The
            random values are added to the existing values.
    """
    self._result = self._init_or_check_result(data, continue_from)
    self._result += ValuationResult.from_random(
        size=len(data), seed=self.random_state, algorithm=str(self)
    )
    return self

values

values(sort: bool = False) -> ValuationResult

Returns a copy of the valuation result.

The valuation must have been run with fit() before calling this method.

PARAMETER DESCRIPTION
sort

Whether to sort the valuation result by value before returning it.

TYPE: bool DEFAULT: False

Returns: The result of the valuation.

Source code in src/pydvl/valuation/base.py
@deprecated(
    target=None,
    deprecated_in="0.10.0",
    remove_in="0.11.0",
)
def values(self, sort: bool = False) -> ValuationResult:
    """Returns a copy of the valuation result.

    The valuation must have been run with `fit()` before calling this method.

    Args:
        sort: Whether to sort the valuation result by value before returning it.
    Returns:
        The result of the valuation.
    """
    if not self.is_fitted:
        raise NotFittedException(type(self))
    assert self._result is not None

    r = self._result.copy()
    if sort:
        r.sort(inplace=True)
    return r