要求
(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))