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

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

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.