
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/plot_diabetesFeatures_importance_example.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_plot_diabetesFeatures_importance_example.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_plot_diabetesFeatures_importance_example.py:


Variable Importance on diabetes dataset
=======================================

This example compares the standard permutation approach for variable importance
and its conditional variant on the diabetes dataset for the single-level case.

.. GENERATED FROM PYTHON SOURCE LINES 10-12

Imports needed for this script
------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 12-31

.. code-block:: Python


    import numpy as np
    from hidimstat.BBI import BlockBasedImportance
    from sklearn.datasets import load_diabetes
    import matplotlib.pyplot as plt

    plt.rcParams.update({"font.size": 14})

    # Fixing the random seed
    rng = np.random.RandomState(2024)

    diabetes = load_diabetes()
    X, y = diabetes.data, diabetes.target

    # Use or not a cross-validation with the provided learner
    k_fold = 2
    # Identifying the categorical (nominal & ordinal) variables
    list_nominal = {}








.. GENERATED FROM PYTHON SOURCE LINES 32-34

Standard Variable Importance
----------------------------

.. GENERATED FROM PYTHON SOURCE LINES 34-54

.. code-block:: Python


    bbi_perm = BlockBasedImportance(
        estimator="RF",
        importance_estimator="Mod_RF",
        do_hyper=True,
        dict_hyper=None,
        conditional=False,
        group_stacking=False,
        prob_type="regression",
        k_fold=k_fold,
        list_nominal=list_nominal,
        n_jobs=10,
        verbose=0,
        n_perm=100,
    )
    bbi_perm.fit(X, y)
    print("Computing the importance scores with standard permutation")
    results_perm = bbi_perm.compute_importance()
    pvals_perm = -np.log10(results_perm["pval"] + 1e-10)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Processing: 1
    Processing: 2
    Computing the importance scores with standard permutation




.. GENERATED FROM PYTHON SOURCE LINES 55-57

Conditional Variable Importance
-------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 57-77

.. code-block:: Python


    bbi_cond = BlockBasedImportance(
        estimator="RF",
        importance_estimator="Mod_RF",
        do_hyper=True,
        dict_hyper=None,
        conditional=True,
        group_stacking=False,
        prob_type="regression",
        k_fold=k_fold,
        list_nominal=list_nominal,
        n_jobs=10,
        verbose=0,
        n_perm=100,
    )
    bbi_cond.fit(X, y)
    print("Computing the importance scores with conditional permutation")
    results_cond = bbi_cond.compute_importance()
    pvals_cond = -np.log10(results_cond["pval"] + 1e-5)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Processing: 1
    Processing: 2
    Computing the importance scores with conditional permutation




.. GENERATED FROM PYTHON SOURCE LINES 78-80

Plotting the comparison
-----------------------

.. GENERATED FROM PYTHON SOURCE LINES 80-103

.. code-block:: Python


    list_res = {"Perm": [], "Cond": []}
    for ind_el, el in enumerate(diabetes.feature_names):
        list_res["Perm"].append(pvals_perm[ind_el][0])
        list_res["Cond"].append(pvals_cond[ind_el][0])

    x = np.arange(len(diabetes.feature_names))
    width = 0.25  # the width of the bars
    multiplier = 0
    fig, ax = plt.subplots(figsize=(5, 5), layout="constrained")

    for attribute, measurement in list_res.items():
        offset = width * multiplier
        rects = ax.bar(x + offset, measurement, width, label=attribute)
        multiplier += 1

    ax.set_ylabel(r"$-log_{10}p_{val}$")
    ax.set_xticks(x + width / 2, diabetes.feature_names)
    ax.legend(loc="upper left", ncols=2)
    ax.set_ylim(0, 3)
    ax.axhline(y=-np.log10(0.05), color="r", linestyle="-")

    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_plot_diabetesFeatures_importance_example_001.png
   :alt: plot diabetesFeatures importance example
   :srcset: /auto_examples/images/sphx_glr_plot_diabetesFeatures_importance_example_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (1 minutes 14.960 seconds)


.. _sphx_glr_download_auto_examples_plot_diabetesFeatures_importance_example.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_diabetesFeatures_importance_example.ipynb <plot_diabetesFeatures_importance_example.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_diabetesFeatures_importance_example.py <plot_diabetesFeatures_importance_example.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
