Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "env: MKL_THREADING_LAYER=GNU\n"
- ]
- }
- ],
- "source": [
- "%env MKL_THREADING_LAYER GNU"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "scrolled": false
- },
- "outputs": [],
- "source": [
- "import numpy as np\n",
- "import arviz as az\n",
- "from arviz.tests.helpers import load_cached_models"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "models = load_cached_models(500, 2)\n",
- "stan_model, stan_fit = models['pystan']\n",
- "\n",
- "# this is the OrderedDict\n",
- "sample_dict = stan_fit.extract(stan_fit.model_pars, permuted=False)\n",
- "\n",
- "# this is an example of the API being used carefully\n",
- "stan_xarray = az.convert_to_xarray(stan_fit, {'school': np.arange(8)}, dims={'theta': ['school'], 'theta_tilde': ['school']})"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "dict_xarray = az.convert_to_xarray(sample_dict, {'chains': 2, 'school': np.arange(8)}, dims={'theta': ['school'], 'theta_tilde': ['school']})"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<xarray.Dataset>\n",
- "Dimensions: (chain: 2, draw: 500, school: 8)\n",
- "Coordinates:\n",
- " * school (school) int64 0 1 2 3 4 5 6 7\n",
- " * draw (draw) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...\n",
- " * chain (chain) int64 0 1\n",
- "Data variables:\n",
- " mu (chain, draw) float64 7.671 7.671 8.908 8.525 8.152 5.315 ...\n",
- " tau (chain, draw) float64 6.842 6.842 0.8769 1.08 0.1644 0.5318 ...\n",
- " theta_tilde (chain, draw, school) float64 0.5065 0.9721 0.7126 1.597 ...\n",
- " theta (chain, draw, school) float64 11.14 14.32 12.55 18.6 3.667 ..."
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "stan_xarray"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<xarray.Dataset>\n",
- "Dimensions: (chain: 2, draw: 500, school: 8)\n",
- "Coordinates:\n",
- " * school (school) int64 0 1 2 3 4 5 6 7\n",
- " * draw (draw) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...\n",
- " * chain (chain) int64 0 1\n",
- "Data variables:\n",
- " mu (chain, draw) float64 7.671 7.671 8.908 8.525 8.152 5.315 ...\n",
- " tau (chain, draw) float64 6.842 6.842 0.8769 1.08 0.1644 0.5318 ...\n",
- " theta_tilde (chain, draw, school) float64 0.5065 0.9721 0.7126 1.597 ...\n",
- " theta (chain, draw, school) float64 11.14 14.32 12.55 18.6 3.667 ..."
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dict_xarray"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: iteration over an xarray.Dataset will change in xarray v0.11 to only include data variables, not coordinates. Iterate over the Dataset.variables property instead to preserve existing behavior in a forwards compatible manner.\n",
- " \"\"\"Entry point for launching an IPython kernel.\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "all((stan_xarray == dict_xarray).all())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment