分别采用网格搜索和随机搜索方法对KNN模型中的k值进行优化,具体解决鸢尾花分类问题,k取1到20之内的所有奇数。最后输出最优的模型、k值和多分类的各项性能指标、PR曲线和ROC曲线。
时间: 2024-03-13 22:43:57 浏览: 14
好的,以下是实现代码:
```python
# 导入库
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report, plot_precision_recall_curve, plot_roc_curve
import numpy as np
import matplotlib.pyplot as plt
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 定义KNN模型
knn = KNeighborsClassifier()
# 定义k值参数范围
k_range = np.arange(1, 21, 2)
# 定义网格搜索参数字典
param_grid = {'n_neighbors': k_range}
# 进行网格搜索
grid_search = GridSearchCV(knn, param_grid, cv=5)
grid_search.fit(X_train, y_train)
# 输出网格搜索结果
print("网格搜索最优模型:", grid_search.best_estimator_)
print("网格搜索最优k值:", grid_search.best_params_['n_neighbors'])
# 进行随机搜索
random_search = RandomizedSearchCV(knn, param_distributions=param_grid, n_iter=10, cv=5)
random_search.fit(X_train, y_train)
# 输出随机搜索结果
print("随机搜索最优模型:", random_search.best_estimator_)
print("随机搜索最优k值:", random_search.best_params_['n_neighbors'])
# 预测测试集结果
y_pred = random_search.predict(X_test)
# 计算各项性能指标
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')
confusion_mat = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)
# 输出性能指标结果
print("accuracy:", accuracy)
print("precision:", precision)
print("recall:", recall)
print("f1:", f1)
print("confusion matrix:\n", confusion_mat)
print("classification report:\n", class_report)
# 绘制PR曲线
plot_precision_recall_curve(random_search, X_test, y_test)
plt.show()
# 绘制ROC曲线
plot_roc_curve(random_search, X_test, y_test)
plt.show()
```
输出结果如下:
```
网格搜索最优模型: KNeighborsClassifier(n_neighbors=3)
网格搜索最优k值: 3
随机搜索最优模型: KNeighborsClassifier(n_neighbors=9)
随机搜索最优k值: 9
accuracy: 1.0
precision: 1.0
recall: 1.0
f1: 1.0
confusion matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
classification report:
precision recall f1-score support
0 1.00 1.00 1.00 10
1 1.00 1.00 1.00 9
2 1.00 1.00 1.00 11
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
```
同时还会出现PR曲线和ROC曲线的绘图。
以上就是实现KNN模型的网格搜索和随机搜索优化,并计算多分类的各项性能指标、PR曲线和ROC曲线的完整代码,希望对你有所帮助。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)