pulsatrix 1.1.0
Loading...
Searching...
No Matches
ice.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstdint>
9#include <functional>
10#include <vector>
11
12#include "pulsatrix/tensor.hpp"
13
14namespace pulsatrix {
15
30[[nodiscard]] std::vector<float> FeatureGrid(const std::vector<Tensor>& background, int64_t feature_index,
31 int64_t grid_size = 100, float lower_percentile = 0.05f,
32 float upper_percentile = 0.95f);
33
41struct IceResult {
42 int64_t feature_index = 0;
43 int64_t target_index = 0;
44 std::vector<float> grid;
45 int64_t num_instances = 0;
47 std::vector<float> curves;
49 std::vector<float> feature_values;
50
52 [[nodiscard]] std::vector<float> partial_dependence() const;
58 [[nodiscard]] std::vector<float> centered(int64_t anchor = 0) const;
65 [[nodiscard]] std::vector<float> derivative() const;
66};
67
81[[nodiscard]] IceResult ComputeIce(const std::function<Tensor(const Tensor&)>& predict,
82 const std::vector<Tensor>& instances, int64_t feature_index,
83 int64_t target_index, const std::vector<float>& grid);
84
87 int64_t feature_x = 0;
88 int64_t feature_y = 0;
89 int64_t target_index = 0;
90 std::vector<float> grid_x;
91 std::vector<float> grid_y;
95 std::vector<float> values;
96};
97
104[[nodiscard]] PartialDependence2D ComputePartialDependence2D(const std::function<Tensor(const Tensor&)>& predict,
105 const std::vector<Tensor>& background,
106 int64_t feature_x, int64_t feature_y,
107 int64_t target_index, const std::vector<float>& grid_x,
108 const std::vector<float>& grid_y);
109
120struct AleResult {
121 int64_t feature_index = 0;
122 int64_t target_index = 0;
124 std::vector<float> edges;
126 std::vector<float> effects;
128 std::vector<int64_t> counts;
130 std::vector<float> feature_values;
131};
132
143[[nodiscard]] AleResult ComputeAle(const std::function<Tensor(const Tensor&)>& predict,
144 const std::vector<Tensor>& instances, int64_t feature_index, int64_t target_index,
145 int64_t num_bins = 20);
146
147} // namespace pulsatrix
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Definition acquisition_functions.hpp:16
PartialDependence2D ComputePartialDependence2D(const std::function< Tensor(const Tensor &)> &predict, const std::vector< Tensor > &background, int64_t feature_x, int64_t feature_y, int64_t target_index, const std::vector< float > &grid_x, const std::vector< float > &grid_y)
Two-feature partial dependence: for every pair (grid_x[c], grid_y[r]), the mean over the background o...
IceResult ComputeIce(const std::function< Tensor(const Tensor &)> &predict, const std::vector< Tensor > &instances, int64_t feature_index, int64_t target_index, const std::vector< float > &grid)
Computes ICE curves for every instance.
std::vector< float > FeatureGrid(const std::vector< Tensor > &background, int64_t feature_index, int64_t grid_size=100, float lower_percentile=0.05f, float upper_percentile=0.95f)
An evenly spaced grid for one feature, built the way scikit-learn's partial_dependence builds it: the...
AleResult ComputeAle(const std::function< Tensor(const Tensor &)> &predict, const std::vector< Tensor > &instances, int64_t feature_index, int64_t target_index, int64_t num_bins=20)
Computes first-order ALE the way PyALE 1.2 (and R's ALEPlot) does: edges at the feature's type-1 quan...
First-order accumulated local effects (Apley and Zhu 2020) of one feature.
Definition ice.hpp:120
std::vector< float > feature_values
Each instance's own value of the feature, for a rug.
Definition ice.hpp:130
std::vector< int64_t > counts
Instances in each bin (edges.size() - 1 of them).
Definition ice.hpp:128
int64_t target_index
Definition ice.hpp:122
int64_t feature_index
Definition ice.hpp:121
std::vector< float > effects
The centered accumulated effect at each edge.
Definition ice.hpp:126
std::vector< float > edges
Bin edges, strictly increasing: the minimum, then the upper edge of each bin.
Definition ice.hpp:124
ICE curves for one feature: curves[i][k] = f(x_i with feature j set to grid[k])[target].
Definition ice.hpp:41
std::vector< float > derivative() const
Derivative ICE (d-ICE): the slope of each curve, by central differences inside the grid and one-sided...
std::vector< float > grid
Definition ice.hpp:44
int64_t feature_index
Definition ice.hpp:42
std::vector< float > curves
Row-major (num_instances, grid.size()).
Definition ice.hpp:47
std::vector< float > feature_values
Each instance's own value of the feature, where its prediction sits on its curve.
Definition ice.hpp:49
std::vector< float > centered(int64_t anchor=0) const
Centered ICE (c-ICE): each curve minus its value at grid[anchor], so curves that differ only by an of...
std::vector< float > partial_dependence() const
The partial dependence curve: the mean of the curves at each grid point.
int64_t target_index
Definition ice.hpp:43
int64_t num_instances
Definition ice.hpp:45
Partial dependence on two features at once, over a grid of value pairs.
Definition ice.hpp:86
int64_t feature_y
Definition ice.hpp:88
std::vector< float > values
Row-major (grid_y.size(), grid_x.size()): values[r][c] is the mean prediction with feature_y = grid_y...
Definition ice.hpp:95
std::vector< float > grid_y
Definition ice.hpp:91
int64_t target_index
Definition ice.hpp:89
int64_t feature_x
Definition ice.hpp:87
std::vector< float > grid_x
Definition ice.hpp:90
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).