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

相关推荐
染指11104 小时前
26.RAG进阶(Advanced RAG)-假设性问题索引
人工智能·windows·agent·rag·advanced rag
闵孚龙4 小时前
动态图机制:为什么 PyTorch 调试起来更舒服
人工智能·pytorch·python
chushiyunen5 小时前
langchain4j笔记、tools
笔记·python·flask
甲维斯5 小时前
还要啥Codex!DeepSeek接入Zcode远程连接!
人工智能
百胜软件@百胜软件5 小时前
百胜软件亮相“AI消费新生活”主题日活动,AI智能运营平台入选市级案例征集
人工智能·生活·零售数字化·数智中台·珠宝行业
程序员三藏5 小时前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
在放️6 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
专注搞钱6 小时前
GPT-4o写设备Recipe:从3小时到10分钟
数据库·人工智能·gpt·半导体
闻道参看6 小时前
贝芯宠AI灵兽 ELFVET 大模型聚焦临床应用,强化宠物诊疗综合能力
人工智能·宠物
MartinYeung56 小时前
[论文学习]重新思考大型语言模型忘却目标:梯度视角与超越
人工智能·学习·语言模型