Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get indices from numpy index result containing multiple elements?

a=np.random.dirichlet(np.ones(3),size=1)

I want to use three numbers, where they sum up to 1. However, I noticed that a[0] will be:

array([0.24414272, 0.01769199, 0.7381653 ])

an index which already contains three elements.

Is there any way to spilt them into three indices?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The default behavior if you don’t pass size is to return a single dimensional array with the specified elements, per the docstring on the function:

size : int or tuple of ints, optional

Output shape. If the give shape is, e.g., (m, n), then m * n * k [where k is size of input and sample sequences] samples are drawn. Default is None, in which case a vector of length k is returned.

By passing size=1, you explicitly tell it to make a multidimensional array of size samples (so, 1 sample, making the outer dimension 1), where not passing size (or passing size=None) would still make just one set of samples, as a single 1D array.

Short version: If you just drop the ,size=1 from your call, you’ll get what you want.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading