arviz_base.extract#
- arviz_base.extract(data, group='posterior', sample_dims=None, *, combined=True, var_names=None, filter_vars=None, num_samples=None, weights=None, resampling_method=None, keep_dataset=False, random_seed=None)[source]#
Extract a group or group subset from a DataTree.
- Parameters:
- idataDataTree-like
DataTree from which to extract the data.
- group
str, optional Which group to extract data from.
- sample_dimssequence of hashable, optional
List of dimensions that should be considered sampling dimensions. Random subsets and potential stacking if
combine=Truehappen over these dimensions only. Defaults torcParams["data.sample_dims"].- combinedbool, optional
Combine sample_dims dimensions into
sample. Won’t work if a dimension namedsamplealready exists. It is irrelevant and ignored when sample_dims is a single dimension.- var_names
strorlistofstr, 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_samples
int, optional Extract only a subset of the samples. Only valid if
combined=Trueor sample_dims represents a single dimension.- weightsarray_like, optional
Extract a weighted subset of the samples. Only valid if num_samples is not
None.- resampling_method
str, optional Method to use for resampling. Default is “multinomial”. Options are “multinomial” and “stratified”. For stratified resampling, weights must be provided. Default is “stratified” if weights are provided, “multinomial” otherwise.
- keep_datasetbool, optional
If true, always return a DataSet. If false (default) return a DataArray when there is a single variable.
- random_seed
int,numpy.Generator, optional Random number generator or seed. Only used if
weightsis notNoneor ifnum_samplesis notNone.
- Returns:
Examples
The default behaviour is to return the posterior group after stacking the chain and draw dimensions.
import arviz_base as az idata = az.load_arviz_data("centered_eight") az.extract(idata)
<xarray.Dataset> Size: 209kB Dimensions: (sample: 2000, school: 8) Coordinates: * sample (sample) object 16kB MultiIndex * school (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon' * 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: 1000You 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.08202185e+01, 4.05364581e+00, -8.82173225e-01, 1.34913572e+01, 8.83962614e+00, 2.99761342e+00, 1.09098667e+01, 3.09387429e+01, 8.77064233e+00, 9.46772141e+00, -2.97112361e+00, 2.00736222e+01, 3.25808233e+00, 8.68112669e+00, 1.00837317e+01, 9.36529694e+00, 1.45832141e-01, 1.46669201e+01, 2.69719116e+00, 3.05350169e+00, 1.77358900e+01, 7.52662516e+00, 4.87593796e+00, 7.52359557e+00, 4.75398239e+00, -6.41664260e+00, 2.17756186e+00, 7.02777773e+00, 5.12259513e+00, 1.04489792e+01, 4.59050634e+00, 1.57870481e+00, 1.59624326e+01, 5.51925873e+00, 4.45437593e+00, 2.47771747e+01, 1.66144158e+01, 9.83765504e+00, 4.18275050e+00, 5.19869161e+00, 5.77069226e+00, 8.64506211e-01, -1.64413909e+00, 1.29420031e+00, 2.99815135e+00, 2.21774971e+01, 4.73699065e+00, 2.72886650e+00, 7.25006163e+00, 9.25162767e+00, 3.25808233e+00, 4.46941839e+00, 6.13458430e+00, 1.50467592e+01, -5.89347515e-02, 9.58212542e+00, 9.80188050e+00, 2.31176997e-01, 4.13129223e+00, 8.02796674e+00, ... 3.74935742e-02, 8.57206490e+00, -1.54271377e+00, 9.05251132e+00, -6.77077630e+00, 1.50243882e+00, 5.19428493e+00, 9.27165510e+00, 3.35132225e+00, 1.49927153e+00, 4.84640657e+00, -7.08801441e+00, 4.12192432e+00, 4.95648908e+00, 9.24872843e+00, 1.54844421e+00, -1.35336102e+01, 2.41581002e-01, 5.62488877e+00, 9.37139552e+00, 1.12827635e+01, -1.61990713e+00, 8.20203440e+00, 9.68439852e+00, -8.11232046e+00, 8.58671448e+00, 5.19428493e+00, 5.92703798e+00, 5.13785481e-01, 6.95403561e+00, 3.11912417e+00, 5.58546860e+00, 8.66829738e+00, 9.06793975e+00, 6.90899655e+00, 5.34001408e+00, 5.45885287e+00, 9.86833959e+00, 1.19446784e+01, 1.16254062e+01, 2.44811532e+01, -4.95894413e+00, 6.85181920e+00, 5.62894307e+00, 6.99697482e+00, 8.75646362e+00, 1.08956780e+00, 9.71423948e+00, 3.23104629e+00, 1.95594803e+00, -2.67215679e+00, -7.20568490e+00, 3.35132225e+00, 5.04095986e+00, 9.16526210e+00, 2.95048204e-01, 1.66612687e+01, 9.65961571e+00]]) Coordinates: * school (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon' * sample (sample) object 800B MultiIndex * chain (sample) int64 800B 2 0 2 3 3 3 0 3 2 1 1 ... 2 1 0 0 1 1 1 0 1 2 2 * draw (sample) int64 800B 91 242 100 318 376 161 ... 105 13 188 251 167To 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 1.941 3.388 4.208 ... 0.06893 2.145 theta (chain, draw, school) float64 32kB 4.866 4.59 ... -2.031 6.045 mu (chain, draw) float64 4kB 3.903 3.915 -1.751 ... 0.7908 2.869 Attributes: arviz_version: 0.13.0.dev0 created_at: 2022-10-13T14:37:26.602116 inference_library: pymc inference_library_version: 4.2.2