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 7.872 3.385 ... 3.404
    theta[Choate]            (chain, draw) float64 16kB 12.32 11.29 ... 0.193
    theta[Deerfield]         (chain, draw) float64 16kB 9.905 9.129 ... 6.498
    theta[Phillips Andover]  (chain, draw) float64 16kB 14.95 3.139 ... -0.8944
    theta[Phillips Exeter]   (chain, draw) float64 16kB 11.01 9.433 ... 6.849
    theta[Hotchkiss]         (chain, draw) float64 16kB 5.58 7.812 ... 1.86
    theta[Lawrenceville]     (chain, draw) float64 16kB 16.9 2.393 ... 7.936
    theta[St. Paul's]        (chain, draw) float64 16kB 13.2 10.06 ... 6.762
    theta[Mt. Hermon]        (chain, draw) float64 16kB 15.06 6.177 ... 1.295
    tau                      (chain, draw) float64 16kB 4.726 3.909 ... 4.461