arborenum 0.2.7__tar.gz → 0.2.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {arborenum-0.2.7/src/arborenum.egg-info → arborenum-0.2.8}/PKG-INFO +1 -1
- {arborenum-0.2.7 → arborenum-0.2.8}/pyproject.toml +1 -1
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum/__init__.py +44 -1
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum/_core.cpp +56 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum/cpp/arborenum.cpp +196 -0
- {arborenum-0.2.7 → arborenum-0.2.8/src/arborenum.egg-info}/PKG-INFO +1 -1
- {arborenum-0.2.7 → arborenum-0.2.8}/MANIFEST.in +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/README.md +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/setup.cfg +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/setup.py +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum/_threshold_guessing.py +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum/cpp/rid.cpp +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum/single_tree.py +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum.egg-info/SOURCES.txt +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum.egg-info/dependency_links.txt +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum.egg-info/requires.txt +0 -0
- {arborenum-0.2.7 → arborenum-0.2.8}/src/arborenum.egg-info/top_level.txt +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "arborenum"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.8"
|
|
8
8
|
description = "Exact, approximate, and anytime decision tree Rashomon sets over binary and continuous features"
|
|
9
9
|
requires-python = ">=3.9"
|
|
10
10
|
dependencies = [
|
|
@@ -2402,6 +2402,18 @@ class ArborEnum:
|
|
|
2402
2402
|
return intervals
|
|
2403
2403
|
|
|
2404
2404
|
|
|
2405
|
+
def permutation_importance_bounds(self, X_eval, y_eval, *, budget_override=None):
|
|
2406
|
+
import pandas as pd
|
|
2407
|
+
|
|
2408
|
+
bounds = self.get_exact_replacement_importance_intervals(
|
|
2409
|
+
X_eval, y_eval, budget_override=budget_override
|
|
2410
|
+
)
|
|
2411
|
+
return pd.DataFrame(
|
|
2412
|
+
bounds,
|
|
2413
|
+
index=pd.Index(self.feature_names_in_, name="feature", tupleize_cols=False),
|
|
2414
|
+
columns=["min_importance", "max_importance"],
|
|
2415
|
+
)
|
|
2416
|
+
|
|
2405
2417
|
def get_wald_divisive_model_reliance_intervals(
|
|
2406
2418
|
self,
|
|
2407
2419
|
X_eval,
|
|
@@ -3954,6 +3966,34 @@ class ArborEnum:
|
|
|
3954
3966
|
)
|
|
3955
3967
|
)
|
|
3956
3968
|
|
|
3969
|
+
def _prediction_query(self, training_query, eval_query, sample_idx, budget, X):
|
|
3970
|
+
budget = -1 if budget is None else budget
|
|
3971
|
+
if X is None:
|
|
3972
|
+
if sample_idx is None:
|
|
3973
|
+
raise TypeError("Pass sample_idx or X.")
|
|
3974
|
+
return training_query(sample_idx, budget)
|
|
3975
|
+
if sample_idx is not None:
|
|
3976
|
+
raise TypeError("Pass either sample_idx or X, not both.")
|
|
3977
|
+
|
|
3978
|
+
X = np.asarray(X)
|
|
3979
|
+
single_row = X.ndim == 1
|
|
3980
|
+
if single_row:
|
|
3981
|
+
X = X[None, :]
|
|
3982
|
+
results = eval_query(self.transform(X), budget)
|
|
3983
|
+
return results[0] if single_row else results
|
|
3984
|
+
|
|
3985
|
+
def prediction_counts(self, sample_idx=None, budget=None, *, X=None):
|
|
3986
|
+
return self._prediction_query(
|
|
3987
|
+
self._model.prediction_counts, self._model.prediction_counts_for_rows,
|
|
3988
|
+
sample_idx, budget, X
|
|
3989
|
+
)
|
|
3990
|
+
|
|
3991
|
+
def has_disagreement(self, sample_idx=None, budget=None, *, X=None):
|
|
3992
|
+
return self._prediction_query(
|
|
3993
|
+
self._model.has_disagreement, self._model.has_disagreement_for_rows,
|
|
3994
|
+
sample_idx, budget, X
|
|
3995
|
+
)
|
|
3996
|
+
|
|
3957
3997
|
def training_sample_has_multiple_reachable_predictions(self, sample_idx: int):
|
|
3958
3998
|
return self._model.training_sample_has_multiple_reachable_predictions(int(sample_idx))
|
|
3959
3999
|
|
|
@@ -5586,4 +5626,7 @@ def rid_plot_all_pairs(
|
|
|
5586
5626
|
if show:
|
|
5587
5627
|
plt.show()
|
|
5588
5628
|
|
|
5589
|
-
return fig, axes
|
|
5629
|
+
return fig, axes
|
|
5630
|
+
|
|
5631
|
+
from .single_tree import ArborEnumTree
|
|
5632
|
+
__all__.append("ArborEnumTree")
|
|
@@ -8,6 +8,17 @@
|
|
|
8
8
|
|
|
9
9
|
namespace py = pybind11;
|
|
10
10
|
|
|
11
|
+
static py::dict prediction_counts_dict(const std::vector<py::int_>& counts) {
|
|
12
|
+
py::dict out;
|
|
13
|
+
for (std::size_t c = 0; c + 1 < counts.size(); ++c) {
|
|
14
|
+
out[py::int_(c)] = counts[c];
|
|
15
|
+
}
|
|
16
|
+
if (!counts.back().equal(py::int_(0))) {
|
|
17
|
+
out[py::int_(DEFER_PREDICTION)] = counts.back();
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
11
22
|
static std::vector<std::vector<double>>
|
|
12
23
|
numpy_double_2d_to_row_major(
|
|
13
24
|
py::array_t<double, py::array::c_style | py::array::forcecast> X,
|
|
@@ -2475,6 +2486,51 @@ PYBIND11_MODULE(_core, m) {
|
|
|
2475
2486
|
std::vector<std::vector<int>>{}
|
|
2476
2487
|
)
|
|
2477
2488
|
|
|
2489
|
+
.def(
|
|
2490
|
+
"prediction_counts",
|
|
2491
|
+
[](const ArborEnum& self, int sample_idx, int budget) {
|
|
2492
|
+
return prediction_counts_dict(self.prediction_counts<py::int_>(sample_idx, budget));
|
|
2493
|
+
},
|
|
2494
|
+
py::arg("sample_idx"),
|
|
2495
|
+
py::arg("budget") = -1
|
|
2496
|
+
)
|
|
2497
|
+
.def(
|
|
2498
|
+
"has_disagreement",
|
|
2499
|
+
&ArborEnum::has_disagreement,
|
|
2500
|
+
py::arg("sample_idx"),
|
|
2501
|
+
py::arg("budget") = -1
|
|
2502
|
+
)
|
|
2503
|
+
.def(
|
|
2504
|
+
"prediction_counts_for_rows",
|
|
2505
|
+
[](const ArborEnum& self,
|
|
2506
|
+
py::array_t<uint8_t, py::array::c_style | py::array::forcecast> X,
|
|
2507
|
+
int budget) {
|
|
2508
|
+
if (X.ndim() != 2) {
|
|
2509
|
+
throw std::invalid_argument("X must be 2D.");
|
|
2510
|
+
}
|
|
2511
|
+
py::list out;
|
|
2512
|
+
for (const auto& counts : self.prediction_counts_for_rows<py::int_>(
|
|
2513
|
+
X.data(), X.shape(0), X.shape(1), budget)) {
|
|
2514
|
+
out.append(prediction_counts_dict(counts));
|
|
2515
|
+
}
|
|
2516
|
+
return out;
|
|
2517
|
+
},
|
|
2518
|
+
py::arg("X"),
|
|
2519
|
+
py::arg("budget") = -1
|
|
2520
|
+
)
|
|
2521
|
+
.def(
|
|
2522
|
+
"has_disagreement_for_rows",
|
|
2523
|
+
[](const ArborEnum& self,
|
|
2524
|
+
py::array_t<uint8_t, py::array::c_style | py::array::forcecast> X,
|
|
2525
|
+
int budget) {
|
|
2526
|
+
if (X.ndim() != 2) {
|
|
2527
|
+
throw std::invalid_argument("X must be 2D.");
|
|
2528
|
+
}
|
|
2529
|
+
return self.has_disagreement_for_rows(X.data(), X.shape(0), X.shape(1), budget);
|
|
2530
|
+
},
|
|
2531
|
+
py::arg("X"),
|
|
2532
|
+
py::arg("budget") = -1
|
|
2533
|
+
)
|
|
2478
2534
|
.def(
|
|
2479
2535
|
"training_samples_with_multiple_reachable_predictions",
|
|
2480
2536
|
[](const ArborEnum &self) {
|
|
@@ -6234,6 +6234,158 @@ private:
|
|
|
6234
6234
|
return out;
|
|
6235
6235
|
}
|
|
6236
6236
|
|
|
6237
|
+
int prediction_budget_(int budget) const {
|
|
6238
|
+
if (!result) {
|
|
6239
|
+
throw std::runtime_error("No Rashomon trie has been constructed. Call fit() first.");
|
|
6240
|
+
}
|
|
6241
|
+
if (budget < -1 || budget > result->budget) {
|
|
6242
|
+
throw std::invalid_argument("budget must be -1 or between 0 and the fitted Rashomon budget.");
|
|
6243
|
+
}
|
|
6244
|
+
return budget == -1 ? result->budget : budget;
|
|
6245
|
+
}
|
|
6246
|
+
|
|
6247
|
+
template <typename Count>
|
|
6248
|
+
using PredictionHistogram = std::map<std::pair<int, int>, Count>;
|
|
6249
|
+
|
|
6250
|
+
template <typename Count>
|
|
6251
|
+
using PredictionHistogramCache = std::unordered_map<
|
|
6252
|
+
const TreeTrieNode*, std::map<std::pair<int, bool>, PredictionHistogram<Count>>
|
|
6253
|
+
>;
|
|
6254
|
+
|
|
6255
|
+
template <typename Count, typename ValueAt>
|
|
6256
|
+
const PredictionHistogram<Count>& prediction_histogram_(
|
|
6257
|
+
const TreeTrieNode* node,
|
|
6258
|
+
int budget,
|
|
6259
|
+
const ValueAt& value_at,
|
|
6260
|
+
bool routed,
|
|
6261
|
+
PredictionHistogramCache<Count>& cache
|
|
6262
|
+
) const {
|
|
6263
|
+
budget = std::min(budget, node->budget);
|
|
6264
|
+
auto [entry, inserted] = cache[node].try_emplace({budget, routed});
|
|
6265
|
+
auto& hist = entry->second;
|
|
6266
|
+
if (!inserted || node->min_objective > budget) {
|
|
6267
|
+
return hist;
|
|
6268
|
+
}
|
|
6269
|
+
|
|
6270
|
+
for (const auto& leaf : node->leaves) {
|
|
6271
|
+
if (leaf.loss <= budget) {
|
|
6272
|
+
auto& count = hist[{leaf.loss, routed ? leaf.prediction : DEFER_PREDICTION}];
|
|
6273
|
+
count = count + Count(1);
|
|
6274
|
+
}
|
|
6275
|
+
}
|
|
6276
|
+
|
|
6277
|
+
for (const auto& split : node->splits) {
|
|
6278
|
+
const auto* child = split.left.get();
|
|
6279
|
+
const auto* sibling = split.right.get();
|
|
6280
|
+
if (routed && !value_at(split.feature)) {
|
|
6281
|
+
std::swap(child, sibling);
|
|
6282
|
+
}
|
|
6283
|
+
if (!child || !sibling || child->min_objective > budget ||
|
|
6284
|
+
sibling->min_objective > budget - child->min_objective) {
|
|
6285
|
+
continue;
|
|
6286
|
+
}
|
|
6287
|
+
|
|
6288
|
+
const auto& child_hist = prediction_histogram_(
|
|
6289
|
+
child, budget - sibling->min_objective, value_at, routed, cache
|
|
6290
|
+
);
|
|
6291
|
+
const auto& sibling_hist = prediction_histogram_(
|
|
6292
|
+
sibling, budget - child->min_objective, value_at, false, cache
|
|
6293
|
+
);
|
|
6294
|
+
for (const auto& [child_key, child_count] : child_hist) {
|
|
6295
|
+
const auto [objective, prediction] = child_key;
|
|
6296
|
+
for (const auto& [sibling_key, sibling_count] : sibling_hist) {
|
|
6297
|
+
const int sibling_objective = sibling_key.first;
|
|
6298
|
+
if (sibling_objective > budget - objective) {
|
|
6299
|
+
break;
|
|
6300
|
+
}
|
|
6301
|
+
auto& count = hist[{objective + sibling_objective, prediction}];
|
|
6302
|
+
count = count + child_count * sibling_count;
|
|
6303
|
+
}
|
|
6304
|
+
}
|
|
6305
|
+
}
|
|
6306
|
+
return hist;
|
|
6307
|
+
}
|
|
6308
|
+
|
|
6309
|
+
template <typename ValueAt>
|
|
6310
|
+
bool prediction_disagrees_(
|
|
6311
|
+
const TreeTrieNode* node,
|
|
6312
|
+
int budget,
|
|
6313
|
+
const ValueAt& value_at,
|
|
6314
|
+
int& first_class,
|
|
6315
|
+
std::unordered_map<const TreeTrieNode*, int>& visited
|
|
6316
|
+
) const {
|
|
6317
|
+
budget = std::min(budget, node->budget);
|
|
6318
|
+
if (node->min_objective > budget) {
|
|
6319
|
+
return false;
|
|
6320
|
+
}
|
|
6321
|
+
auto [entry, inserted] = visited.try_emplace(node, budget);
|
|
6322
|
+
if (!inserted && entry->second >= budget) {
|
|
6323
|
+
return false;
|
|
6324
|
+
}
|
|
6325
|
+
entry->second = budget;
|
|
6326
|
+
|
|
6327
|
+
for (const auto& leaf : node->leaves) {
|
|
6328
|
+
if (leaf.loss > budget || leaf.prediction == DEFER_PREDICTION) {
|
|
6329
|
+
continue;
|
|
6330
|
+
}
|
|
6331
|
+
if (first_class != DEFER_PREDICTION && leaf.prediction != first_class) {
|
|
6332
|
+
return true;
|
|
6333
|
+
}
|
|
6334
|
+
first_class = leaf.prediction;
|
|
6335
|
+
}
|
|
6336
|
+
|
|
6337
|
+
for (const auto& split : node->splits) {
|
|
6338
|
+
const bool go_left = value_at(split.feature);
|
|
6339
|
+
const auto* child = (go_left ? split.left : split.right).get();
|
|
6340
|
+
const auto* sibling = (go_left ? split.right : split.left).get();
|
|
6341
|
+
if (child && sibling && sibling->min_objective <= budget &&
|
|
6342
|
+
prediction_disagrees_(
|
|
6343
|
+
child, budget - sibling->min_objective, value_at, first_class, visited
|
|
6344
|
+
)) {
|
|
6345
|
+
return true;
|
|
6346
|
+
}
|
|
6347
|
+
}
|
|
6348
|
+
return false;
|
|
6349
|
+
}
|
|
6350
|
+
|
|
6351
|
+
template <typename Count, typename ValueAt>
|
|
6352
|
+
std::vector<Count> prediction_counts_(int budget, const ValueAt& value_at) const {
|
|
6353
|
+
PredictionHistogramCache<Count> cache;
|
|
6354
|
+
const auto& hist = prediction_histogram_(
|
|
6355
|
+
result.get(), budget, value_at, true, cache
|
|
6356
|
+
);
|
|
6357
|
+
std::vector<Count> counts(static_cast<std::size_t>(num_classes) + 1);
|
|
6358
|
+
for (const auto& [key, count] : hist) {
|
|
6359
|
+
const int prediction = key.second;
|
|
6360
|
+
const int index = prediction == DEFER_PREDICTION ? num_classes : prediction;
|
|
6361
|
+
counts[index] = counts[index] + count;
|
|
6362
|
+
}
|
|
6363
|
+
return counts;
|
|
6364
|
+
}
|
|
6365
|
+
|
|
6366
|
+
template <typename ValueAt>
|
|
6367
|
+
bool has_disagreement_(int budget, const ValueAt& value_at) const {
|
|
6368
|
+
int first_class = DEFER_PREDICTION;
|
|
6369
|
+
std::unordered_map<const TreeTrieNode*, int> visited;
|
|
6370
|
+
return prediction_disagrees_(result.get(), budget, value_at, first_class, visited);
|
|
6371
|
+
}
|
|
6372
|
+
|
|
6373
|
+
int training_prediction_budget_(int sample_idx, int budget) const {
|
|
6374
|
+
budget = prediction_budget_(budget);
|
|
6375
|
+
if (sample_idx < 0 || sample_idx >= n_samples) {
|
|
6376
|
+
throw std::out_of_range("sample_idx is out of range.");
|
|
6377
|
+
}
|
|
6378
|
+
return budget;
|
|
6379
|
+
}
|
|
6380
|
+
|
|
6381
|
+
int eval_prediction_budget_(std::size_t features, int budget) const {
|
|
6382
|
+
budget = prediction_budget_(budget);
|
|
6383
|
+
if (features != static_cast<std::size_t>(n_features)) {
|
|
6384
|
+
throw std::invalid_argument("X has the wrong number of internal features.");
|
|
6385
|
+
}
|
|
6386
|
+
return budget;
|
|
6387
|
+
}
|
|
6388
|
+
|
|
6237
6389
|
bool training_value_(int sample_idx, int feat) const {
|
|
6238
6390
|
if (sample_idx < 0 || sample_idx >= n_samples) {
|
|
6239
6391
|
throw std::runtime_error("training_value_ got out-of-range sample index.");
|
|
@@ -23529,6 +23681,50 @@ public:
|
|
|
23529
23681
|
);
|
|
23530
23682
|
}
|
|
23531
23683
|
|
|
23684
|
+
template <typename Count>
|
|
23685
|
+
std::vector<Count> prediction_counts(int sample_idx, int budget = -1) const {
|
|
23686
|
+
budget = training_prediction_budget_(sample_idx, budget);
|
|
23687
|
+
return prediction_counts_<Count>(budget, [this, sample_idx](int feature) {
|
|
23688
|
+
return training_value_(sample_idx, feature);
|
|
23689
|
+
});
|
|
23690
|
+
}
|
|
23691
|
+
|
|
23692
|
+
bool has_disagreement(int sample_idx, int budget = -1) const {
|
|
23693
|
+
budget = training_prediction_budget_(sample_idx, budget);
|
|
23694
|
+
return has_disagreement_(budget, [this, sample_idx](int feature) {
|
|
23695
|
+
return training_value_(sample_idx, feature);
|
|
23696
|
+
});
|
|
23697
|
+
}
|
|
23698
|
+
|
|
23699
|
+
template <typename Count>
|
|
23700
|
+
std::vector<std::vector<Count>> prediction_counts_for_rows(
|
|
23701
|
+
const uint8_t* rows, std::size_t samples, std::size_t features, int budget
|
|
23702
|
+
) const {
|
|
23703
|
+
budget = eval_prediction_budget_(features, budget);
|
|
23704
|
+
std::vector<std::vector<Count>> counts;
|
|
23705
|
+
counts.reserve(samples);
|
|
23706
|
+
for (std::size_t i = 0; i < samples; ++i) {
|
|
23707
|
+
counts.push_back(prediction_counts_<Count>(budget, [rows, i, features](int feature) {
|
|
23708
|
+
return rows[i * features + feature] != 0;
|
|
23709
|
+
}));
|
|
23710
|
+
}
|
|
23711
|
+
return counts;
|
|
23712
|
+
}
|
|
23713
|
+
|
|
23714
|
+
std::vector<bool> has_disagreement_for_rows(
|
|
23715
|
+
const uint8_t* rows, std::size_t samples, std::size_t features, int budget
|
|
23716
|
+
) const {
|
|
23717
|
+
budget = eval_prediction_budget_(features, budget);
|
|
23718
|
+
std::vector<bool> disagreement;
|
|
23719
|
+
disagreement.reserve(samples);
|
|
23720
|
+
for (std::size_t i = 0; i < samples; ++i) {
|
|
23721
|
+
disagreement.push_back(has_disagreement_(budget, [rows, i, features](int feature) {
|
|
23722
|
+
return rows[i * features + feature] != 0;
|
|
23723
|
+
}));
|
|
23724
|
+
}
|
|
23725
|
+
return disagreement;
|
|
23726
|
+
}
|
|
23727
|
+
|
|
23532
23728
|
bool training_sample_has_multiple_reachable_predictions(
|
|
23533
23729
|
int sample_idx
|
|
23534
23730
|
) const {
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|