Time Limit
Some explanations are computationally expensive to compute. For this reason, most explanation methods accept a time_limit parameter (in seconds). If the time limit is reached, the method stops and returns either an approximation of the desired explanation or [] if no approximation was found. By default, time_limit is set to None, giving an unlimited amount of time.
All explainers expose an elapsed_time attribute set to the time in seconds taken by the last explanation method call. It is equal to Explaining.TIMEOUT if the time limit was reached.
The following methods accept a time_limit parameter:
| Method | DT | RF | BT | BT (regression) |
|---|---|---|---|---|
sufficient_reason | ✓ | ✓ | ✓ | |
minimal_sufficient_reason | ✓ | ✓ | ||
preferred_sufficient_reason | ✓ | |||
n_sufficient_reasons | ✓ | |||
n_sufficient_reasons_per_attribute | ✓ | |||
anchored_reason | ✓ | |||
minimal_contrastive_reason | ✓ | ✓ | ||
majoritary_reason | ✓ | |||
preferred_majoritary_reason | ✓ | |||
minimal_majoritary_reason | ✓ | |||
most_anchored_reason | ✓ | |||
tree_specific_reason | ✓ | ✓ | ||
minimal_tree_specific_reasons | ✓ | |||
range_for_partial_instance | ✓ |
The following example illustrates the usage of time_limit and elapsed_time:
from pyxai import Learning, Explaining
learner = Learning.Scikitlearn("../../dataset/iris.csv", problem_type=Learning.CLASSIFICATION)
model = learner.evaluate(splitting_method=Learning.HOLD_OUT, model_type=Learning.DT)
instance, prediction = learner.get_instances(model, n=1, is_correct=True)
explainer = Explaining.initialize(model, instance)
sufficient_reason = explainer.sufficient_reason(time_limit=10)
if explainer.elapsed_time == Explaining.TIMEOUT:
print("Timeout reached.")
print(f"sufficient_reason: {sufficient_reason} (approximation)")
else:
print(f"Time to compute: {explainer.elapsed_time:.3f}s")
print(f"sufficient_reason: {sufficient_reason}")