【Sklearn】【实战】用SVM进行乳腺癌检测,模拟线上部署(一)

0. 前言

1. 需求

  1. 通过sklearn中的自带乳腺癌数据集 训练 svc分类器
  2. 对模型进行保存,即为模型持久化
  3. 载入训练好的模型,对模型模拟线上测试

2. 数据集介绍

  1. 数据集官网介绍:sklearn.datasets.load_breast_cancer
  2. 二分类任务:212(M) + 357(B) = 569
  3. 特征个数:30 维
    在这里插入图片描述

3. 导入依赖包

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import  classification_report
from sklearn.model_selection import GridSearchCV
import joblib
import time

4. 载入数据集

cancer = load_breast_cancer()
X = cancer.data
Y = cancer.target
print(f'The shape of cancer dataset is {X.shape}')
print(f'The shape of label is {Y.shape}')

5. 数据预处理

  • 导入数据和标签
cancer = load_breast_cancer()
X = cancer.data
Y = cancer.target
print(f'The shape of cancer dataset is {X.shape}')
print(f'The shape of label is {Y.shape}')

  • 运行结果为
    在这里插入图片描述

6. 特征工程

  • 由于此数据集的特征是构造好的,我们无需构造,所以此步骤省略

7. 建模

  1. 划分训练集合测试集,比例为 8:2
# 划分训练集合测试集,比例为 8:2
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)
print(f'The shape of X_train is {X_train.shape}')
print(f'The shape of y_train is {y_train.shape}')
print(f'The shape of X_test is {X_test.shape}')
print(f'The shape of y_test is {y_test.shape}')
  1. 运行结果为
    在这里插入图片描述
  2. 建模:SVC介绍为:sklearn.svm.SVC
# 建模
clf_svc = SVC()
clf_svc.fit(X_train, y_train)

8. 模型评估

  1. 我们使用两种评估方式,一种是只有准确率,另外一种是分类报告,包括精确度,召回率,F1分等
  2. API介绍:sklearn.metrics.classification_report
  3. 代码如下
# 模型评估
accuracy = clf_svc.score(X_test, y_test)
print(f'The accuracy of the SVC model is {accuracy*100}%')
y_pred = clf_svc.predict(X_test)
print(f'The report of the SVC model is \n {classification_report(y_test, y_pred)}')
  1. 运行结果
    在这里插入图片描述

9. 参数调优

  1. 我们使用了网格搜索法进行参数调优
  2. API介绍网站为:sklearn.model_selection.GridSearchCV
  3. 代码如下
# 参数调优
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svc = SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(X_train, y_train)
print(f'The best parameters are {clf.best_params_}')
accuracy = clf.score(X_test, y_test)
print(f'The accuracy of the SVC model is {accuracy*100}%')
y_pred = clf.predict(X_test)
print(f'The report of the SVC model is \n {classification_report(y_test, y_pred)}')
# 保存模型
joblib.dump(clf, "svc_breast_cancer.m")
  1. 运行结果
    在这里插入图片描述
  2. 由此可见,我们最佳参数为,C=10, kernel =“linear”, 准确率也从 86% 提升到了 98%

10. 模拟线上部署

  1. 这段我们通过time.sleep() 来模拟模型的真实线上部署情况
  2. 代码如下
# 模拟线上部署
clf_online = joblib.load("svc_breast_cancer.m")
for i, e in enumerate(X_test):
    result_SVM = clf_online.predict([e])
    print(f"--------------The {i+1}th data result is: ---------------------- ")
    print(f"The Result of Model is {result_SVM[0]}")
    print(f"The True value is {y_test[i]} \n")
    time.sleep(1)
  1. 运行结果
    在这里插入图片描述

11. 参考资料

  1. 数据集官网介绍:sklearn.datasets.load_breast_cancer
  2. SVC介绍为:sklearn.svm.SVC
  3. API介绍:sklearn.metrics.classification_report
  4. API介绍网站为:sklearn.model_selection.GridSearchCV
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值