Skip to content

IndexError("Too many indices for array") raised when attempting to run the K-Means|| example #803

Closed
dask/dask
#7391

Description

@hristog

What happened:
Exception raised: IndexError("Too many indices for array") as a direct result of following the steps from the K-Means|| example.

What you expected to happen:
The K-Means algorithm to successfully complete the fitting stage.

Minimal Complete Verifiable Example:

Reproducible via following the steps from the K-Means|| example.

import dask_ml.datasets
import dask_ml.cluster


X, y = dask_ml.datasets.make_blobs(n_samples=10000000,
                                   chunks=1000000,
                                   random_state=0,
                                   centers=3)
X = X.persist()

km = dask_ml.cluster.KMeans(n_clusters=3, init_max_iter=2, oversampling_factor=10)
km.fit(X)

Anything else we need to know?:
Traceback:

In []: km.fit(X)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-e41873a7fbf2> in <module>
----> 1 km.fit(X)

~/anaconda3/lib/python3.8/site-packages/dask_ml/cluster/k_means.py in fit(self, X, y)
    194     def fit(self, X, y=None):
    195         X = self._check_array(X)
--> 196         labels, centroids, inertia, n_iter = k_means(
    197             X,
    198             self.n_clusters,

~/anaconda3/lib/python3.8/site-packages/dask_ml/cluster/k_means.py in k_means(X, n_clusters, init, precompute_distances, n_init, max_iter, verbose, tol, random_state, copy_x, n_jobs, algorithm, return_n_iter, oversampling_factor, init_max_iter)
    266     * n_jobs=-1
    267     """
--> 268     labels, inertia, centers, n_iter = _kmeans_single_lloyd(
    269         X,
    270         n_clusters,

~/anaconda3/lib/python3.8/site-packages/dask_ml/cluster/k_means.py in _kmeans_single_lloyd(X, n_clusters, max_iter, init, verbose, x_squared_norms, random_state, tol, precompute_distances, oversampling_factor, init_max_iter)
    569             # Require at least one per bucket, to avoid division by 0.
    570             counts = da.maximum(counts, 1)
--> 571             new_centers = new_centers / counts[:, None]
    572             (new_centers,) = compute(new_centers)
    573 

~/anaconda3/lib/python3.8/site-packages/dask/array/core.py in __getitem__(self, index)
   1694         )
   1695 
-> 1696         index2 = normalize_index(index, self.shape)
   1697         dependencies = {self.name}
   1698         for i in index2:

~/anaconda3/lib/python3.8/site-packages/dask/array/slicing.py in normalize_index(idx, shape)
    895     idx = idx + (slice(None),) * (len(shape) - n_sliced_dims)
    896     if len([i for i in idx if i is not None]) > len(shape):
--> 897         raise IndexError("Too many indices for array")
    898 
    899     none_shape = []

IndexError: Too many indices for array

Inspection of values via ipdb:

In []: km.fit(X)
> /root/anaconda3/lib/python3.8/site-packages/dask/array/slicing.py(898)normalize_index()
    897         import ipdb; ipdb.set_trace()
--> 898         raise IndexError("Too many indices for array")
    899 

ipdb> list
    893             n_sliced_dims += 1
    894 
    895     idx = idx + (slice(None),) * (len(shape) - n_sliced_dims)
    896     if len([i for i in idx if i is not None]) > len(shape):
    897         import ipdb; ipdb.set_trace()
--> 898         raise IndexError("Too many indices for array")
    899 
    900     none_shape = []
    901     i = 0
    902     for ind in idx:
    903         if ind is not None:

ipdb> pp idx
(slice(None, None, None), None)
ipdb> pp n_sliced_dims
1
ipdb> pp len([i for i in idx if i is not None])
1
ipdb> pp shape
()

Possibly related to #802.

Environment:

  • Dask version: 2021.03.0
  • Dask_ml version: 1.8.0
  • Python version: 3.8.5 (default, Sep 4 2020, 07:30:14), [GCC 7.3.0]
  • Operating System: Debian Buster
  • Install method (conda, pip, source): pip

If this is not reproducible on the maintainers' side, I'll be happy to provide a Dockerfile with a bit more concrete details about the underlying environment.

Also, please, let me know if this should be moved to the Dask repository instead.

Thanks!

Activity

  1. nils-braun commented on Mar 11, 2021

    @nils-braun

    I see the same issue here. One additional information: it works with Dask 2021.2.0 (which would also be a quickfix, @hristog, if you need one).

  2. hristog commented on Mar 11, 2021

    @hristog
    ContributorAuthor

    Hi @nils-braun, thanks very much! I'll give it a go, yes.

  3. TomAugspurger commented on Mar 12, 2021

    @TomAugspurger
    Member
  4. hristog commented on Mar 12, 2021

    @hristog
    ContributorAuthor

    @TomAugspurger, it might've been dask/dask#7326. I'll check if it was indeed that one (or something else), when I get a chance.

    Update: Turns out it wasn't that PR. Please, see the next comment below.

  5. hristog commented on Mar 12, 2021

    @hristog
    ContributorAuthor

    Reporting some partial update on this one (partial, because I haven't looked into the underlying specifics too much).

    I could be wrong, but according to git bisect, it seems that the erroneous behavior originated in the following commit and PR: dask/dask@bf1c65c4 / dask/dask#7183:

    commit bf1c65c4fd198e0b6b28a82b03b60ba4e7954c08
    Date:   Wed Feb 24 10:15:51 2021 -0500
    
        [WIP] ENH Adds tree reduction to bincount (#7183)
        
        * ENH Adds tree reduction to bincount
        
        * CLN Uses meta from small bincount
        
        * REV Revert change
        
        * ENH Early exit if not a list
    

    Apparently, the same commit/PR is one of the culprits behind dask/dask#7324.

    Would the dask code base benefit from some associated unit-tests, rather than having to rely upon unit-tests from dependent libraries?

    Update: I've created a proposal for utilizing repository dispatch events in dask under dask/dask#7385.

  6. TomAugspurger commented on Mar 14, 2021

    @TomAugspurger
    Member

    Would the dask code base benefit from some associated unit-tests, rather than having to rely upon unit-tests from dependent libraries?

    Yep, definitely (assuming this is an actual regression, and not dask-ml relying on buggy behavior).

  7. GenevieveBuckley commented on Mar 15, 2021

    @GenevieveBuckley
    Contributor

    I don't think this is dask-ml relying on buggy behaviour, we're seeing tests in scikit-image and cupy failing too. So I think that particular side effect was unintentional.

  8. GenevieveBuckley commented on Mar 15, 2021

    @GenevieveBuckley
    Contributor

    Can someone from dask-ml try this change and see if it fixes your issue?

    dask/dask#7391

    It seems like this helps fix similar issues in scikit-image, and I don't think I'm introducing more problems than it solves (should have a better idea of that soon though)

  9. hristog commented on Mar 15, 2021

    @hristog
    ContributorAuthor

    I've run some tests in correspondence to dask/dask#7391. It'd be best if somebody from the dask/dask-ml team could have a bit more thorough look, because I'm new to the ecosystem.

    My findings are that using the same random_state, indeed givess the same results (in terms of cluster centers) for the K-Means|| example, as it does with the code-base version prior to introducing dask/dask@bf1c65c4.

    In the table below, I've summarized the corresponding unit-test results (with respect to my local environment) for:

    • py.test dask/array/ --verbose --doctest-modules (dask)
    • py.test tests/ --verbose --doctest-modules (dask-ml)
    dask commit date dask code-base version failed (dask) passed (dask) skipped (dask) xfailed (dask) xpassed (dask) warnings (dask) errors (dask) failed (dask-ml) passed (dask-ml) skipped (dask-ml) xfailed (dask-ml) xpassed (dask-ml) warnings (dask-ml) errors (dask-ml)
    Mar 15 dask/dask#7391 (proposed fix) 0 3350 443 77 3 4 0 0 654 10 13 2 16 6
    Mar 15 immediately before dask/dask#7391 0 3350 443 78 2 4 0 18 636 10 13 2 11 6
    Feb 24 immediately before dask/dask@bf1c65c4 *0 *3338 *428 *78 *2 *4 *0 **117 **537 **10 **13 **2 **19 **6

    Table notes:

    • * : These numbers correspond to a code-base version that's 3 weeks older than the other two versions
    • **: These numbers are obtained using the same dask-ml version (the latest main from Feb 24), that was used in the other two table rows, whilst the dask code-base versions are different.

    Hope this provides a better idea about the impact of the proposed fix.

  10. GenevieveBuckley commented on Mar 16, 2021

    @GenevieveBuckley
    Contributor

    Confirmed, with the change proposed in dask/dask#7391 we can now run the dask-ml K-means example code again:

    In [1]: import dask_ml.datasets
       ...: import dask_ml.cluster
       ...: 
       ...: 
       ...: X, y = dask_ml.datasets.make_blobs(n_samples=10000000,
       ...:                                    chunks=1000000,
       ...:                                    random_state=0,
       ...:                                    centers=3)
       ...: X = X.persist()
       ...: 
       ...: km = dask_ml.cluster.KMeans(n_clusters=3, init_max_iter=2, oversampling_
       ...: factor=10)
       ...: km.fit(X)
    Out[1]: KMeans(init_max_iter=2, n_clusters=3, oversampling_factor=10)
  11. GenevieveBuckley commented on Mar 16, 2021

    @GenevieveBuckley
    Contributor

    The rest of the dask-ml test suite passes on my local machine with this fix, so I'm happy about that

  12. RichardScottOZ commented on Jun 6, 2021

    @RichardScottOZ

    Additionally, I had the above fail with this combination
    Dask version: 2021.03.0
    Dask_ml version: 1.9.0
    Python version: 3.8.5
    Operating System: Ubuntu
    Install method (conda, pip, source): pip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions