NameError: name 'GridSearchCV' is not defined
时间: 2023-09-16 11:13:01 浏览: 353
Python中对错误NameError: name ‘xxx’ is not defined进行总结
5星 · 资源好评率100%
This error message typically occurs when the `GridSearchCV` function from the `sklearn.model_selection` module is not imported properly or is misspelled.
Here is an example of how to import and use `GridSearchCV`:
```python
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.datasets import load_iris
# Load iris dataset
iris = load_iris()
# Set up SVM classifier
svm = SVC()
# Set up parameter grid for GridSearchCV
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf', 'sigmoid']}
# Set up GridSearchCV object
grid = GridSearchCV(svm, param_grid, cv=5)
# Fit the GridSearchCV object to the data
grid.fit(iris.data, iris.target)
# Print the best parameters and score
print("Best parameters: ", grid.best_params_)
print("Best score: ", grid.best_score_)
```
Make sure that you have the necessary packages installed and imported (`sklearn` in this case).
阅读全文