支持向量机分析乳腺癌数据

要求

(1)     得出相应的分类指标准确率accuracy,精确率precision,召回率recall,F1-score,并画出最终的ROC曲线,得出AUC值。

(2)     对比感知机算法(调包就行)也进行训练和测试,比较两个算法的结果。

代码

import pandas as pd
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.linear_model import Perceptron
from sklearn.metrics import roc_curve, auc, classification_report
from sklearn.model_selection import cross_val_score, cross_val_predict


def acu_curve(y, prob):
    fpr, tpr, threshold = roc_curve(y, prob)  ###计算真正率和假正率
    roc_auc = auc(fpr, tpr)  ###计算auc的值
    print("AUC:", roc_auc)
    plt.figure()
    lw = 2
    plt.figure(figsize=(6, 6))
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.3f)' % roc_auc)  ###假正率为横坐标,真正率为纵坐标做曲线
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic')
    plt.legend(loc="lower right")
    plt.show()


# 加载数据
train = pd.read_csv(r".\breast-cancer-train.csv").iloc[:, 1:]
test = pd.read_csv(r".\breast-cancer-test.csv").iloc[:, 1:]

x_train, y_train = train.iloc[:, :-1], train.iloc[:, -1]
x_test, y_test = test.iloc[:, :-1], test.iloc[:, -1]

# 用线性核函数建立支持向量机模型
model = svm.SVC(kernel='linear', probability=True)
model.fit(x_train, y_train)
print('------------------------SVM------------------------')
print(classification_report(y_test,model.predict(x_test)))
# 画出ROC曲线
acu_curve(y_test, model.predict_proba(x_test)[:,1:])

print('------------------------感知机算法------------------------')
# 感知机算法
model2 = Perceptron()
model2.fit(x_train, y_train)
print(classification_report(y_test, model2.predict(x_test)))
acu_curve(y_test, model2.predict(x_test))

注意

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rhyme_7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值