运用KNN对breast—cancer进行分类,通过GridSearchCV寻找最优的参数,并给出bestscore。
时间: 2023-06-19 10:06:59 浏览: 89
好的,我可以为您提供代码实现。首先,我们需要导入必要的库和数据集:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
# 加载数据集
data = load_breast_cancer()
X = data.data
y = data.target
```
然后,我们将数据集分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
接下来,我们使用GridSearchCV来搜索最优参数,并训练模型:
```python
# 定义超参数范围
param_grid = {'n_neighbors': [3, 5, 7, 9, 11, 13, 15]}
# 定义KNN分类器
knn = KNeighborsClassifier()
# 使用GridSearchCV搜索最优参数
grid_search = GridSearchCV(knn, 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_)
```
最后,我们可以使用测试集评估模型的性能:
```python
# 使用最优参数构建模型
knn_best = KNeighborsClassifier(n_neighbors=grid_search.best_params_['n_neighbors'])
knn_best.fit(X_train, y_train)
# 输出测试集得分
print("Test Set Score: ", knn_best.score(X_test, y_test))
```
完整代码如下:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
# 加载数据集
data = load_breast_cancer()
X = data.data
y = data.target
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义超参数范围
param_grid = {'n_neighbors': [3, 5, 7, 9, 11, 13, 15]}
# 定义KNN分类器
knn = KNeighborsClassifier()
# 使用GridSearchCV搜索最优参数
grid_search = GridSearchCV(knn, 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_)
# 使用最优参数构建模型
knn_best = KNeighborsClassifier(n_neighbors=grid_search.best_params_['n_neighbors'])
knn_best.fit(X_train, y_train)
# 输出测试集得分
print("Test Set Score: ", knn_best.score(X_test, y_test))
```
阅读全文