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

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


NameError Traceback(最后一次调用) C:\Users\VAISHN~1\AppData\Local\Temp/ipykernel_6520/2922578464.py 中 3 'kernel': ['rbf']} 4个 ----> 5 optimal_params=GridSearchCV( 6 SVC(), 7 param_grid,

NameError: 名称 'GridSearchCV' 未定义
这是我的代码:

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_)

发生这种情况可能有 3 个原因:

  1. 您还没有在您的环境中安装该库。您可以使用以下代码解决此问题:
python -m pip install --upgrade pip
pip install -U scikit-learn
  1. 您可能安装了它但没有导入它:
from sklearn.model_selection import GridSearchCV

更新

  1. 您的 scikit 学习包版本可能已过时。所以只是尝试升级它
pip install --upgrade scikit-learn