pydvl.reporting
¶
plot_ci_array
¶
plot_ci_array(
data: NDArray,
level: float,
type: Literal["normal", "t", "auto"] = "normal",
abscissa: Optional[Sequence[str]] = None,
mean_color: Optional[str] = "dodgerblue",
shade_color: Optional[str] = "lightblue",
ax: Optional[Axes] = None,
**kwargs: Any,
) -> Axes
Plot values and a confidence interval from a 2D array.
Supported intervals are based on the normal and the t distributions.
PARAMETER | DESCRIPTION |
---|---|
data
|
A 2D array with M different values for each of the N indices.
TYPE:
|
level
|
The confidence level.
TYPE:
|
type
|
The type of confidence interval to use.
TYPE:
|
abscissa
|
The values for the x-axis. Leave empty to use increasing integers. |
mean_color
|
The color of the mean line. |
shade_color
|
The color of the confidence interval. |
ax
|
If passed, axes object into which to insert the figure. Otherwise, a new figure is created and the axes returned.
TYPE:
|
**kwargs
|
Additional arguments to pass to the plot function.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Axes
|
The matplotlib axes. |
Source code in src/pydvl/reporting/plots.py
plot_ci_values
¶
plot_ci_values(
values: ValuationResult,
level: float,
type: Literal["normal", "t", "auto"] = "auto",
abscissa: Optional[Sequence[Any]] = None,
mean_color: Optional[str] = "dodgerblue",
shade_color: Optional[str] = "lightblue",
ax: Optional[Axes] = None,
**kwargs: Any,
) -> Axes
Plot values and a confidence interval.
Supported intervals are based on the normal and the t distributions.
PARAMETER | DESCRIPTION |
---|---|
values
|
The valuation result. The object must be sorted by calling
TYPE:
|
level
|
The confidence level.
TYPE:
|
type
|
The type of confidence interval to use. If "auto", uses "norm" if the minimum number of updates for all indices is greater than 30, otherwise uses "t".
TYPE:
|
abscissa
|
The values for the x-axis. Leave empty to use increasing integers. |
mean_color
|
The color of the mean line. |
shade_color
|
The color of the confidence interval. |
ax
|
If passed, axes object into which to insert the figure. Otherwise, a new figure is created and the axes returned.
TYPE:
|
**kwargs
|
Additional arguments to pass to the plot function.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Axes
|
The matplotlib axes. |
Source code in src/pydvl/reporting/plots.py
spearman_correlation
¶
spearman_correlation(vv: List[OrderedDict], num_values: int, pvalue: float)
Simple matrix plots with spearman correlation for each pair in vv.
PARAMETER | DESCRIPTION |
---|---|
vv
|
list of OrderedDicts with index: value. Spearman correlation is computed for the keys.
TYPE:
|
num_values
|
Use only these many values from the data (from the start of the OrderedDicts)
TYPE:
|
pvalue
|
correlation coefficients for which the p-value is below the
threshold
TYPE:
|
Source code in src/pydvl/reporting/plots.py
plot_shapley
¶
plot_shapley(
df: DataFrame,
*,
level: float = 0.05,
ax: Optional[Axes] = None,
title: Optional[str] = None,
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
prefix: Optional[str] = "data_value",
) -> Axes
Plots the shapley values, as returned from compute_shapley_values, with error bars corresponding to an \(\alpha\)-level Normal confidence interval.
PARAMETER | DESCRIPTION |
---|---|
df
|
dataframe with the shapley values
TYPE:
|
level
|
confidence level for the error bars
TYPE:
|
ax
|
axes to plot on or None if a new subplots should be created
TYPE:
|
title
|
string, title of the plot |
xlabel
|
string, x label of the plot |
ylabel
|
string, y label of the plot |
RETURNS | DESCRIPTION |
---|---|
Axes
|
The axes created or used |
Source code in src/pydvl/reporting/plots.py
plot_influence_distribution
¶
plot_influence_distribution(
influences: NDArray[float64], index: int, title_extra: str = ""
) -> Axes
Plots the histogram of the influence that all samples in the training set have over a single sample index.
PARAMETER | DESCRIPTION |
---|---|
influences
|
array of influences (training samples x test samples) |
index
|
Index of the test sample for which the influences will be plotted.
TYPE:
|
title_extra
|
Additional text that will be appended to the title.
TYPE:
|
Source code in src/pydvl/reporting/plots.py
plot_influence_distribution_by_label
¶
plot_influence_distribution_by_label(
influences: NDArray[float64],
labels: NDArray[float64],
title_extra: str = "",
)
Plots the histogram of the influence that all samples in the training set have over a single sample index, separated by labels.
PARAMETER | DESCRIPTION |
---|---|
influences
|
array of influences (training samples x test samples) |
labels
|
labels for the training set. |
title_extra
|
Additional text that will be appended to the title.
TYPE:
|
Source code in src/pydvl/reporting/plots.py
run_removal_experiment
¶
run_removal_experiment(
data_factory: DataSplitFactory,
valuation_factories: list[ValuationFactory],
utility_factory: UtilityFactory,
removal_percentages: NDArray,
n_runs: int = 1,
n_jobs: int = 1,
random_state: int | None = None,
) -> tuple[DataFrame, DataFrame]
Run the sample removal experiment.
Given the factories, the removal percentages, and the number of runs, this function does the following in each run:
- Sample a random state
-
For each valuation method, compute the values and iteratively compute the scores after retraining on subsets of the data. This is parallelized. Each job requires 3 factories:
-
A factory that returns a train-test split of the data given a random state
- A factory returning a valuation method. The training set is passed to the factory, in case the valuation needs to train something. E.g. for Data-OOB we need the bagging model to be fitted before the valuation is computed.
- A factory that returns a utility that evaluates some model on a given test set. This is used for the performance evaluation. The model need not be the same as the one used for the valuation.
- It returns the scores in two DataFrames, one for the high value removals and one for the low value removals.
PARAMETER | DESCRIPTION |
---|---|
data_factory
|
A callable that returns a tuple of Datasets (train, test) given a random state
TYPE:
|
valuation_factories
|
A list of callables that return Valuation objects given a model, train data, and random state. The training data is typically not needed for construction, but bagging models may require it
TYPE:
|
utility_factory
|
A callable that returns a ModelUtility object given a test dataset and a random state. This object is used to evaluate the performance of the valuation method by removing data points from the training set and retraining the model, then scoring it on the test set.
TYPE:
|
removal_percentages
|
The percentage of data to remove from the training set. This should be a list of floats between 0 and 1.
TYPE:
|
n_runs
|
The number of repetitions of the experiment.
TYPE:
|
n_jobs
|
The number of parallel jobs to use.
TYPE:
|
random_state
|
The initial random state.
TYPE:
|
Returns: A tuple of DataFrames with the scores for the low and high value removals
Source code in src/pydvl/reporting/point_removal.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
compute_removal_score
¶
compute_removal_score(
u: ModelUtility,
values: ValuationResult,
training_data: Dataset,
percentages: NDArray[float_] | Iterable[float],
*,
remove_best: bool = False,
progress: bool = False,
) -> dict[float, float]
Fits a model and computes its score on a test set after incrementally removing a percentage of data points from the training set, based on their values.
PARAMETER | DESCRIPTION |
---|---|
u
|
Utility object with model, test data, and scoring function.
TYPE:
|
training_data
|
Dataset from which to remove data points.
TYPE:
|
values
|
Data values of data instances in the training set.
TYPE:
|
percentages
|
Sequence of removal percentages. |
remove_best
|
If True, removes data points in order of decreasing valuation.
TYPE:
|
progress
|
If True, display a progress bar.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[float, float]
|
Dictionary that maps the percentages to their respective scores. |