ImportError No module named grid_search, learning_curve
Codemia Knowledge Hub ·
Introduction
The ImportError: No module named grid_search and ImportError: No module named learning_curve errors occur when using import paths from scikit-learn versions prior to 0.18. In scikit-learn 0.18, several modules were reorganized — grid_search and learning_curve were moved into the model_selection module. Code written for older versions breaks on newer installations.
The Error
The Fix: Updated Import Paths
The reorganization was meant to put grid search, cross-validation, and learning-curve utilities under one model-selection namespace. So when you see this error, the fix is usually code migration rather than package installation.
Grid Search
Learning Curve
Cross-Validation
Complete Migration Reference
| Old Import Path (< 0.18) | New Import Path (>= 0.18) |
sklearn.grid_search.GridSearchCV | sklearn.model_selection.GridSearchCV |
sklearn.grid_search.RandomizedSearchCV | sklearn.model_selection.RandomizedSearchCV |
sklearn.grid_search.ParameterGrid | sklearn.model_selection.ParameterGrid |
sklearn.learning_curve.learning_curve | sklearn.model_selection.learning_curve |
sklearn.learning_curve.validation_curve | sklearn.model_selection.validation_curve |
sklearn.cross_validation.cross_val_score | sklearn.model_selection.cross_val_score |
sklearn.cross_validation.KFold | sklearn.model_selection.KFold |
sklearn.cross_validation.StratifiedKFold | sklearn.model_selection.StratifiedKFold |
sklearn.cross_validation.train_test_split | sklearn.model_selection.train_test_split |
Working Examples with New Imports
GridSearchCV
Learning Curve
Checking Your scikit-learn Version
If you are maintaining an older codebase, decide whether you want to migrate imports forward or pin scikit-learn to an older version temporarily. Migration is usually the better long-term choice because it keeps examples, dependencies, and documentation aligned.
Common Pitfalls
- Virtual Environments: Different virtual environments may have different scikit-learn versions. Always check the version in your active environment with
sklearn.__version__. - Documentation Review: When following tutorials or Stack Overflow answers, check the publication date. Code from pre-2016 articles likely uses old import paths.
- Deprecation warnings: scikit-learn 0.18-0.19 showed deprecation warnings before removing the old modules entirely in 0.20. If you see
DeprecationWarning, update imports before upgrading further. - Conda vs pip: Conda environments may lag behind pip for scikit-learn versions. Use
conda update scikit-learnorpip install --upgrade scikit-learndepending on your package manager. - Frozen requirements: If your project has
requirements.txtpinningscikit-learn<0.18, old imports work but you miss years of bug fixes and performance improvements.
Summary
grid_search,learning_curve, andcross_validationmodules were moved tosklearn.model_selectionin version 0.18- Replace
from sklearn.grid_search import GridSearchCVwithfrom sklearn.model_selection import GridSearchCV - Replace
from sklearn.learning_curve import learning_curvewithfrom sklearn.model_selection import learning_curve - Check your version with
sklearn.__version__and upgrade withpip install --upgrade scikit-learn - Always check the date of tutorials and examples to avoid using outdated import paths
Related reading
- ImportError No module named 'keras
- ImportError No module named 'matplotlib' -- Using Anaconda tensorflow environment
- ImportError No module named matplotlib.pyplot
- ImportError No module named 'MySQL
- ImportError No module named MySQLdb
- ImportError No module named 'nets
- ImportError No module named pandas
- ImportError No module named PIL
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.