arviz.InferenceData.sel#
- InferenceData.sel(groups: Optional[Union[str, List[str]]] = None, filter_groups: Optional[Literal['like', 'regex']] = None, inplace: bool = False, chain_prior: Optional[bool] = None, **kwargs: Any) Optional[arviz.data.inference_data.InferenceDataT] [source]#
Perform an xarray selection on all groups.
Loops groups to perform Dataset.sel(key=item) for every kwarg if key is a dimension of the dataset. One example could be performing a burn in cut on the InferenceData object or discarding a chain. 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.sel
- Parameters
- groups: str 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, default=None
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.
- inplace: bool, optional
If
True
, modify the InferenceData object inplace, otherwise, return the modified copy.- chain_prior: bool, optional, deprecated
If
False
, do not select prior related groups usingchain
dim. Otherwise, use selection onchain
if present. Default=False- **kwargs: mapping
It must be accepted by Dataset.sel().
- Returns
- InferenceData
A new InferenceData object by default. When inplace==True perform selection in-place and return None
See also
xarray.Dataset.sel
Returns a new dataset with each array indexed by tick labels along the specified dimension(s).
isel
Returns a new dataset with each array indexed along the specified dimension(s).
Examples
Use
sel
to discard one chain of the InferenceData object. We first check the dimensions of the original object:In [1]: import arviz as az ...: idata = az.load_arviz_data("centered_eight") ...: del idata.prior # prior group only has 1 chain currently ...: print(idata.posterior.coords) ...: print(idata.posterior_predictive.coords) ...: print(idata.observed_data.coords) ...: Coordinates: * chain (chain) int64 0 1 2 3 * 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' Coordinates: * chain (chain) int64 0 1 2 3 * 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' Coordinates: * school (school) object 'Choate' 'Deerfield' ... "St. Paul's" 'Mt. Hermon'
In order to remove the third chain:
In [2]: idata_subset = idata.sel(chain=[0, 1, 3]) ...: print(idata_subset.posterior.coords) ...: print(idata_subset.posterior_predictive.coords) ...: print(idata_subset.observed_data.coords) ...: Coordinates: * chain (chain) int64 0 1 3 * 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' Coordinates: * chain (chain) int64 0 1 3 * 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' Coordinates: * school (school) object 'Choate' 'Deerfield' ... "St. Paul's" 'Mt. Hermon'