Naming matrix columns

I have a model like this.

with pymc3.Model() as model:
    beta = pymc3.Normal('beta', mu=0, sd=2, shape=x.shape[1])
    mu =  pymc3.math.dot(x, beta)
    theta = 1 / (1 + pymc3.math.exp(-mu))
    cutpoints_prior = [b+.5 for b in sorted(set(y))[:-1]]
    cutpoints = pymc3.Normal("cutpoints", mu=cutpoints_prior, sd=np.array([0.1 for _ in cutpoints_prior]), shape=len(set(y))-1,
                          transform=pymc3.distributions.transforms.ordered)

    y_ = pymc3.OrderedLogistic("y", cutpoints=cutpoints, eta=theta, observed=y)
  
    
    trace = pymc3.sample(5000,chains=4)

All the summary results use parameter names like “beta 0”, “beta 1” “beta 2”, “cutpoints 0”, “cutpoints 1”, etc. I would like to pass my own names to appear in the summary and plots. How can I do that?

You can use pandas simply to rename what you need

You can export it to arviz and pass in coords and dims:

import arviz

tr = arviz.from_pymc3(
    trace=trace,
    coords={
        'some_good_name_for_first_dimension_of_beta': pd.Index(['names_for_items']),
        'some_good_name_for_first_dimension_of_cutpoints': pd.Index(['foo']),
    },
    dims={
        'beta': ('some_good_name_for_first_dimension_of_beta',),
        'cutpoints': ('some_good_name_for_first_dimension_of_cutpoints',),
    }
)

tr.posterior is an xarray dataset, with all the right names. It works a lot like a multidimensional version of pandas. You can also convert it to pandas in this case if that is what you like: tr.posterior.beta.to_pandas() or tr.posterior.beta.to_dataframe().

1 Like