.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/alternatives.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_sampling_alternatives.py: List of alternatives ==================== Script reading the list of alternatives and identifying subsets Michel Bierlaire Fri Jul 25 2025, 17:43:34 .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python import pandas as pd from IPython.core.display_functions import display from biogeme.partition import Partition .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python alternatives = pd.read_csv('restaurants.dat') display(alternatives) .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python ID_COLUMN = 'ID' .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: Python all_alternatives = set(list(alternatives[ID_COLUMN])) .. GENERATED FROM PYTHON SOURCE LINES 28-29 Set of Asian restaurants .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python asian = set(alternatives[alternatives['Asian'] == 1][ID_COLUMN]) print(f'Number of asian restaurants: {len(asian)}') .. GENERATED FROM PYTHON SOURCE LINES 33-34 Set of restaurants located in downtown .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python downtown = set(alternatives[alternatives['downtown'] == 1][ID_COLUMN]) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Set of Asian restaurants in downtown .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python asian_and_downtown = asian & downtown .. GENERATED FROM PYTHON SOURCE LINES 41-42 Set of Asian restaurants, and of restaurants in downtown .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python asian_or_downtown = asian | downtown .. GENERATED FROM PYTHON SOURCE LINES 45-46 Set of Asian restaurants not in downtown .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python only_asian = asian - asian_and_downtown .. GENERATED FROM PYTHON SOURCE LINES 49-50 Set of non-Asian restaurants in downtown .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python only_downtown = downtown - asian_and_downtown .. GENERATED FROM PYTHON SOURCE LINES 53-54 Set of restaurants that are neither Asian nor in downtown .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python others = all_alternatives - asian_or_downtown .. GENERATED FROM PYTHON SOURCE LINES 58-63 .. code-block:: Python def complement(a_set: set[int]) -> set[int]: """Returns the complement of a set""" return all_alternatives - a_set .. GENERATED FROM PYTHON SOURCE LINES 64-65 Partitions. .. GENERATED FROM PYTHON SOURCE LINES 65-75 .. code-block:: Python partition_asian = Partition([asian, complement(asian)], full_set=all_alternatives) partition_downtown = Partition( [downtown, complement(downtown)], full_set=all_alternatives ) partition_uniform = Partition([all_alternatives], full_set=all_alternatives) partition_uniform_asian = Partition([asian], full_set=asian) partition_uniform_asian_or_downtown = Partition( [asian_or_downtown], full_set=asian_or_downtown ) .. GENERATED FROM PYTHON SOURCE LINES 76-83 .. code-block:: Python partitions = { 'uniform': partition_uniform, 'asian': partition_asian, 'downtown': partition_downtown, 'uniform_asian_or_downtown': partition_uniform_asian_or_downtown, 'uniform_asian': partition_uniform_asian, } .. _sphx_glr_download_auto_examples_sampling_alternatives.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: alternatives.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: alternatives.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: alternatives.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_