question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing sklearn.cluster throws ImportError

See original GitHub issue

When I try to import the sklearn.cluster submodule the following Exception is raised:

ImportError: cannot import name 'k_means'

Steps/Code to Reproduce

I am running Python 3.6.9 with scikit-learn 0.22 on an Ubuntu 18.04. A single import statement is enough to trigger that behavior for me:

import sklearn.cluster

Error Message

That’s the output I get:

ImportError Traceback (most recent call last) <ipython-input-23-c8a0f0fed13e> in <module>() ----> 1 import sklearn.cluster

/home/XXX/.local/lib/python3.6/site-packages/sklearn/cluster/init.py in <module>() 4 “”" 5 ----> 6 from ._spectral import spectral_clustering, SpectralClustering 7 from ._mean_shift import (mean_shift, MeanShift, 8 estimate_bandwidth, get_bin_seeds)

/home/XXX/.local/lib/python3.6/site-packages/sklearn/cluster/_spectral.py in <module>() 16 from …neighbors import kneighbors_graph, NearestNeighbors 17 from …manifold import spectral_embedding —> 18 from ._k_means import k_means 19 20

ImportError: cannot import name ‘k_means’


Versions

Output of sklearn.show_versions():

System: python: 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0] executable: /usr/bin/python3 machine: Linux-5.0.0-37-generic-x86_64-with-Ubuntu-18.04-bionic

Python dependencies: pip: 19.3.1 setuptools: 45.0.0 sklearn: 0.22 numpy: 1.18.1 scipy: 1.4.1 Cython: None pandas: 0.25.3 matplotlib: 3.1.2 joblib: 0.14.1

Built with OpenMP: True

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tensionheadcommented, Jan 18, 2020

Hey @glemaitre, so I uninstalled scikit-learn via pip3 and re-installed it, problem has gone away! Even though we didn’t get to the bottom of this (why did some submodules work others not), I think we can close this?!

0reactions
glemaitrecommented, Nov 10, 2020

You should not do that. I am pasting the snippet of documentation and lock this thread.

from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0],
              [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
Read more comments on GitHub >

github_iconTop Results From Across the Web

importing KMeans from sklearn.cluster throws error #18841
cluster e.g. DBSCAN and Birch works without issue. Indeed, importing sklearn. cluster alone doesn't result in an error nor have I had any...
Read more >
python - Import error. Can't import kmeans_plusplus
You need to import KMeans and set the init key word argument to kmeans++ to obtain the behaviour you want from sklearn.cluster import...
Read more >
How to Fix : “ImportError: Cannot import name X” in Python?
You can solve the “ ImportError : Cannot import name X” Error by resolving the circular dependencies. You can do that either by...
Read more >
no module named 'sklearn.metrics.classification' ( Solved )
Most Importantly In this scenario, even after the proper way of importing, Interpreter will the though the same error. There are two possible...
Read more >
Unable to import sklearn.mixture in CP3 - Usage & Issues
If I instead include import sklearn.mixture, a different error is thrown at startup: ImportError: No module named mixture.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found