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:
dimHashable or iterable of Hashable, optional

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

groupsstr 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

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.

inplacebool, 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:

import arviz as az
import numpy as np
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
arviz.InferenceData
    • <xarray.Dataset> Size: 20kB
      Dimensions:  (chain: 1, draw: 100, b1: 10, z: 12)
      Coordinates:
        * chain    (chain) int64 8B 0
        * draw     (draw) int64 800B 0 1 2 3 4 5 6 7 8 ... 91 92 93 94 95 96 97 98 99
        * b1       (b1) int64 80B 0 1 2 3 4 5 6 7 8 9
        * z        (z) object 96B MultiIndex
        * c1       (z) int64 96B 0 0 0 0 1 1 1 1 2 2 2 2
        * c99      (z) int64 96B 0 1 2 3 0 1 2 3 0 1 2 3
      Data variables:
          a        (chain, draw) float64 800B -0.8054 1.025 -2.319 ... 0.748 -1.192
          b        (chain, draw, b1) float64 8kB -1.327 1.395 ... -1.383 -0.5102
          c        (chain, draw, z) float64 10kB -0.2137 1.952 ... 0.8256 -0.7988
      Attributes:
          created_at:     2024-06-11T13:57:56.261831+00:00
          arviz_version:  0.19.0.dev0

    • <xarray.Dataset> Size: 20kB
      Dimensions:  (chain: 1, draw: 100, b1: 10, z: 12)
      Coordinates:
        * chain    (chain) int64 8B 0
        * draw     (draw) int64 800B 0 1 2 3 4 5 6 7 8 ... 91 92 93 94 95 96 97 98 99
        * b1       (b1) int64 80B 0 1 2 3 4 5 6 7 8 9
        * z        (z) object 96B MultiIndex
        * c1       (z) int64 96B 0 0 0 0 1 1 1 1 2 2 2 2
        * c99      (z) int64 96B 0 1 2 3 0 1 2 3 0 1 2 3
      Data variables:
          a        (chain, draw) float64 800B -0.8054 1.025 -2.319 ... 0.748 -1.192
          b        (chain, draw, b1) float64 8kB -1.327 1.395 ... -1.383 -0.5102
          c        (chain, draw, z) float64 10kB -0.2137 1.952 ... 0.8256 -0.7988
      Attributes:
          created_at:     2024-06-11T13:57:56.265336+00:00
          arviz_version:  0.19.0.dev0

In order to unstack the dimension z, we use:

idata.unstack(inplace=True)
idata
arviz.InferenceData
    • <xarray.Dataset> Size: 19kB
      Dimensions:  (c1: 3, c99: 4, chain: 1, draw: 100, b1: 10)
      Coordinates:
        * c1       (c1) int64 24B 0 1 2
        * c99      (c99) int64 32B 0 1 2 3
        * chain    (chain) int64 8B 0
        * draw     (draw) int64 800B 0 1 2 3 4 5 6 7 8 ... 91 92 93 94 95 96 97 98 99
        * b1       (b1) int64 80B 0 1 2 3 4 5 6 7 8 9
      Data variables:
          a        (chain, draw) float64 800B -0.8054 1.025 -2.319 ... 0.748 -1.192
          b        (chain, draw, b1) float64 8kB -1.327 1.395 ... -1.383 -0.5102
          c        (chain, draw, c1, c99) float64 10kB -0.2137 1.952 ... -0.7988
      Attributes:
          created_at:     2024-06-11T13:57:56.261831+00:00
          arviz_version:  0.19.0.dev0

    • <xarray.Dataset> Size: 19kB
      Dimensions:  (c1: 3, c99: 4, chain: 1, draw: 100, b1: 10)
      Coordinates:
        * c1       (c1) int64 24B 0 1 2
        * c99      (c99) int64 32B 0 1 2 3
        * chain    (chain) int64 8B 0
        * draw     (draw) int64 800B 0 1 2 3 4 5 6 7 8 ... 91 92 93 94 95 96 97 98 99
        * b1       (b1) int64 80B 0 1 2 3 4 5 6 7 8 9
      Data variables:
          a        (chain, draw) float64 800B -0.8054 1.025 -2.319 ... 0.748 -1.192
          b        (chain, draw, b1) float64 8kB -1.327 1.395 ... -1.383 -0.5102
          c        (chain, draw, c1, c99) float64 10kB -0.2137 1.952 ... -0.7988
      Attributes:
          created_at:     2024-06-11T13:57:56.265336+00:00
          arviz_version:  0.19.0.dev0