arviz.InferenceData.stack

InferenceData.stack(dimensions=None, groups=None, filter_groups=None, inplace=False, **kwargs)[source]

Perform an xarray stacking on all groups.

Stack any number of existing dimensions into a single new dimension. Loops groups to perform Dataset.stack(key=value) for every kwarg if value is a dimension of the dataset. The selection is performed on all relevant groups (like posterior, prior, sample stats) while non relevant groups like observed data are omitted. See xarray.Dataset.stack()

Parameters
dimensions: dict

Names of new dimensions, and the existing dimensions that they replace.

groups: str or list of str, optional

Groups where the selection is to be applied. Can either be group names or metagroup names.

filter_groups: {None, “like”, “regex”}, optional, default=None

If None (default), interpret groups as the real group or metagroup names. If “like”, interpret groups as substrings of the real group or metagroup names. If “regex”, interpret groups as regular expressions on the real group or metagroup names. A la pandas.filter.

inplace: bool, optional

If True, modify the InferenceData object inplace, otherwise, return the modified copy.

**kwargs: mapping

It must be accepted by xarray.Dataset.stack().

Returns
InferenceData

A new InferenceData object by default. When inplace==True perform selection in-place and return None

See also

xarray.Dataset.stack

Stack any number of existing dimensions into a single new dimension.

unstack

Perform an xarray unstacking on all groups of InferenceData object.

Examples

Use stack to stack any number of existing dimensions into a single new dimension. We first check the original object:

In [1]: import arviz as az
   ...: idata = az.load_arviz_data("rugby")
   ...: idata
   ...: 
Out[1]: 
Inference data with groups:
	> posterior
	> posterior_predictive
	> sample_stats
	> prior
	> observed_data

In order to stack two dimensions chain and draw to sample, we can use:

In [2]: idata.stack(sample=["chain", "draw"], inplace=True)
   ...: idata
   ...: 
Out[2]: 
Inference data with groups:
	> posterior
	> posterior_predictive
	> sample_stats
	> prior
	> observed_data

We can also take the example of custom InferenceData object and perform stacking. We first check the original object:

In [3]: import arviz as az
   ...: datadict = {
   ...:     "a": np.random.randn(100),
   ...:     "b": np.random.randn(1, 100, 10),
   ...:     "c": np.random.randn(1, 100, 3, 4),
   ...: }
   ...: coords = {
   ...:     "c1": np.arange(3),
   ...:     "c99": np.arange(4),
   ...:     "b1": np.arange(10),
   ...: }
   ...: dims = {"c": ["c1", "c99"], "b": ["b1"]}
   ...: idata = az.from_dict(
   ...:     posterior=datadict, posterior_predictive=datadict, coords=coords, dims=dims
   ...: )
   ...: idata.posterior
   ...: 
Out[3]: 
<xarray.Dataset>
Dimensions:  (chain: 1, draw: 100, b1: 10, c1: 3, c99: 4)
Coordinates:
  * chain    (chain) int64 0
  * draw     (draw) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 99
  * b1       (b1) int64 0 1 2 3 4 5 6 7 8 9
  * c1       (c1) int64 0 1 2
  * c99      (c99) int64 0 1 2 3
Data variables:
    a        (chain, draw) float64 0.07773 -0.7267 -0.515 ... -0.01829 -1.327
    b        (chain, draw, b1) float64 -1.152 -0.6946 1.387 ... -1.107 -1.943
    c        (chain, draw, c1, c99) float64 0.3029 -0.4903 ... -0.07863 -0.9361
Attributes:
    created_at:     2021-11-15T19:17:08.509779
    arviz_version:  0.11.4

In order to stack two dimensions c1 and c99 to z, we can use:

In [4]: idata.stack(z=["c1", "c99"], inplace=True)
   ...: idata.posterior
   ...: 
Out[4]: 
<xarray.Dataset>
Dimensions:  (chain: 1, draw: 100, b1: 10, z: 12)
Coordinates:
  * chain    (chain) int64 0
  * draw     (draw) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 99
  * b1       (b1) int64 0 1 2 3 4 5 6 7 8 9
  * z        (z) MultiIndex
  - c1       (z) int64 0 0 0 0 1 1 1 1 2 2 2 2
  - c99      (z) int64 0 1 2 3 0 1 2 3 0 1 2 3
Data variables:
    a        (chain, draw) float64 0.07773 -0.7267 -0.515 ... -0.01829 -1.327
    b        (chain, draw, b1) float64 -1.152 -0.6946 1.387 ... -1.107 -1.943
    c        (chain, draw, z) float64 0.3029 -0.4903 -1.633 ... -0.07863 -0.9361
Attributes:
    created_at:     2021-11-15T19:17:08.509779
    arviz_version:  0.11.4