【Python机器学习】SVM——预处理数据

为了解决特征特征数量级差异过大,导致的模型过拟合问题,有一种方法就是对每个特征进行缩放,使其大致处于同一范围。核SVM常用的缩放方法是将所有的特征缩放到0和1之间。

"人工"处理方法:

python 复制代码
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
cancer=load_breast_cancer()
X_train,X_test,y_train,y_test=train_test_split(cancer.data,cancer.target,random_state=0)

#计算训练集中每个特征的最小值
min_on_train=X_train.min(axis=0)
#计算训练集中每个特征的范围(最小值-最大值)
range_on_train=(X_train-min_on_train).max(axis=0)
#减去最小值,然后除以范围,这样最大值都是1,最小值都是0
X_train_scales=(X_train-min_on_train)/range_on_train

print('每个特征的最小值:{}'.format(X_train_scales.min(axis=0)))
print('每个特征的最大值:{}'.format(X_train_scales.max(axis=0)))

X_test_scales=(X_test-min_on_train)/range_on_train

svc=SVC(C=1,gamma=1)
svc.fit(X_train_scales,y_train)
print('训练集精度:{:.3f}'.format(svc.score(X_train_scales,y_train)))
print('测试集精度:{:.3f}'.format(svc.score(X_test_scales,y_test)))

可以看到,最终的结果上训练集和测试集的精度都非常好,但还没有接近100%的精度,可能存在欠拟合,后续可以通过调整C参数来继续优化。

相关推荐
byte轻骑兵7 小时前
【HID】规范精讲[10]: 蓝牙HID设备的连接基石——GAP协议如何掌控发现、连接与安全
人工智能·人机交互·蓝牙·键盘·鼠标·hid
OJAC1117 小时前
软件工程应届生不想留在原路上,他一周内等来了上海offer
人工智能
小仙女的小稀罕7 小时前
口碑推荐 | 解决会议记录功能单一的实用靠谱方案
人工智能
Dfreedom.7 小时前
【实战篇】分类任务全流程演示——决策树
人工智能·算法·决策树·机器学习·分类
Joseph Cooper7 小时前
大模型微调技术完全指南:从原理到实践的系统性入门(2026 版)
人工智能·chatgpt·lora·peft·qlora·大模型微调·dpo
我鑫如一7 小时前
口碑好的AI API中转站哪家强
人工智能·python
无忧智库7 小时前
从0开始全面认识高质量数据集建设指南
大数据·人工智能·知识图谱
赵优秀一一7 小时前
AI入门学习
人工智能·pytorch·深度学习
晶台光耦7 小时前
领时代·智未来 | 晶台光耦亮相2026北京车展
人工智能·光耦·光耦应用