pythonmachine-learningsvmnameerrorgridsearchcv

NameError: name 'GridSearchCV' is not defined. I have done the import though : from sklearn.model_selection import GridSearchCV



NameError Traceback (most recent call last) C:\Users\VAISHN~1\AppData\Local\Temp/ipykernel_6520/2922578464.py in 3 'kernel': ['rbf']} 4 ----> 5 optimal_params=GridSearchCV( 6 SVC(), 7 param_grid,

NameError: name 'GridSearchCV' is not defined
Here is my code:

param_grid={'C': [0.5,1,10,100],
     'gamma': ['scale',1,0.1,0.001,0.0001],
     'kernel': ['rbf']}

optimal_params=GridSearchCV(
    SVC(),
    param_grid,
    cv=5,
    scoring='accuracy',
    verbose=0
)
optimal_params.fit(X_train_scaled,y_train)
print(optimal_params.best_params_)

Solution

  • There could be 3 reasons on why this is happening:

    1. You haven't installed the library in your environment. You can solve this using the code below:
    python -m pip install --upgrade pip
    pip install -U scikit-learn
    
    1. You might have it installed but you are not importing it:
    from sklearn.model_selection import GridSearchCV
    

    UPDATE

    1. You might have an outdated version of the scikit learn package. So just try to upgrade it
    pip install --upgrade scikit-learn