酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

葡萄酒数据集:带多项式核 SVM 超参数调优,多策略寻最优准确率!

```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. 考虑使用交叉验证来更全面地评估模型性能。 ######[AI写代码神器 | 418点数解答 | 2024-12-17 22:15:53]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]