'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 that already contains three elements.

Is there any way to split them into three indices?



Solution 1:[1]

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.

Solution 2:[2]

If that's the only thing you want, then this should work:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ShadowRanger
Solution 2 Jhanzaib Humayun