.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_learn_structure.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_01_learn_structure.py: Learn a structure from discrete data ==================================== :class:`~causalbase.search.GES` learns the Markov equivalence class of a causal structure from discrete observations. It takes a DataFrame, an array or a :class:`~causalbase.data.DiscreteDataset`, and every distinct value of a column is one level. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: Python from causalbase import GES from causalbase.datasets import load_asia from causalbase.metrics import edge_scores, shd bundle = load_asia() frame = bundle.data.to_pandas() frame.head() .. raw:: html
lung smoke bronc xray tub asia dysp either
0 no no no no no no no no
1 no yes yes no no no yes no
2 no no no no no no no no
3 no no no no no no no no
4 no yes no no no no no no


.. GENERATED FROM PYTHON SOURCE LINES 20-23 Fitting follows scikit-learn: hyperparameters go to the constructor, and the results end in an underscore. ``cpdag_`` is the equivalence class; ``adjacency_`` is the same graph with the variable names. .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: Python model = GES().fit(frame) model.adjacency_ .. raw:: html
lung smoke bronc xray tub asia dysp either
lung 0 1 0 0 0 0 0 1
smoke 1 0 1 0 0 0 0 0
bronc 0 1 0 0 0 0 1 0
xray 0 0 0 0 0 0 0 0
tub 0 0 0 0 0 0 0 1
asia 0 0 0 0 0 0 0 0
dysp 0 0 0 0 0 0 0 0
either 0 0 0 1 0 0 1 0


.. GENERATED FROM PYTHON SOURCE LINES 28-30 A directed edge is one every DAG of the class agrees on. An undirected edge is one the data cannot orient. .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python names = model.feature_names_in_ for u, v in model.cpdag_.directed_edges(): print(f"{names[u]} -> {names[v]}") for u, v in model.cpdag_.undirected_edges(): print(f"{names[u]} -- {names[v]}") .. rst-class:: sphx-glr-script-out .. code-block:: none lung -> either bronc -> dysp tub -> either either -> xray either -> dysp lung -- smoke smoke -- bronc .. GENERATED FROM PYTHON SOURCE LINES 38-39 ``dag_`` is one member of the class, which is what the metrics compare. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python scores = edge_scores(bundle.truth, model.dag_) print(f"SHD {shd(bundle.truth, model.dag_)}") print(f"Skeleton precision {scores.precision_skel:.2f}, recall {scores.recall_skel:.2f}") .. rst-class:: sphx-glr-script-out .. code-block:: none SHD 2 Skeleton precision 1.00, recall 0.88 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.435 seconds) .. _sphx_glr_download_auto_examples_01_learn_structure.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01_learn_structure.ipynb <01_learn_structure.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01_learn_structure.py <01_learn_structure.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01_learn_structure.zip <01_learn_structure.zip>`