arviz.InferenceData.unstack#

InferenceData.unstack(dim=None, groups=None, filter_groups=None, inplace=False)[source]#

Perform an xarray unstacking on all groups.

Unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions. Loops groups to perform Dataset.unstack(key=value). 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.unstack()

Parameters
dim: Hashable or iterable of Hashable, optional

Dimension(s) over which to unstack. By default unstacks all MultiIndexes.

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.

Returns
InferenceData

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

See also

xarray.Dataset.unstack

Unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions.

stack

Perform an xarray stacking on all groups of InferenceData object.

Examples

Use unstack to unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions. We first stack two dimensions c1 and c99 to z:

In [1]: 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.stack(z=["c1", "c99"], inplace=True)
   ...: idata
   ...: 
Out[1]: 
Inference data with groups:
	> posterior
	> posterior_predictive

In order to unstack the dimension z, we use:

In [2]: idata.unstack(inplace=True)
   ...: idata
   ...: 
Out[2]: 
Inference data with groups:
	> posterior
	> posterior_predictive