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([[-1.78311617e+00, -1.13886614e+00,  5.19089416e+00,
        -6.24775331e-01,  8.19749048e+00,  1.73526948e+01,
         1.67363399e+00,  1.57870481e+00, -1.93842972e+00,
         1.74297394e+01,  7.44738170e+00,  5.69355298e+00,
         4.93254022e+00,  8.07073012e+00,  1.21768644e+01,
         3.25808233e+00,  5.03099276e+00,  6.76183244e+00,
         5.58890873e+00, -2.38202838e+00,  1.06024945e+00,
        -6.40270719e+00,  2.11944623e+01, -5.89213263e+00,
         3.25808233e+00,  3.25808233e+00,  1.00503087e+00,
         4.73699065e+00,  4.71360212e+00,  1.24760716e+01,
         5.70862545e+00,  8.89077916e+00,  1.45347601e+01,
         3.27119891e-01,  4.21145362e+00,  9.76585447e+00,
         1.31810776e+01,  3.36315998e+00,  9.15852024e+00,
        -2.59916527e-01,  1.20049894e+01,  1.00837317e+01,
         8.87951466e+00,  2.97984261e+00,  4.40547375e+00,
         4.95966219e-01,  1.38290000e+01, -3.00071136e-01,
         8.64506211e-01,  8.96712480e+00,  2.31074395e+00,
         4.27281294e+00, -1.44923689e+00,  8.43825298e+00,
         6.72791671e+00,  5.86562232e+00,  1.48492425e+01,
         2.97785044e+00,  3.52332416e+00, -2.44684071e-01,
...
         1.95594803e+00, -5.52611408e+00,  8.05639749e+00,
        -1.61045716e-01,  9.29492710e+00, -3.23634279e+00,
         6.06084422e+00,  8.12276457e+00,  5.08197088e+00,
         4.63566609e+00, -5.44638384e+00,  6.70567466e+00,
         9.46142894e+00,  4.54580884e+00,  1.33246268e+01,
         6.23728051e+00,  7.59795845e+00,  2.24231202e+00,
         1.07928340e+01,  1.02558035e+01,  2.44976560e+00,
         6.85181920e+00,  6.09071623e+00,  3.23645339e+00,
         6.61190575e-02, -1.11811594e+01, -5.93250093e-01,
        -3.66902984e+00,  1.74868934e+00, -3.26253213e+00,
         5.45885287e+00,  7.70316527e-01,  3.30738673e+00,
         1.54844421e+00,  9.82982661e+00,  1.20210045e+00,
         1.23008603e+01,  2.22113452e+00,  3.15202276e+00,
         3.93829921e+00,  6.31606936e-02,  4.59893074e+00,
         2.30305350e+00, -9.98873440e+00,  3.71764031e+00,
        -3.38299854e+00,  1.32603049e+01, -2.39197951e-01,
         5.80128351e-01,  1.59962412e+00,  3.83504623e+00,
         6.91642449e+00,  8.93847841e+00,  1.11477976e+01,
         4.33335094e+00,  4.68740748e+00,  3.74670827e+00,
         9.40393402e+00]])
Coordinates:
  * school   (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon'
  * sample   (sample) object 800B MultiIndex
  * chain    (sample) int64 800B 2 3 3 2 1 2 1 3 1 2 0 ... 3 2 0 1 2 0 3 1 0 2 2
  * draw     (sample) int64 800B 154 454 238 334 361 320 ... 416 496 496 346 36

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