Home > front end >  How to get indices from numpy index result containing multiple elements?
How to get indices from numpy index result containing multiple elements?

Time:04-14

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?

CodePudding user response:

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.

CodePudding user response:

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

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