{ "cells": [ { "cell_type": "markdown", "id": "9cbc01bf", "metadata": {}, "source": [ "# Time Limit" ] }, { "cell_type": "markdown", "id": "94210ca1", "metadata": {}, "source": [ "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.\n" ] }, { "cell_type": "markdown", "id": "e4dfe8a0", "metadata": {}, "source": [ "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.\n" ] }, { "cell_type": "markdown", "id": "c3843608", "metadata": {}, "source": [ "The following methods accept a ```time_limit``` parameter:\n", "\n", "| Method | DT | RF | BT | BT (regression) |\n", "|:-------|:--:|:--:|:--:|:---------:|\n", "| ```sufficient_reason``` | ✓ | ✓ | | ✓ |\n", "| ```minimal_sufficient_reason``` | ✓ | ✓ | | |\n", "| ```preferred_sufficient_reason``` | ✓ | | | |\n", "| ```n_sufficient_reasons``` | ✓ | | | |\n", "| ```n_sufficient_reasons_per_attribute``` | ✓ | | | |\n", "| ```anchored_reason``` | ✓ | | | |\n", "| ```minimal_contrastive_reason``` | | ✓ | ✓ | |\n", "| ```majoritary_reason``` | | ✓ | | |\n", "| ```preferred_majoritary_reason``` | | ✓ | | |\n", "| ```minimal_majoritary_reason``` | | ✓ | | |\n", "| ```most_anchored_reason``` | | ✓ | | |\n", "| ```tree_specific_reason``` | | | ✓ | ✓ |\n", "| ```minimal_tree_specific_reasons``` | | | ✓ | |\n", "| ```range_for_partial_instance``` | | | | ✓ |\n" ] }, { "cell_type": "markdown", "id": "af439d32", "metadata": {}, "source": [ "The following example illustrates the usage of ```time_limit``` and ```elapsed_time```:\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2d7d29af", "metadata": {}, "outputs": [], "source": [ "from pyxai import Learning, Explaining\n", "\n", "learner = Learning.Scikitlearn(\"../../dataset/iris.csv\", problem_type=Learning.CLASSIFICATION)\n", "model = learner.evaluate(splitting_method=Learning.HOLD_OUT, model_type=Learning.DT)\n", "instance, prediction = learner.get_instances(model, n=1, is_correct=True)\n", "explainer = Explaining.initialize(model, instance)\n", "\n", "sufficient_reason = explainer.sufficient_reason(time_limit=10)\n", "if explainer.elapsed_time == Explaining.TIMEOUT:\n", " print(\"Timeout reached.\")\n", " print(f\"sufficient_reason: {sufficient_reason} (approximation)\")\n", "else:\n", " print(f\"Time to compute: {explainer.elapsed_time:.3f}s\")\n", " print(f\"sufficient_reason: {sufficient_reason}\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }