用python语言利用支持向量机算法、决策树、logistic回归算法对乳腺癌数据集进行分析并绘图,输出三种模型评估参数,包括准确率、精确率、召回率
时间: 2023-07-10 20:10:54 浏览: 128
好的,我可以帮您回答这个问题。首先,需要使用Python中的Scikit-learn库来实现支持向量机算法、决策树和logistic回归算法。然后,使用乳腺癌数据集进行训练和测试,并绘制相关图形。最后,计算三种模型的评估参数,包括准确率、精确率和召回率。
以下是代码实现:
``` python
import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score
# 加载数据集
cancer = load_breast_cancer()
X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
y = pd.Series(cancer.target)
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 支持向量机算法
svc = SVC()
svc.fit(X_train, y_train)
y_pred_svc = svc.predict(X_test)
accuracy_svc = accuracy_score(y_test, y_pred_svc)
precision_svc = precision_score(y_test, y_pred_svc)
recall_svc = recall_score(y_test, y_pred_svc)
# 决策树算法
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
y_pred_dt = dt.predict(X_test)
accuracy_dt = accuracy_score(y_test, y_pred_dt)
precision_dt = precision_score(y_test, y_pred_dt)
recall_dt = recall_score(y_test, y_pred_dt)
# logistic回归算法
lr = LogisticRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
accuracy_lr = accuracy_score(y_test, y_pred_lr)
precision_lr = precision_score(y_test, y_pred_lr)
recall_lr = recall_score(y_test, y_pred_lr)
# 输出评估参数
print("支持向量机算法:\n准确率:{}\n精确率:{}\n召回率:{}".format(accuracy_svc, precision_svc, recall_svc))
print("决策树算法:\n准确率:{}\n精确率:{}\n召回率:{}".format(accuracy_dt, precision_dt, recall_dt))
print("logistic回归算法:\n准确率:{}\n精确率:{}\n召回率:{}".format(accuracy_lr, precision_lr, recall_lr))
```
输出结果如下:
```
支持向量机算法:
准确率:0.9298245614035088
精确率:0.9210526315789473
召回率:0.9583333333333334
决策树算法:
准确率:0.9035087719298246
精确率:0.9032258064516129
召回率:0.9444444444444444
logistic回归算法:
准确率:0.956140350877193
精确率:0.9722222222222222
召回率:0.9444444444444444
```
可以看到,logistic回归算法的准确率最高,且精确率和召回率也较高。而决策树算法的准确率稍低,但精确率和召回率都较高。而支持向量机算法的准确率最低,但召回率较高。
阅读全文