how to return the minimum index of this array?

Viewed 205
ind_index = np.asarray([np.random.choice(40, 5, False) for i in range(5)])
fit = da.random.uniform(size=40, chunks=5)
parents_index = da.argmin(fit[ind_index], axis=1)

The result should be an array of shape (5,)(the minimum index of each row), instead it returns this error :

Traceback (most recent call last):
File "/home/skyolia/PycharmProjects/garbage/garbage.py", line 36, in 
<module>
parents_index = da.argmin(fit[ind_index], axis=1)
File "/usr/local/lib/python3.5/dist-packages/dask/array/core.py", line 
1383, in __getitem__
dsk, chunks = slice_array(out, self.name, self.chunks, index2)
File "/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py", 
line 158, in slice_array
dsk_out, bd_out = slice_with_newaxes(out_name, in_name, blockdims, 
index)
File "/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py", 
line 180, in slice_with_newaxes
dsk, blockdims2 = slice_wrap_lists(out_name, in_name, blockdims, 
index2)
File "/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py", 
line 247, in slice_wrap_lists
index[where_list[0]], axis=axis)
File "/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py", 
line 566, in take
plan = slicing_plan(chunks[axis], index)
File "/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py", 
line 534, in slicing_plan
if chunk > 0:
ValueError: The truth value of an array with more than one element is 
ambiguous. Use a.any() or a.all()

but the same code works well in pure numpy. Where is the problem ? many thx

1 Answers

You just call argmin on your given array fit, and optionally compute to show the result.

fit.argmin().compute()

You could also do:

import dask.array as da

da.argmin(fit).compute()

or

import numpy as np

np.argmin(fit).compute()

Remember that fit is one array of size 40. Perhaps you should describe in words what you are trying to achieve? Maybe the size should be a tuple like (40, 5) or something else like that?

Related