No module named ‘sklearn.datasets.samples_generator’ ( Solved )

modulenotfounderror_ no module named 'sklearn.datasets.samples_generator'

The root cause for modulenotfounderror: no module named ‘sklearn.datasets.samples_generator‘ is samples_generator module replacement in scikit-learn. Apart from it, Another most common reason behind the same error is bad practice for developers to import any Python statement. Well, I know two lines about root causes are not sufficient to fix the issue. Hence let’s understand the same in a detailed way.

modulenotfounderror: no module named ‘sklearn.datasets.samples_generator’ ( Root Cause and Fix ) –

Well in both of the most possible root causes, module replacement in the latest version is more frequent. So let’s explore it first.

Case 1: Internal code structure change

Actually, if you are using this importing statement in the recent version of sckit-learn –

from sklearn.datasets.samples_generator import make_blobs
modulenotfounderror no module named 'sklearn.datasets.samples_generator'
modulenotfounderror no module named ‘sklearn.datasets.samples_generator’

Then you will encounter this error for sure. Because this samples_generator explicitly is not available in scikit-learn in recent versions. So we better call the above statement in correct way like below –

from sklearn.datasets import make_blobs
correct -no module named 'sklearn.datasets.samples_generator'
correct -no module named ‘sklearn.datasets.samples_generator’

Case 2: Syntax error ( Not Valid for recent scikit-learn module)

Sometimes The syntax we write for importing any python module is typically wrong. IN order to differentiate between incorrect and correct ones. Let’s take the practical example –

Incorrect –

import sklearn.datasets.samples_generator

Here we directly import all together but that is a syntax error in python. The funny fact is most of us do it like this including me as well. Well, now let’s see the correct way for this.

from sklearn.datasets.samples_generator import make_blobs

But this will run in the lower version of scikit-learn because as I explained in the above section samples_generator no longer exists separately. But In some scenarios, where we are running the lower version for scikit-learn, we can use the above way of importing.

Bonus Tip –

Do not forget to install scikit-learn again if the error persists even after retrying these tricks. Use any of the below package managers to install the module.

pip install -U scikit-learn

Or

conda install -c anaconda scikit-learn

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner