.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_pandas.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_pandas.py: ========================= Using moocore with Pandas ========================= This example shows how to use ``moocore`` functions with Pandas (https://pandas.pydata.org/). This example requires pandas version >= 2.0.0 .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import moocore import pandas as pd print(f"pandas version: {pd.__version__}") .. rst-class:: sphx-glr-script-out .. code-block:: none pandas version: 2.2.3 .. GENERATED FROM PYTHON SOURCE LINES 15-16 First, we create a toy Pandas :class:`~pandas.DataFrame`. .. GENERATED FROM PYTHON SOURCE LINES 16-27 .. code-block:: Python df = pd.DataFrame( dict( obj1=[1, 2, 3, 4, 5], obj2=[5, 4, 3, 2, 1], obj3=[100, 200, 200, 300, 100], algo=2 * ["foo"] + 2 * ["bar"] + ["foo"], ) ) df .. raw:: html
obj1 obj2 obj3 algo
0 1 5 100 foo
1 2 4 200 foo
2 3 3 200 bar
3 4 2 300 bar
4 5 1 100 foo


.. GENERATED FROM PYTHON SOURCE LINES 28-29 Normalize it (only replace the objective columns!). .. GENERATED FROM PYTHON SOURCE LINES 29-34 .. code-block:: Python obj_cols = ["obj1", "obj2", "obj3"] df[obj_cols] = moocore.normalise(df[obj_cols], to_range=[1, 2]) df .. raw:: html
obj1 obj2 obj3 algo
0 1.00 2.00 1.0 foo
1 1.25 1.75 1.5 foo
2 1.50 1.50 1.5 bar
3 1.75 1.25 2.0 bar
4 2.00 1.00 1.0 foo


.. GENERATED FROM PYTHON SOURCE LINES 35-36 Calculate the hypervolume for each ``algo`` using :meth:`~pandas.DataFrame.groupby` and :meth:`~pandas.core.groupby.DataFrameGroupBy.apply`. .. GENERATED FROM PYTHON SOURCE LINES 36-45 .. code-block:: Python ref = 2.1 hv = ( df.groupby("algo")[obj_cols] .apply(moocore.hypervolume, ref=ref) .reset_index(name="hv") ) hv .. raw:: html
algo hv
0 bar 0.22475
1 foo 0.34350


.. GENERATED FROM PYTHON SOURCE LINES 46-47 Or we can just use: .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: Python hv = moocore.apply_within_sets( df[obj_cols], df["algo"], moocore.hypervolume, ref=ref ) hv .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.3435 , 0.22475]) .. GENERATED FROM PYTHON SOURCE LINES 55-59 :func:`moocore.apply_within_sets()` processes each group in order, even if the elements of the same group are not contiguous. That is, it processes the groups like :meth:`pandas.Series.unique` and not like :class:`set` or :func:`numpy.unique()`. .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python df["algo"].unique() .. rst-class:: sphx-glr-script-out .. code-block:: none array(['foo', 'bar'], dtype=object) .. GENERATED FROM PYTHON SOURCE LINES 63-64 If we have multiple columns that we want to use to define the sets, such as ``algo`` and ``run``: .. GENERATED FROM PYTHON SOURCE LINES 64-77 .. code-block:: Python df = pd.DataFrame( dict( obj1=[1, 2, 3, 4, 5, 6, 5, 4, 3, 1], obj2=[6, 5, 4, 3, 2, 1, 5, 4, 5, 6], obj3=[1, 2, 3, 4, 5, 6, 6, 7, 5, 2], algo=["a"] * 3 + ["b"] * 3 + ["a", "b"] * 2, run=[1, 1, 2, 1, 1, 2, 2, 2, 1, 1], ) ) obj_cols = ["obj1", "obj2", "obj3"] df .. raw:: html
obj1 obj2 obj3 algo run
0 1 6 1 a 1
1 2 5 2 a 1
2 3 4 3 a 2
3 4 3 4 b 1
4 5 2 5 b 1
5 6 1 6 b 2
6 5 5 6 a 2
7 4 4 7 b 2
8 3 5 5 a 1
9 1 6 2 b 1


.. GENERATED FROM PYTHON SOURCE LINES 78-79 We can still use :meth:`~pandas.DataFrame.groupby` but we may need to reset and clean-up the index. .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: Python df.groupby(["algo", "run"])[obj_cols].apply( moocore.filter_dominated ).reset_index(level=["algo", "run"]) .. raw:: html
algo run obj1 obj2 obj3
0 a 1 1 6 1
1 a 1 2 5 2
2 a 2 3 4 3
3 b 1 4 3 4
4 b 1 5 2 5
9 b 1 1 6 2
5 b 2 6 1 6
7 b 2 4 4 7


.. GENERATED FROM PYTHON SOURCE LINES 85-87 Or we can combine the multiple columns as one to define the sets: .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python sets = df["algo"].astype(str) + "-" + df["run"].astype(str) sets .. rst-class:: sphx-glr-script-out .. code-block:: none 0 a-1 1 a-1 2 a-2 3 b-1 4 b-1 5 b-2 6 a-2 7 b-2 8 a-1 9 b-1 dtype: object .. GENERATED FROM PYTHON SOURCE LINES 91-93 then identify nondominated rows within each set: .. GENERATED FROM PYTHON SOURCE LINES 93-96 .. code-block:: Python is_nondom = moocore.is_nondominated_within_sets(df[obj_cols], sets=sets) is_nondom .. rst-class:: sphx-glr-script-out .. code-block:: none array([ True, True, True, True, True, True, False, True, False, True]) .. GENERATED FROM PYTHON SOURCE LINES 97-99 And use the boolean vector above to filter rows: .. GENERATED FROM PYTHON SOURCE LINES 99-100 .. code-block:: Python df[is_nondom] .. raw:: html
obj1 obj2 obj3 algo run
0 1 6 1 a 1
1 2 5 2 a 1
2 3 4 3 a 2
3 4 3 4 b 1
4 5 2 5 b 1
5 6 1 6 b 2
7 4 4 7 b 2
9 1 6 2 b 1


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.236 seconds) .. _sphx_glr_download_auto_examples_plot_pandas.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pandas.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pandas.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pandas.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_