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

相关推荐
斐夷所非26 分钟前
人工智能 AI. 机器学习 ML. 深度学习 DL. 神经网络 NN 的区别与联系
人工智能
递归不收敛40 分钟前
吴恩达机器学习课程(PyTorch适配)学习笔记:2.4 激活函数与多类别处理
pytorch·学习·机器学习
F_D_Z1 小时前
数据集相关类代码回顾理解 | StratifiedShuffleSplit\transforms.ToTensor\Counter
python·torchvision·transforms
Funny_AI_LAB2 小时前
OpenAI DevDay 2025:ChatGPT 进化为平台,开启 AI 应用新纪元
人工智能·ai·语言模型·chatgpt
深瞳智检2 小时前
YOLO算法原理详解系列 第002期-YOLOv2 算法原理详解
人工智能·算法·yolo·目标检测·计算机视觉·目标跟踪
tao3556672 小时前
【Python刷力扣hot100】283. Move Zeroes
开发语言·python·leetcode
深眸财经3 小时前
机器人再冲港交所,优艾智合能否破行业困局?
人工智能·机器人
小宁爱Python3 小时前
从零搭建 RAG 智能问答系统1:基于 LlamaIndex 与 Chainlit实现最简单的聊天助手
人工智能·后端·python
湖南人爱科技有限公司3 小时前
RaPhp和Python某音最新bd-ticket-guard-client-data加密算法解析(视频评论)
android·python·php·音视频·爬山算法·raphp
新知图书4 小时前
Encoder-Decoder架构的模型简介
人工智能·架构·ai agent·智能体·大模型应用开发·大模型应用