Python
ImportError
grid_search
learning_curve
troubleshooting

ImportError No module named grid_search, learning_curve

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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

python
1# Old imports that no longer work (scikit-learn < 0.18)
2from sklearn.grid_search import GridSearchCV       # ImportError!
3from sklearn.learning_curve import learning_curve   # ImportError!
4from sklearn.cross_validation import cross_val_score  # ImportError!

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.

python
1# Old (deprecated)
2from sklearn.grid_search import GridSearchCV
3from sklearn.grid_search import RandomizedSearchCV
4
5# New (scikit-learn >= 0.18)
6from sklearn.model_selection import GridSearchCV
7from sklearn.model_selection import RandomizedSearchCV

Learning Curve

python
1# Old (deprecated)
2from sklearn.learning_curve import learning_curve
3from sklearn.learning_curve import validation_curve
4
5# New (scikit-learn >= 0.18)
6from sklearn.model_selection import learning_curve
7from sklearn.model_selection import validation_curve

Cross-Validation

python
1# Old (deprecated)
2from sklearn.cross_validation import cross_val_score
3from sklearn.cross_validation import KFold
4from sklearn.cross_validation import train_test_split
5
6# New (scikit-learn >= 0.18)
7from sklearn.model_selection import cross_val_score
8from sklearn.model_selection import KFold
9from sklearn.model_selection import train_test_split

Complete Migration Reference

Old Import Path (< 0.18)New Import Path (>= 0.18)
sklearn.grid_search.GridSearchCVsklearn.model_selection.GridSearchCV
sklearn.grid_search.RandomizedSearchCVsklearn.model_selection.RandomizedSearchCV
sklearn.grid_search.ParameterGridsklearn.model_selection.ParameterGrid
sklearn.learning_curve.learning_curvesklearn.model_selection.learning_curve
sklearn.learning_curve.validation_curvesklearn.model_selection.validation_curve
sklearn.cross_validation.cross_val_scoresklearn.model_selection.cross_val_score
sklearn.cross_validation.KFoldsklearn.model_selection.KFold
sklearn.cross_validation.StratifiedKFoldsklearn.model_selection.StratifiedKFold
sklearn.cross_validation.train_test_splitsklearn.model_selection.train_test_split

Working Examples with New Imports

GridSearchCV

python
1from sklearn.model_selection import GridSearchCV
2from sklearn.ensemble import RandomForestClassifier
3
4param_grid = {
5    'n_estimators': [50, 100, 200],
6    'max_depth': [5, 10, None]
7}
8
9grid = GridSearchCV(
10    RandomForestClassifier(),
11    param_grid,
12    cv=5,
13    scoring='accuracy'
14)
15grid.fit(X_train, y_train)
16print(f"Best params: {grid.best_params_}")

Learning Curve

python
1from sklearn.model_selection import learning_curve
2import matplotlib.pyplot as plt
3import numpy as np
4
5train_sizes, train_scores, val_scores = learning_curve(
6    RandomForestClassifier(),
7    X, y,
8    train_sizes=np.linspace(0.1, 1.0, 10),
9    cv=5,
10    scoring='accuracy'
11)
12
13plt.plot(train_sizes, np.mean(train_scores, axis=1), label='Training')
14plt.plot(train_sizes, np.mean(val_scores, axis=1), label='Validation')
15plt.xlabel('Training Set Size')
16plt.ylabel('Accuracy')
17plt.legend()
18plt.show()

Checking Your scikit-learn Version

python
1import sklearn
2print(sklearn.__version__)
3
4# Upgrade if needed
5# pip install --upgrade scikit-learn

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-learn or pip install --upgrade scikit-learn depending on your package manager.
  • Frozen requirements: If your project has requirements.txt pinning scikit-learn<0.18, old imports work but you miss years of bug fixes and performance improvements.

Summary

  • grid_search, learning_curve, and cross_validation modules were moved to sklearn.model_selection in version 0.18
  • Replace from sklearn.grid_search import GridSearchCV with from sklearn.model_selection import GridSearchCV
  • Replace from sklearn.learning_curve import learning_curve with from sklearn.model_selection import learning_curve
  • Check your version with sklearn.__version__ and upgrade with pip install --upgrade scikit-learn
  • Always check the date of tutorials and examples to avoid using outdated import paths

Course illustration
Course illustration

All Rights Reserved.