酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

仍基于上述的葡萄酒数据集,使用带多项式核函数的支持向量机进行分类,并尝试调优多项式核函数的超参数degree以及正则化系数c,使其性能达到最优。使用下面提供的数据,不要改动或进行归一化!尽自己最大所能调,并给出达到最大准确率时两个超参数的取值。

```python from sklearn import svm from sklearn.datasets import load_wine from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # 加载葡萄酒数据集 wine = load_wine() X = wine.data y = wine.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 定义超参数的取值范围 degree_values = [2, 3, 4, 5] c_values = [0.1, 1, 10, 100] best_accuracy = 0 best_degree = 0 best_c = 0 # 遍历超参数组合进行调优 for degree in degree_values: for c in c_values: # 创建支持向量机分类器 clf = svm.SVC(kernel='poly', degree=degree, C=c) # 训练模型 clf.fit(X_train, y_train) # 在测试集上进行预测 y_pred = clf.predict(X_test) # 计算准确率 accuracy = accuracy_score(y_test, y_pred) # 更新最优超参数和准确率 if accuracy > best_accuracy: best_accuracy = accuracy best_degree = degree best_c = c print("最优的 degree 取值为:", best_degree) print("最优的 C 取值为:", best_c) print("最大准确率为:", best_accuracy) ``` 额外需要注意的逻辑点: 1. 数据划分的随机性可能会影响结果,可多次运行以获取更稳定的评估。 2. 超参数的取值范围可以根据实际情况进一步扩大或细化。 3. 考虑使用交叉验证来更全面地评估模型性能。 [2024-12-17 22:15:53 | AI写代码神器 | 418点数解答]

相关提问