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.67k stars 7.86k forks source link

Fix array value #358

Open takatex opened 6 years ago

takatex commented 6 years ago

Hello,

first, many thanks for your good documents.

In 'Ch2_MorePyMC_PyMC3.ipynb', I modified a part of the code from

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

to

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

lambda_1.random() return "array(value)" and the shape is (), so lambda_1.random()[0] generate an error("too many indices for array") in original code. So, I modified the code to change the value type from "numpy.ndarray" to "numpy.float64" by using flat[0].


postscript

I modified it again

samples = lambda_1.random(size=20000)
domZippilli commented 6 years ago

+1. I was about to submit a PR for the same issue. I fixed it slightly differently:

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

Can also be:

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

What I don't quite follow is why the usual Python index selection syntax doesn't work. This might only be an issue with certain platforms.