svc和网格搜索调参数代码
时间: 2023-10-11 13:06:44 浏览: 102
下面是使用SVC和网格搜索调参的示例代码:
```python
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# 加载数据集
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=0)
# 定义模型
svc = SVC()
# 定义参数范围
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf'], 'gamma': [0.1, 1, 10]}
# 网格搜索
grid_search = GridSearchCV(svc, param_grid, cv=5)
grid_search.fit(X_train, y_train)
# 输出最优参数和得分
print('Best parameters: ', grid_search.best_params_)
print('Best score: ', grid_search.best_score_)
print('Test set score: ', grid_search.score(X_test, y_test))
```
在上面的代码中,我们使用了`SVC`模型,定义了参数范围`param_grid`,然后使用`GridSearchCV`进行网格搜索,最后输出了最优参数和得分。可以根据实际情况修改参数范围和模型类型。
阅读全文
相关推荐


















