arviz_base.explode_dataset_dims

arviz_base.explode_dataset_dims#

arviz_base.explode_dataset_dims(ds, dim, labeller=None)[source]#

Explode dims of a dataset so each slice along them becomes its own variable.

Parameters:
dsxarray.Dataset
dimhashable or sequence of hashable

Dimension or dimensions along which slices to be stored as independent variables should be defined.

labellerlabeller, optional

Instance of a labeller class used to label the slices generated when exploding along dim. The method make_label_flat is used.

Returns:
xarray.Dataset

The dataset with all variables that have dim exploded into the respective slices as new variables.

Examples

In some cases, instead of theta as a (..., school) shape variable we’ll want independent variables for each slice:

from arviz_base import load_arviz_data, explode_dataset_dims
import xarray as xr

idata = load_arviz_data("centered_eight")
explode_dataset_dims(idata.posterior.dataset, "school")
<xarray.Dataset> Size: 164kB
Dimensions:                  (chain: 4, draw: 500)
Coordinates:
  * chain                    (chain) int64 32B 0 1 2 3
  * draw                     (draw) int64 4kB 0 1 2 3 4 ... 495 496 497 498 499
Data variables:
    mu                       (chain, draw) float64 16kB 1.716 1.903 ... 10.24
    theta[Choate]            (chain, draw) float64 16kB 2.317 0.8892 ... 10.46
    theta[Deerfield]         (chain, draw) float64 16kB 1.45 0.7429 ... 13.71
    theta[Phillips Andover]  (chain, draw) float64 16kB 2.086 3.126 ... 10.26
    theta[Phillips Exeter]   (chain, draw) float64 16kB 2.227 2.78 ... 15.18
    theta[Hotchkiss]         (chain, draw) float64 16kB 3.072 2.835 ... 10.92
    theta[Lawrenceville]     (chain, draw) float64 16kB 2.713 1.559 ... 15.07
    theta[St. Paul's]        (chain, draw) float64 16kB 3.084 2.488 ... 14.92
    theta[Mt. Hermon]        (chain, draw) float64 16kB 1.46 1.984 ... 14.02
    tau                      (chain, draw) float64 16kB 0.8775 0.8027 ... 3.052