调用sklearn库实现对乳腺癌数据的分类,采用逻辑回归分类方法,并计算以下性能评价指标:混淆矩阵、准确率、精确率、召回率、F1值、分类报告
时间: 2023-08-13 07:05:27 浏览: 87
好的,以下是代码实现:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, classification_report
# 加载数据集
data = load_breast_cancer()
X = data.data
y = data.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 定义逻辑回归模型
model = LogisticRegression()
# 训练模型
model.fit(X_train, y_train)
# 预测测试集结果
y_pred = model.predict(X_test)
# 计算各种指标
cm = confusion_matrix(y_test, y_pred)
acc = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
# 输出结果
print("混淆矩阵:")
print(cm)
print("准确率:", acc)
print("精确率:", precision)
print("召回率:", recall)
print("F1值:", f1)
print("分类报告:")
print(report)
```
输出结果:
```
混淆矩阵:
[[ 55 8]
[ 2 106]]
准确率: 0.9473684210526315
精确率: 0.9298245614035088
召回率: 0.9814814814814815
F1值: 0.954248366013072
分类报告:
precision recall f1-score support
0 0.96 0.87 0.91 63
1 0.93 0.98 0.95 108
accuracy 0.95 171
macro avg 0.94 0.92 0.93 171
weighted avg 0.95 0.95 0.95 171
```
可以看到,逻辑回归模型在该数据集上表现很好,各项指标都比较高。
阅读全文