ValueError: x and y can be no greater than 2D, but have shapes (144,) and (144, 500, 144)

Hello. I’m trying to recreate the following tutorial Air passengers - Prophet-like model — PyMC3 3.11.5 documentation and I think Arviz has updated since.

I’m taking the trace object and running the following:

df.plot.scatter(x="Month", y="#Passengers", color="k", ax=ax[0])
ax[0].set_title("Posterior predictive")
posterior_trend = linear_trace.posterior["trend"].stack(sample=("draw", "chain")).T
ax[1].plot(df["Month"], _sample(posterior_trend, 100).T * y_max, color="blue", alpha=0.01)
df.plot.scatter(x="Month", y="#Passengers", color="k", ax=ax[1])
ax[1].set_title("Posterior trend lines");

I’m hoping to recreate the following graph:

However, I’m getting the following error:

ValueError: x and y can be no greater than 2D, but have shapes (144,) and (144, 500, 144)

Could someone please help me out? The only change in the code from the tutorial is the amount of samples, changed from 100 to 144.

A couple of questions. Exactly what dd you change and where? What line is generating the error message you pasted?

I changed:

ax[1].plot(df["Month"], _sample(posterior_trend, 100).T * y_max, color="blue", alpha=0.01)

to

ax[1].plot(df["Month"], _sample(posterior_trend, 144).T * y_max, color="blue", alpha=0.01)
df.plot.scatter(x="Month", y="#Passengers", color="k", ax=ax[1])

When I change it back to 100, I get an error of a size mismatch. Changing it to 144 matches my input data size.

Specifically the amount of samples taken. In the tutorial, the _sample function is

def _sample(array, n_samples):
    """Little utility function, sample n_samples with replacement"""
    idx = np.random.choice(np.arange(len(array)), n_samples, replace=True)
    return array[idx]

Strange. Either one works fine for me. What is the shape of your posterior_trend?

Hi, I have recently re-run this notebook in PyMC v4 on this PR.

It is not merged yet, but you can check the detailed notebook here to see how to run it with pymc 4.0.0b6 and arviz 0.12.0 (along the _sample function).

Besides, In arviz>=0.12.0, this _sample function is no longer needed, and it can be replaced with arviz.extract_dataset. I am going to spend sometime this week to update this and other minor things, and hopefully it will be merged soon.

3 Likes