<jupyter_start><jupyter_text>Introduction to xarray, InferenceData, and NetCDF for ArviZ While ArviZ supports plotting from familiar datatypes, such as dictionaries and numpy arrays, there are a couple data structures central to ArviZ that are useful to know when using the library. They are      xarray.Dataset  arviz.InferenceData  netCDF  Why more than one data structure?Bayesian Inference generates numerous datasets that represent different aspects of the model. For example in a single analysis a Bayesian practioner could end up with any of the following data.    Prior Distribution for N number of variables  Posterior Distribution for N number of variables  Prior Predictive Distribution  Posterior Predictive Distribution  Trace data for each of the above  Sample statistics for each inference run  Any other array like data source  Why not Pandas Dataframes or Numpy Arrays?Data from probabilistic programming is naturally high dimensional. To add to the complexity ArviZ must handle the data generated from multiple Bayesian Modeling libraries, such as PyMC3 and PyStan. This is an application that the *xarray* package handles quite well. The xarray package lets users manage high dimensional data with human readable dimensions and coordinates quite easily.![InferenceData Structure](InferenceDataStructure.png)  Above is a visual representation of the data structures and their relationships. Although seemingly more complex at a glance the ArviZ devs believe that the usage of *xarray*, *InferenceData*, and *NetCDF* will simplify the handling, referencing, and serialization of data generated during Bayesian analysis.    An introduction to eachTo help get familiar with each, ArviZ includes some toy datasets. To start an `az.InferenceData` sample can be loaded from disk.<jupyter_code># Load the centered eight schools model
import arviz as az
data = az.load_arviz_data('centered_eight')
data<jupyter_output><empty_output><jupyter_text>In this case the `az.InferenceData` object contains both a posterior predictive distribution and the observed data, among other datasets. Each group in InferenceData is both an attribute on `InferenceData` and itself a `xarray.Dataset` object.<jupyter_code># Get the posterior Dataset
posterior = data.posterior
posterior<jupyter_output><empty_output><jupyter_text>In our eight schools example the posterior trace consists of 3 variables, estimated over 4 chains. In addition this model is a hierachial modes where values for the variable `theta` are associated with a particular school. In xarray's terminology, data variables are the actual values generated from the MCMC draws, Dimensions are the axes on which refer to the data variables and coordinates which are pointers to specific slices or points in the `xarray.Dataset`Observed data from the Eight Schools model can be accessed through the same method.<jupyter_code># Get the observed xarray
observed_data = data.observed_data
observed_data<jupyter_output><empty_output><jupyter_text>It should be noted that the observed dataset contains only 8 data variables and doesn't have a chain and draw dimension or coordinates unlike posterior. This difference in sizes is the motivating reason behind *InferenceData*. Rather than force multiple different sized arrays into one array, or have users to manage multiple objects corresponding to different datasets, it is easier to hold references to each *xarray.Dataset* in an *InferenceData* object.  NetCDF[NetCDF](https://www.unidata.ucar.edu/software/netcdf/) is a standard for referencing array oriented files. In other words while, *xarray.Dataset*s, and by extension *InferenceData*, are convenient for accessing arrays in Python memory, *NetCDF* provides a convenient mechanism for persistence of model data on disk. In fact the NetCDF dataset was the inspiration for *InferenceData* as NetCDF4 supports the concept of groups. *InferenceData* merely wraps xarray.Dataset with the same functionality,Most users will not have to concern themselves with the *NetCDF* standard but for completeness it is good to make its usage transparent. It is also worth noting that the NetCDF4 file standard is interoperable with HDF5 which may be familiar from other contexts.Earlier in this tutorial *InferenceData* was loaded from a *NetCDF* file<jupyter_code>data = az.load_arviz_data('centered_eight')<jupyter_output><empty_output><jupyter_text>Similarly the *InferenceData* objects can be persisted tp disk in the NetCDF format<jupyter_code>data.to_netcdf("eight_schools_model.nc")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0411829134720701, 'fraction_numerical': 0.0015334063526834612, 'mean_word_length': 5.835329341317365, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '38557', 'license': 'Apache-2.0', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1095, 'n_words': 593, 'path': 'doc/notebooks/XarrayforArviZ.ipynb', 'repo_name': 'sethaxen/arviz', 'size': '4565'}