Probabilistic-Programming-and-Bayesian-Methods-for-Hackers icon indicating copy to clipboard operation
Probabilistic-Programming-and-Bayesian-Methods-for-Hackers copied to clipboard

In Ch2_MorePyMC_PyMC3 Ln[11], running "lambda_1.random()[0]", got "IndexError: too many indices for array"

Open gaoleihlj opened this issue 7 years ago • 2 comments

The codes in Ln[11] are:

%matplotlib inline from IPython.core.pylabtools import figsize import matplotlib.pyplot as plt import scipy.stats as stats figsize(12.5, 4)

samples = [lambda_1.random()[0] for i in range(20000)] plt.hist(samples, bins=70, normed=True, histtype="stepfilled") plt.title("Prior distribution for $\lambda_1$") plt.xlim(0, 8);

And the program stopped at:

samples = [lambda_1.random()[0] for i in range(20000)]

with error:

IndexError: too many indices for array

So I tried to change it as:

samples= [lambda_1.random() for i in range(20000)]

and it worked.

gaoleihlj avatar Dec 13 '17 07:12 gaoleihlj

I removed the [0] indexing and it worked. It looks like lambda_1.random() is a scalar and not an array.

samples = [lambda_1.random() for i in range(20000)]

WillKoehrsen avatar Feb 03 '18 01:02 WillKoehrsen

as of numpy 1.18.5 / pymc 2.3.8, you may need something like this:

samples = np.array([lambda_1.random() for i in range(20000)]).reshape(20000,1)

JamesCHub avatar Jul 12 '21 18:07 JamesCHub