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

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

.. _sphx_glr_auto_examples_01-basic-examples_04-plot_eos_with_dimensionality_reduction.py:


Explore EoS with dimensionality reduction techniques
====================================================

The first step before applying any harmonization technique is to understand and characterize our data.

Usually, we deal with high dimensional data. In order to reduce the dimensions of the problem and see how much target and Effect of Site (EoS) information there is in our data.

Let's explore how simulated data looks like using different dimensionality reduction methods.

For this we will use the function ``plot_2d_projection`` of ``uniharmony``, that allows us to pass our data and automatically generate a scatter plot.

.. GENERATED FROM PYTHON SOURCE LINES 15-17

Imports
-------

.. GENERATED FROM PYTHON SOURCE LINES 17-30

.. code-block:: Python


    import matplotlib.pyplot as plt
    import seaborn as sns
    from sklearn.decomposition import FastICA

    from uniharmony import verbosity
    from uniharmony.datasets import make_multisite_classification
    from uniharmony.plot import plot_2d_projection


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








.. GENERATED FROM PYTHON SOURCE LINES 31-34

Data generation
---------------
Let's simulate data with only Effects of Site (EoS) or only real signal, and see how tSNE groups the target and Eos.

.. GENERATED FROM PYTHON SOURCE LINES 34-39

.. code-block:: Python


    X, y, sites = make_multisite_classification(n_sites=5, signal_strength=10, site_effect_strength=0)
    # Using the function as default. The function will use t-SNE as default dimensionality reductor.
    plot_2d_projection(X, sites, y)




.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_001.png
   :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using tsne - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using tsne - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))



.. GENERATED FROM PYTHON SOURCE LINES 40-45

.. code-block:: Python


    X, y, sites = make_multisite_classification(n_sites=5, signal_strength=0, site_effect_strength=10, random_state=23)
    plot_2d_projection(X, sites, y)
    plt.show()




.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_002.png
   :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    2026-05-18 13:03:39 [warning  ] signal_strength is 0. Adding a delta (1e-6) to signal_strength to avoid degenerate data.




.. GENERATED FROM PYTHON SOURCE LINES 46-50

In the first plot, we see that classes are perfectly separated and the sites are all mixed in the clusters.
This is because tSNE used the target information to get the clusters, as there was no EoS information.

In the second plot, exactly the opposite happened, tSNE clustered the sites almost perfectly.

.. GENERATED FROM PYTHON SOURCE LINES 52-53

Let's try now another dimensionality reduction method.

.. GENERATED FROM PYTHON SOURCE LINES 55-60

.. code-block:: Python


    X, y, sites = make_multisite_classification(n_classes=2, n_sites=5, n_features=1200, signal_strength=10, site_effect_strength=0)
    # We can also pass the method as string. Only a few methods are available to pass as string in "method"
    plot_2d_projection(X, sites, y, method="pca")




.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_003.png
   :alt: 2D Projection using pca - Colored by site, 2D Projection using pca - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_003.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using pca - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using pca - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))



.. GENERATED FROM PYTHON SOURCE LINES 61-67

.. code-block:: Python


    X, y, sites = make_multisite_classification(n_classes=2, n_sites=5, n_features=120, signal_strength=0, site_effect_strength=10)
    fig, axes = plot_2d_projection(X, sites, y, method="pca")
    plt.show()





.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_004.png
   :alt: 2D Projection using pca - Colored by site, 2D Projection using pca - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_004.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    2026-05-18 13:03:44 [warning  ] signal_strength is 0. Adding a delta (1e-6) to signal_strength to avoid degenerate data.




.. GENERATED FROM PYTHON SOURCE LINES 68-70

Dimensionality reduction
------------------------

.. GENERATED FROM PYTHON SOURCE LINES 70-76

.. code-block:: Python


    dim_reductor = FastICA(n_components=2)
    X, y, sites = make_multisite_classification(n_classes=2, n_sites=5, n_features=1200, signal_strength=10, site_effect_strength=0)
    # We can also pass directly an instance of the dimensionality reductor that we want to use.
    plot_2d_projection(X, sites, y, dim_reductor=dim_reductor)




.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_005.png
   :alt: 2D Projection using fastica - Colored by site, 2D Projection using fastica - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_005.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using fastica - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using fastica - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))



.. GENERATED FROM PYTHON SOURCE LINES 77-83

.. code-block:: Python


    X, y, sites = make_multisite_classification(n_classes=2, n_sites=5, n_features=120, signal_strength=0, site_effect_strength=10)
    fig, axes = plot_2d_projection(X, sites, y, dim_reductor=dim_reductor)
    plt.show()





.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_006.png
   :alt: 2D Projection using fastica - Colored by site, 2D Projection using fastica - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_04-plot_eos_with_dimensionality_reduction_006.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    2026-05-18 13:03:46 [warning  ] signal_strength is 0. Adding a delta (1e-6) to signal_strength to avoid degenerate data.




.. GENERATED FROM PYTHON SOURCE LINES 84-85

We found similar behavior using PCA os FastICA, but different clusters were generated.


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

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


.. _sphx_glr_download_auto_examples_01-basic-examples_04-plot_eos_with_dimensionality_reduction.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 04-plot_eos_with_dimensionality_reduction.ipynb <04-plot_eos_with_dimensionality_reduction.ipynb>`

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

      :download:`Download Python source code: 04-plot_eos_with_dimensionality_reduction.py <04-plot_eos_with_dimensionality_reduction.py>`

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

      :download:`Download zipped: 04-plot_eos_with_dimensionality_reduction.zip <04-plot_eos_with_dimensionality_reduction.zip>`


.. only:: html

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

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