Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

name 'KMeans' is not defined in Jupyter Notebook

enter image description here

I am unable to run KMeans() in my Jupyter Notebook. The same piece of code works correctly if I just use it in a Python script. I have already installed KMeans/Sklearn using.

!pip3 install KMeans

Then what's the issue?

like image 268
x89 Avatar asked Nov 05 '25 07:11

x89


2 Answers

The above two cells are not yet executed.

Please re-run all the cells. Probably you've restarted the kernel and forgot to run the cells where you've imported the KMeans from sklearn.cluster.

like image 140
Strange Avatar answered Nov 06 '25 22:11

Strange


Straightforward answer: Write in a cell of the Jupyter notebook (in In[6] for example:

from sklearn.cluster import KMean
km=KMean(1)

And see if there is an error.

Long answer You have different cells in the Jupyter notebook. You need to execute all the cells if you have restarted Jupyter Notebook.

On your screenshots: It is written In[] in the two first pieces of code, and In[6] in the piece of code where you got the error. That might mean that you did not execute the two first cells (In[]) before the third (In[6]), thus the Kmean had not been imported.

What you can do: - Ensure you have executed the two first pieces of code ? - Ensure you did not clear the variables before executing In[6]? (not sure if this function exists on Jupyter)

like image 39
totalMongot Avatar answered Nov 06 '25 22:11

totalMongot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!