【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参数来继续优化。

相关推荐
把所有砖敲烂32 分钟前
DeepSeek V4正式版发布,教你在Codex中配置Flash
人工智能·产品
wyg_03111337 分钟前
从0搭建极简transformer大模型
人工智能·深度学习·transformer
老王以为43 分钟前
走进 AI Agent
前端·人工智能·架构
dong_junshuai1 小时前
每天一个开源项目#59 AirLLM:27.6K星,3.72GB显存跑2.8T模型
人工智能·程序员·github
文心快码BaiduComate1 小时前
Comate 已支持DeepSeek-V4-Flash 正式版
人工智能·程序员
MartinYeung51 小时前
[论文学习]OSReward:为跨平台计算机使用奖励模型建立标准化评估
人工智能·学习
MC皮蛋侠客1 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
数字供应链安全产品选型2 小时前
悬镜安全打造“中国版 Anthropic Mythos”:以AI治理AI,重构新一代数字供应链安全
人工智能·安全·重构
Tangyuewei2 小时前
2026 下半年:从更快到更可控
人工智能
Zane19942 小时前
@property 到底是怎么把方法伪装成属性的?一文吃透 property、staticmethod、classmethod
后端·python