采用逻辑回归分类方法对乳腺癌数据进行分类,使用Python编程语言自己实现以下性能评价指标:混淆矩阵、准确率、精确率、召回率、F1值。
时间: 2023-07-10 16:06:38 浏览: 45
好的,下面是Python代码实现:
```python
import numpy as np
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
# 导入乳腺癌数据集
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.2, random_state=42)
# 训练模型
lr = LogisticRegression()
lr.fit(X_train, y_train)
# 预测结果
y_pred = lr.predict(X_test)
# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print('Confusion Matrix:\n', cm)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
# 计算精确率
precision = precision_score(y_test, y_pred)
print('Precision:', precision)
# 计算召回率
recall = recall_score(y_test, y_pred)
print('Recall:', recall)
# 计算F1值
f1 = f1_score(y_test, y_pred)
print('F1 Score:', f1)
```
其中,`load_breast_cancer`函数用于导入乳腺癌数据集,`train_test_split`函数用于划分训练集和测试集,`LogisticRegression`类用于训练逻辑回归模型,`confusion_matrix`、`accuracy_score`、`precision_score`、`recall_score`、`f1_score`函数用于计算各种性能评价指标。
阅读全文