NameError Name GridSearchCV is Not Defined

When you are working with Machine learning models and want to get accurate results, you mostly use hyperparameter tuning methods. One of the famous hyperparameter tuning methods is GridSearchCV if you are getting NameError: Name GridSearchCV is Not Defined error when implementing GridSearchCV using Python and sklearn module. Then it is because you are either having an outdated sklearn module or you are not importing the GridSearchCV class.

In this short article, we will discuss how we can solve NameError: Name GridSearchCV is Not Defined error using various methods. Moreover, we will also discuss the reasons for getting the error as well. If the given solutions in this article will not help you to solve the issue, please comment below and we will try to help you.

How to Solve NameError: Name GridSearchCV is Not Defined?

We know how important it is to tune the model to get more accurate results but getting NameError: Name GridSearchCV is Not Defined error when using GridSearchCV seems to be very annoying. Well, there can be many possible reasons for getting this error but the three most common and important reasons for getting NameError: Name GridSearchCV is Not Defined are as follows:

  • You haven’t installed the module on your systems
  • You haven’t imported the module correctly from sklearn
  • Your sklearn module is outdated.

Well, if you are getting the error because of any of the given reasons, then go through the rest of the article where we will mention how to solve the problem.

Solution-1: Installing the Module on Your System

Sometimes, it happened that we forget to install the required modules and run the code, and as a result, we come up with errors. Similarly, first and the most important is to check if you have installed the module on your system or not. Usually, this happens, when you are using a virtual environment because for the virtual environment you need to install the module separately for each of the projects.

You can use any of the given methods, depending on your operating system to install the sklearn module on your system.

# installing sklearn module
pip install -U scikit-learn

# using pip install 
pip install scikit-learn

# using conda
conda install scikit-learn

#for jupyter notebook
pip install -U scikit-learn

# for ubuntu
sudo install scikit-learn

Once the installation is complete, you can run the code again and see, if you are still getting the error or not. If the error continues even after installing the module, then there must be a problem with the import statement.

Solution-2: Importing GridSearchCV

If the error continues after installing the module, there must be a problem with the import statement. Either you are importing the module incorrectly or you are not importing it at all.

Here is how you can add the import statement before the GridSearchCV code.

from sklearn.model_selection import GridSearchCV

Hope this will help you to get rid of NameError: Name GridSearchCV is Not Defined error.

Solution-3: Updating the Sklearn Module

Another main reason for getting the error is because of having an outdated sklearn module on your system. The error will be gone by updating the sklearn module.

You can use any of the following commands to upgrade the sklearn module.

# using pip command
pip install --upgrade scikit-learn

# for jupyter user
!pip install --upgrade scikit-learn

# anaconda users
conda install --upgrade scikit-learn

Hopefully, you will get rid of errors.

Implementation of GridSearchCV in Python

Here we will just take an example and see how we can use the sklearn GridSearchCV to find the optimum values for the given parameters. As an example of the LightGBM module and find the optimum parameter values using the GridSearchCV method.

Let us first import the dataset.

import pandas as pd

# importing dataset 
data = pd.read_csv('house.csv')
data.dropna(inplace=True)

# heading of the dataset
data.head()

Output:

NameError: Name GridSearchCV is Not Defined

Let us now, import the required modules and apply sklearn GridSearchCV to find the optimum parameter values.

import lightgbm as lgb
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import GridSearchCV

# defiing the model
model = lgb.LGBMRegressor()


# creating a dict of grids
grid = dict()

# values for iteration
grid['n_estimators'] = [10, 50, 100, 500]

# values for learning rate
grid['learning_rate'] = [0.0001, 0.001, 0.01, 0.1, 1.0]

# values for the sampel
grid['subsample'] = [0.5, 0.7, 1.0]

# values for teh depth of tree
grid['max_depth'] = [3, 4, 5]

# defining the cv
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3)


# applying the gridsearchcv method
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=cv, scoring='r2')

# storing the values
grid_result = grid_search.fit(Input, Output)


# printing the best parameters
print("Accuracy score: %f using %s" % (grid_result.best_score_, grid_result.best_params_))

Output:

Accuracy score: 0.532401 using {'learning_rate': 0.1, 'max_depth': 5, 'n_estimators': 50, 'subsample': 0.5}

As you can see, the GridSearchCV returned the optimum values of the given model along with the R-2 score.

Conclusion

NameError: Name GridSearchCV is Not Defined is a common error that occurs usually when dealing with hyperparameter tuning of the model in Machine learning or deep learning. There can be many reasons for getting this error. In this short article, we discussed three main reasons for getting the error and covered how we can solve the error using various methods.

Related Errors

Leave a Comment