Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name '_k_means' #26

Closed
Randool opened this issue Mar 6, 2020 · 10 comments
Closed

ImportError: cannot import name '_k_means' #26

Randool opened this issue Mar 6, 2020 · 10 comments

Comments

@Randool
Copy link

Randool commented Mar 6, 2020

Hi. I install spherecluster usingpip install spherecluster successfully in Ubuntu 18.04. But when I call from spherecluster import SphericalKMeans, I got an ImportError.

Traceback (most recent call last):
  File "LM/vectors_cluster.py", line 9, in <module>
    from spherecluster import SphericalKMeans
  File "/path_to_anaconda/lib/python3.6/site-packages/spherecluster/__init__.py", line 2, in <module>
    from .spherical_kmeans import SphericalKMeans
  File "/path_to_anaconda/lib/python3.6/site-packages/spherecluster/spherical_kmeans.py", line 16, in <module>
    from sklearn.cluster import _k_means
ImportError: cannot import name '_k_means'

Here is my environmental information:

Package Version
numpy 1.14.3
scipy 1.1.0
scikit-learn 0.22.2.post1
pytest 3.5.1
nose 1.3.7
joblib 0.14.1
spherecluster 0.1.7

If anyone can help me, I would really appreciate it!

@rtrad89
Copy link

rtrad89 commented Mar 13, 2020

I'm not 100% sure, but it appears that sklearn 0.22.1 has introduced some changes that made the aforesaid code line to fail, namely:

from sklearn.cluster import _k_means

I got the code running using:

from sklearn.cluster import _kmeans as _k_means  # _k_means directly caused the error

But I am getting some new errors in the function _check_sample_weight, more precisely at the line:

n_samples = _num_samples(X)

The error reads:

TypeError: Expected sequence or array-like, got <class 'NoneType'>

Despite that, I hope this is of any help.

@Randool
Copy link
Author

Randool commented Mar 13, 2020

@rtrad89 Thanks. I also made the same changes to the code and got the same 'TypeError'. Hope for the next version.

@rtrad89
Copy link

rtrad89 commented Mar 13, 2020

@rtrad89 Thanks. I also made the same changes to the code and got the same 'TypeError'. Hope for the next version.

@Randool would you please confirm whether you have the latest spherecluster version, 0.1.7 that is? Because I noticed something may be the culprit.

In my local version of the aforementioned spherecluster (0.1.7), the call in spherical_kmeans.py is:

sample_weight = _check_sample_weight(X, sample_weight)

While in the online github version, it is in the correct form:

sample_weight = _check_sample_weight(sample_weight, X)

@Randool
Copy link
Author

Randool commented Mar 13, 2020

@rtrad89 I read the source code and found this change, so I commented this line. But I remember the result is still disappointing.

@YBZh
Copy link

YBZh commented Apr 2, 2020

It seems that downgrading the sklearn to the older version of 0.20.0 can solve the problem.
conda install scikit-learn=0.20.0

@Randool
Copy link
Author

Randool commented Apr 2, 2020

It seems that downgrading the sklearn to the older version of 0.20.0 can solve the problem.
conda install scikit-learn=0.20.0

Thanks! That's the right solution!

@Randool Randool closed this as completed Apr 2, 2020
@glicerico
Copy link

This is a workaround, not a solution.
A side-effect of downgrading to 0.20.0 is that the OPTICS clustering algorithm is not available anymore.
Can we re-open this issue, and find a proper solution for it?

@Randool Randool reopened this Apr 30, 2020
@glicerico
Copy link

glicerico commented May 1, 2020

Oh, I see this has been taken care of in the develop branch of the original repo, as @rtrad89 pointed out above.
So the next package should solve this issue, and for now one has to compile the repo code to have it working properly.
I tested with OPTICS in the develop version and it works fine :)
@Randool, sorry, I think this issue can indeed be closed.

@Randool
Copy link
Author

Randool commented May 2, 2020

haha, looking forward to the next version :> @glicerico

@rtrad89
Copy link

rtrad89 commented Aug 6, 2020

This is a workaround, not a solution.
A side-effect of downgrading to 0.20.0 is that the OPTICS clustering algorithm is not available anymore.
Can we re-open this issue, and find a proper solution for it?

Just reporting that downgrading only to 0.22.0 doesn't have this side effect.

If you are using Anaconda3, you can write:

 conda install scikit-learn=0.22.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants