用python语言利用支持向量机算法、决策树、logistic回归算法对乳腺癌数据集进行分析并绘制曲线图,输出三种模型评估参数,包括准确率、精确率、召回率
时间: 2023-07-10 19:11:07 浏览: 134
好的,以下是利用支持向量机算法、决策树、logistic回归算法对乳腺癌数据集进行分析并绘制曲线图,输出三种模型评估参数的 Python 代码。这里使用 scikit-learn 库进行建模和评估:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
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
# 加载数据集
breast_cancer = datasets.load_breast_cancer()
X = breast_cancer.data
y = breast_cancer.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# SVM模型
svm = SVC(kernel='linear', C=1, random_state=42)
svm.fit(X_train, y_train)
svm_y_pred = svm.predict(X_test)
svm_accuracy = accuracy_score(y_test, svm_y_pred)
svm_precision = precision_score(y_test, svm_y_pred)
svm_recall = recall_score(y_test, svm_y_pred)
# 决策树模型
dt = DecisionTreeClassifier(max_depth=3, random_state=42)
dt.fit(X_train, y_train)
dt_y_pred = dt.predict(X_test)
dt_accuracy = accuracy_score(y_test, dt_y_pred)
dt_precision = precision_score(y_test, dt_y_pred)
dt_recall = recall_score(y_test, dt_y_pred)
# Logistic回归模型
lr = LogisticRegression(C=1, random_state=42)
lr.fit(X_train, y_train)
lr_y_pred = lr.predict(X_test)
lr_accuracy = accuracy_score(y_test, lr_y_pred)
lr_precision = precision_score(y_test, lr_y_pred)
lr_recall = recall_score(y_test, lr_y_pred)
# 输出评估参数
print('SVM模型:准确率={:.3f},精确率={:.3f},召回率={:.3f}'.format(svm_accuracy, svm_precision, svm_recall))
print('决策树模型:准确率={:.3f},精确率={:.3f},召回率={:.3f}'.format(dt_accuracy, dt_precision, dt_recall))
print('Logistic回归模型:准确率={:.3f},精确率={:.3f},召回率={:.3f}'.format(lr_accuracy, lr_precision, lr_recall))
# 绘制曲线图
plt.figure(figsize=(8, 6))
plt.bar(['SVM', '决策树', 'Logistic回归'], [svm_accuracy, dt_accuracy, lr_accuracy], alpha=0.5, label='准确率')
plt.bar(['SVM', '决策树', 'Logistic回归'], [svm_precision, dt_precision, lr_precision], alpha=0.5, label='精确率')
plt.bar(['SVM', '决策树', 'Logistic回归'], [svm_recall, dt_recall, lr_recall], alpha=0.5, label='召回率')
plt.legend()
plt.title('三种模型评估参数比较')
plt.show()
```
运行该代码后,会输出三种模型的评估参数,同时绘制一个包含准确率、精确率和召回率的柱状图,用于比较三种模型在这些评估指标上的表现。
阅读全文