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>
Dimensions:  (sample: 2000, school: 8)
Coordinates:
  * school   (school) object 'Choate' 'Deerfield' ... "St. Paul's" 'Mt. Hermon'
  * sample   (sample) object MultiIndex
  * chain    (sample) int64 0 0 0 0 0 0 0 0 0 0 0 0 ... 3 3 3 3 3 3 3 3 3 3 3 3
  * draw     (sample) int64 0 1 2 3 4 5 6 7 ... 492 493 494 495 496 497 498 499
Data variables:
    mu       (sample) float64 7.872 3.385 9.1 7.304 ... 1.859 1.767 3.486 3.404
    theta    (school, sample) float64 12.32 11.29 5.709 ... -2.623 8.452 1.295
    tau      (sample) float64 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)>
array([[ 8.82885706e+00,  3.27531513e+00,  7.71446622e+00,
         7.21561108e+00,  9.50669547e+00,  1.67363399e+00,
         6.19166018e+00,  1.00837317e+01, -2.29521925e+00,
         6.99213381e+00,  1.95265460e+01,  3.75091313e+00,
         5.36521047e+00,  1.29864107e-01, -3.97651749e+00,
         4.40498925e+00,  2.22945911e+00,  1.63025899e+00,
         4.61346793e+00,  1.07845202e+01,  1.41049176e+01,
         9.72051672e+00,  1.52660632e+00,  6.32589200e+00,
         1.07041408e+01,  4.50599475e+00,  1.45832141e-01,
         5.79889622e+00,  5.58890873e+00,  1.06024945e+00,
         5.66827805e+00,  8.96675347e+00,  8.56597384e+00,
        -1.41896381e+01,  9.44361810e+00,  6.07652331e+00,
         6.10026872e+00,  5.67765139e+00, -3.17071720e-01,
         2.86064613e+00,  3.42527056e+00, -3.96651606e-01,
         9.71751869e+00,  1.53984813e+01,  2.05616287e+01,
         9.61476686e+00,  5.69355298e+00,  3.37453765e+00,
         5.80852374e-01,  6.18563316e+00,  2.27413636e+00,
         9.54687800e+00, -8.12128199e+00,  4.67878394e+00,
         1.02752773e+01,  1.12856232e+01,  8.47108056e+00,
         3.54038165e+00,  2.28772366e+00,  1.19291930e+01,
...
         5.64273170e+00, -2.46646286e+01,  1.15104032e+01,
         9.65961571e+00,  2.36656658e+00,  1.53044298e+00,
         5.21696464e-01,  3.14824203e+00,  1.11410416e+01,
         6.35474731e+00, -9.26931010e-01,  7.05062428e-01,
         1.07513537e+01,  6.17672421e+00,  1.10079187e+01,
         5.79259069e+00, -2.39197951e-01,  3.73976292e+00,
         1.05200857e+01,  3.35132225e+00,  6.50970366e+00,
         1.17078108e+01,  2.48759308e+00, -1.34509177e+00,
        -9.83216952e-01,  5.38529348e+00, -1.62716688e+00,
         7.79568661e+00, -1.46629935e+00, -1.00244989e+00,
         6.55530380e+00,  7.69227049e+00,  5.08451764e+00,
         1.40122789e+01,  1.32765881e+01,  2.77586579e+00,
         1.22603714e+01, -2.63846828e+00,  4.18385037e+00,
         6.74760929e+00,  4.11176477e+00,  5.04095986e+00,
         4.68006899e+00,  3.04080487e+00,  1.12167838e+01,
         6.19330398e+00,  3.79041671e+00,  9.24872843e+00,
         9.65910877e+00, -4.20468849e-01, -3.58086781e+00,
         1.34963928e+00,  9.32159816e+00,  1.34236713e+00,
         1.16208554e+01, -1.42720848e+00,  1.98634001e+01,
        -6.95003768e-02]])
Coordinates:
  * school   (school) object 'Choate' 'Deerfield' ... "St. Paul's" 'Mt. Hermon'
  * sample   (sample) object MultiIndex
  * chain    (sample) int64 2 1 2 3 2 1 3 0 3 0 1 0 ... 0 3 0 2 1 0 1 1 1 0 3 3
  * draw     (sample) int64 201 215 185 169 227 427 ... 60 359 197 483 153 311

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

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