
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/04-interpolation-based/01-plot_isi_binary_classification.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_04-interpolation-based_01-plot_isi_binary_classification.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_04-interpolation-based_01-plot_isi_binary_classification.py:

Binary classification using ISI
===============================

.. GENERATED FROM PYTHON SOURCE LINES 6-8

Imports
-------

.. GENERATED FROM PYTHON SOURCE LINES 8-22

.. code-block:: Python


    import matplotlib.pyplot as plt
    import pandas as pd
    import seaborn as sns

    from uniharmony import verbosity
    from uniharmony.datasets import make_multisite_classification
    from uniharmony.interpolation import IntraSiteInterpolation


    sns.set_theme(style="whitegrid")
    verbosity("warning")









.. GENERATED FROM PYTHON SOURCE LINES 23-25

Data generation
---------------

.. GENERATED FROM PYTHON SOURCE LINES 25-38

.. code-block:: Python


    X, y, sites = make_multisite_classification(
        n_features=2,
        balance_per_site=[0.3, 0.7],
    )
    df = pd.DataFrame({"Target": y, "Site": sites})

    plt.figure(figsize=[10, 6])
    plt.title("Unbalanced classes by site")
    sns.countplot(df, x="Target", hue="Site")
    plt.grid(axis="y", color="black", alpha=0.5, linestyle="--")





.. image-sg:: /auto_examples/04-interpolation-based/images/sphx_glr_01-plot_isi_binary_classification_001.png
   :alt: Unbalanced classes by site
   :srcset: /auto_examples/04-interpolation-based/images/sphx_glr_01-plot_isi_binary_classification_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 39-41

Harmonisation
-------------

.. GENERATED FROM PYTHON SOURCE LINES 41-45

.. code-block:: Python


    isi = IntraSiteInterpolation("smote")
    X_r, y_r = isi.fit_resample(X, y, sites=sites)








.. GENERATED FROM PYTHON SOURCE LINES 46-48

Plotting
--------

.. GENERATED FROM PYTHON SOURCE LINES 48-62

.. code-block:: Python


    df = pd.DataFrame(
        {
            "Target": y_r,
            "Site": isi.sites_resampled_,
            "Feature 1": X_r[:, 0],
            "Feature 2": X_r[:, 1],
        }
    )
    plt.figure(figsize=[10, 6])
    plt.title("Balanced classes by site")
    sns.countplot(df, x="Target", hue="Site")
    plt.grid(axis="y", color="black", alpha=0.5, linestyle="--")




.. image-sg:: /auto_examples/04-interpolation-based/images/sphx_glr_01-plot_isi_binary_classification_002.png
   :alt: Balanced classes by site
   :srcset: /auto_examples/04-interpolation-based/images/sphx_glr_01-plot_isi_binary_classification_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 63-67

.. admonition:: Take-home message

   Now the classes are balanced within each site, thus a ML model would not be
   able to pick an Effect of Site signal to give a fraudulently good performance.


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

   **Total running time of the script:** (0 minutes 0.718 seconds)


.. _sphx_glr_download_auto_examples_04-interpolation-based_01-plot_isi_binary_classification.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 01-plot_isi_binary_classification.ipynb <01-plot_isi_binary_classification.ipynb>`

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

      :download:`Download Python source code: 01-plot_isi_binary_classification.py <01-plot_isi_binary_classification.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 01-plot_isi_binary_classification.zip <01-plot_isi_binary_classification.zip>`


.. only:: html

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

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