使用svc预测乳腺癌的发病率

作者: Brave 分类: 学习 发布时间: 2018-10-07 18:20
import pandas as pd
from sklearn.preprocessing import LabelEncoder  #字符编码,区分良性还是恶性肿瘤
from sklearn.cross_validation import train_test_split  #训练集和测试集划分
from sklearn.preprocessing import StandardScaler  #将数据进行标准化,归一化
from sklearn.pipeline import Pipeline  #封装测试环境
from sklearn.svm import SVC  #svm支持向量机,svc是改良版
import numpy as np
from sklearn.metrics import  confusion_matrix  #混淆矩阵
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score  #查全率 、查准率 、F1 score
#下载病例数据
file = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data",header=None)
df = file
X = df.loc[:,2:].values  #获取病例特征
y = df.loc[:,1].values  #获取诊断结果
le = LabelEncoder()
y = le.fit_transform(y)  #分类整数化
#划分训练集、测试集
X_train,X_test,y_train,y_test = train_test_split(X, y, test_size = 0.2, random_state = 1)
#建立测试环境
pipe_svc = Pipeline([('scl',StandardScaler()),('clf', SVC(random_state=1))])
#训练数据
pipe_svc.fit(X_train, y_train)
#预测测试集数据
y_pred = pipe_svc.predict(X_test)
#混淆矩阵并可视化
confmat = confusion_matrix(y_true = y_test, y_pred=y_pred)
#画图
fig, ax = plt.subplots(figsize = (2.5, 2.5))
ax.matshow(confmat, cmap = plt.cm.Blues, alpha = 0.3)
for i in range(confmat.shape[0]):
    for j in range(confmat.shape[1]):
        ax.text(x = j, y = i, s = confmat[i, j], va = "center", ha = 'center')
plt.xlabel('predicted label')
plt.ylabel('true label')
plt.show()

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注