CamDavidsonPilon / Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.55k stars 7.85k forks source link

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

Open gaoleihlj opened 6 years ago

gaoleihlj commented 6 years ago

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.

WillKoehrsen commented 6 years ago

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)]

JamesCHub commented 3 years ago

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)