python - 鸢尾花数据集的 KNN 和 SVM GridsearchCV
问题描述
我遇到了以下查询的问题,该查询应该为 KNN 和不同类型的 SVM 绘制最佳参数:线性、Rbf、Poly。
到目前为止,我写了下面的查询:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn import datasets
from sklearn.model_selection import GridSearchCV
from matplotlib.colors import ListedColormap
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
iris_data = iris["data"]
iris_target = iris["target"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0)
param_poly = {'coef0': [0, 1], 'degree': [0, 1, 5, 7, 10], 'C': [0.1, 1, 5, 10, 100]}
# KNN
KNN = GridSearchCV(KNeighborsClassifier(),{'n_neighbors': [1, 5, 7, 10]},cv=5).fit(X_train, y_train)
# LinearSVC (linear kernel)
SVM_lin = GridSearchCV(SVC(kernel='linear'), {'C': [0.1, 1, 5, 10]},cv=5).fit(X_train, y_train)
# SVC with RBF kernel
SVM_rbf = GridSearchCV(SVC(kernel='rbf'), {'C': [0.1, 1, 5, 10]}, cv=5).fit(X_train, y_train)
# SVC with polynomial (degree 3) kernel
SVM_poly = GridSearchCV(SVC(kernel='poly'),param_poly, cv=5).fit(X_train, y_train)
# title for the plots
titles = ['KNN Plot',
'LinearSVC (linear kernel)',
'SVC with polynomial kernel', 'SVC with RBF kernel']
for i, clf in enumerate(KNN, SVM_lin, SVM_poly, SVM_rbf):
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
plt.subplot(2, 2, i + 1)
plt.subplots_adjust(wspace=0.4, hspace=0.4)
plt.title(titles[i])
def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
# setup marker generator and color map
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
# plot the decision surface
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
np.arange(x2_min, x2_max, resolution))
Z = clf.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
# Plot also the training points
X_test, y_test = X[test_idx, :], y[test_idx]
for idx, cl in enumerate(np.unique(y)):
plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
alpha=0.8, c=cmap(idx),
marker=markers[idx], label=cl)
# highlight test samples
if test_idx:
X_test, y_test = X[test_idx, :], y[test_idx]
plt.scatter(X_test[:, 0], X_test[:, 1], c='',
alpha=1.0, linewidth=1, marker='o',
s=55, label='test set')
X_combined_std = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(X_combined_std,
y_combined, classifier=clf,
test_idx=range(105, 150))
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
plt.scatter(x=iris_data[iris_target == 0][:, 0], y=iris_data[iris_target == 0][:, 1], color="tab:blue",
label="iris_setosa")
plt.scatter(x=iris_data[iris_target == 1][:, 0], y=iris_data[iris_target == 1][:, 1], color="tab:orange",
label="iris_versicolor")
plt.scatter(x=iris_data[iris_target == 2][:, 0], y=iris_data[iris_target == 2][:, 1], color="tab:green",
label="iris_virginica")
plt.xticks(())
plt.yticks(())
plt.legend()
plt.show()
请帮我绘制如图所示的结果,代码也需要时间执行也许可以更快地完成。
解决方案
推荐阅读
- javascript - 如何改变 Map 对象 ES6 中给定键的值
- r - 使用 extrafont 或 showtext 库向 R 添加字体(在 Mac 上通过 FontBook)
- python - 从 Python 中的语言环境中去除变体?
- android-studio - cmd:找不到服务:活动#Error:ADB在flutter中以退出代码1退出
- python - BeautifulSoup Python 没有捕获文件中的所有 HTML
- python - 用于仅 FTP 虚拟主机的 Python
- android - Android:如何将 R.string 值从 strings.xml 设置为 Kotlin 中的全局常量?
- boost - 在 CMakeLists.txt 中使用 find_package(Boost ... ) 时出现 CMake 错误:未找到合适的构建变体
- r - stat_summary连接不同组的ggplot问题
- python - Python 实例变量