arviz.extract#

arviz.extract(data, group='posterior', combined=True, var_names=None, filter_vars=None, num_samples=None, keep_dataset=False, rng=None)[source]#

Extract an InferenceData group or subset of it.

Parameters:
idataInferenceData or InferenceData_like

InferenceData from which to extract the data.

groupstr, optional

Which InferenceData data group to extract data from.

combinedbool, optional

Combine chain and draw dimensions into sample. Won’t work if a dimension named sample already exists.

var_namesstr or list of str, optional

Variables to be extracted. Prefix the variables by ~ when you want to exclude them.

filter_vars: {None, “like”, “regex”}, optional

If None (default), interpret var_names as the real variables names. If “like”, interpret var_names as substrings of the real variables names. If “regex”, interpret var_names as regular expressions on the real variables names. A la pandas.filter. Like with plotting, sometimes it’s easier to subset saying what to exclude instead of what to include

num_samplesint, optional

Extract only a subset of the samples. Only valid if combined=True

keep_datasetbool, optional

If true, always return a DataSet. If false (default) return a DataArray when there is a single variable.

rngbool, int, numpy.Generator, optional

Shuffle the samples, only valid if combined=True. By default, samples are shuffled if num_samples is not None, and are left in the same order otherwise. This ensures that subsetting the samples doesn’t return only samples from a single chain and consecutive draws.

Returns:
xarray.DataArray or xarray.Dataset

Examples

The default behaviour is to return the posterior group after stacking the chain and draw dimensions.

import arviz as az
idata = az.load_arviz_data("centered_eight")
az.extract(idata)
<xarray.Dataset> Size: 209kB
Dimensions:  (sample: 2000, school: 8)
Coordinates:
  * school   (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon'
  * sample   (sample) object 16kB MultiIndex
  * chain    (sample) int64 16kB 0 0 0 0 0 0 0 0 0 0 0 ... 3 3 3 3 3 3 3 3 3 3 3
  * draw     (sample) int64 16kB 0 1 2 3 4 5 6 7 ... 493 494 495 496 497 498 499
Data variables:
    mu       (sample) float64 16kB 7.872 3.385 9.1 7.304 ... 1.767 3.486 3.404
    theta    (school, sample) float64 128kB 12.32 11.29 5.709 ... 8.452 1.295
    tau      (sample) float64 16kB 4.726 3.909 4.844 1.857 ... 2.741 2.932 4.461
Attributes:
    created_at:                 2022-10-13T14:37:37.315398
    arviz_version:              0.13.0.dev0
    inference_library:          pymc
    inference_library_version:  4.2.2
    sampling_time:              7.480114936828613
    tuning_steps:               1000

You can also indicate a subset to be returned, but in variables and in samples:

az.extract(idata, var_names="theta", num_samples=100)
<xarray.DataArray 'theta' (school: 8, sample: 100)> Size: 6kB
array([[ 9.67286631e+00,  7.56338176e+00,  3.04243417e+00,
         2.95204754e+00,  3.25808233e+00,  4.70021331e-01,
         2.47851682e-01,  1.22883075e+01,  8.58978394e+00,
         8.13842739e-01,  9.08385687e+00,  2.16847276e+01,
         5.42773616e+00,  2.70803837e+00,  5.82519719e+00,
         3.34359524e+00,  1.03945797e+01,  2.20100223e+00,
         1.40189781e+01,  3.25808233e+00,  5.41498435e+00,
         7.02777773e+00, -5.18724150e+00, -2.33366782e+00,
         5.82519719e+00,  4.24604215e+00,  1.23471082e+01,
        -5.10846749e+00, -1.88251307e+00,  2.00630445e+01,
         6.19166018e+00,  5.39909402e+00,  9.75288135e+00,
         1.64329887e+01,  4.11926539e+00, -1.33919123e-02,
         1.25101358e+01,  4.55295525e+00,  4.60844644e+00,
        -2.38202838e+00,  1.24155393e+01,  8.20186337e+00,
         4.05364581e+00, -3.22971769e+00,  5.82519719e+00,
         5.17287960e+00,  5.55149719e-01,  5.52002251e+00,
         7.25006163e+00,  2.93914584e+01, -3.51079417e+00,
         1.14838953e+01,  2.23945711e+00,  9.83765504e+00,
         1.10176100e+01, -2.00751135e-01,  3.25808233e+00,
         1.38641174e+01,  6.75293935e-01,  3.93190159e+00,
...
         3.93829921e+00, -4.13465536e+00,  8.79855496e+00,
         8.46030942e+00,  4.48225888e+00,  4.87831767e+00,
         5.19428493e+00,  2.03454051e+00, -3.66902984e+00,
        -8.93273953e-01,  3.23201494e+00,  8.25682722e+00,
         8.52392150e+00,  1.16208554e+01,  3.35132225e+00,
         7.20084454e+00,  6.01996324e-01, -9.14445769e-02,
         3.23104629e+00,  7.65669073e+00,  1.09491930e+00,
         1.43320123e+01, -1.24931670e+01,  7.05316330e+00,
         9.82982661e+00,  2.61621294e-01,  9.72955237e-01,
         1.00406316e+01,  6.79472198e+00,  8.82150186e+00,
         5.37556287e+00,  1.18337006e+01,  7.16213323e+00,
         1.00273509e+01,  4.28070881e+00,  3.99945479e+00,
         2.41469846e+00,  7.23421922e+00,  8.11969294e+00,
         2.35058063e+00,  1.21280710e+01,  2.09180643e+00,
         7.40326889e+00,  3.23104629e+00,  1.49019828e+00,
         4.49577177e+00,  7.02286348e+00,  7.90169447e+00,
         5.58934916e+00, -1.46629935e+00, -3.62942848e-01,
         6.45165326e+00,  5.06044968e+00,  3.36798595e+00,
         5.38601869e+00, -6.18485180e-01,  6.04676715e+00,
         3.94983056e+00]])
Coordinates:
  * school   (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon'
  * sample   (sample) object 800B MultiIndex
  * chain    (sample) int64 800B 1 1 3 0 1 1 3 2 2 0 1 ... 0 2 0 1 2 3 0 2 1 0 3
  * draw     (sample) int64 800B 305 338 400 396 438 46 ... 86 441 45 432 128

To keep the chain and draw dimensions, use combined=False.

az.extract(idata, group="prior", combined=False)
<xarray.Dataset> Size: 45kB
Dimensions:  (chain: 1, draw: 500, school: 8)
Coordinates:
  * chain    (chain) int64 8B 0
  * draw     (draw) int64 4kB 0 1 2 3 4 5 6 7 ... 493 494 495 496 497 498 499
  * school   (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon'
Data variables:
    tau      (chain, draw) float64 4kB ...
    theta    (chain, draw, school) float64 32kB ...
    mu       (chain, draw) float64 4kB ...
Attributes:
    arviz_version:              0.13.0.dev0
    created_at:                 2022-10-13T14:37:26.602116
    inference_library:          pymc
    inference_library_version:  4.2.2