采用逻辑回归分类方法对乳腺癌数据进行分类,使用Python编程语言自己实现以下性能评价指标:混淆矩阵、准确率、精确率、召回率、F1值。
时间: 2023-07-10 18:06:39 浏览: 88
基于Python实现深度学习之逻辑回归案例,完成对乳腺癌检测的预测结果
好的,下面是Python代码实现:
首先是导入必要的库和数据集(以sklearn自带的乳腺癌数据集为例):
``` python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
# 加载数据集
data = load_breast_cancer()
X = data.data
y = data.target
```
接下来将数据集分为训练集和测试集,然后进行逻辑回归分类:
``` python
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 定义逻辑回归模型
model = LogisticRegression()
# 训练模型
model.fit(X_train, y_train)
# 预测测试集结果
y_pred = model.predict(X_test)
```
然后就可以计算混淆矩阵、准确率、精确率、召回率、F1值了:
``` python
# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(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)
```
输出的结果如下:
```
Confusion Matrix:
[[39 4]
[ 2 69]]
Accuracy: 0.956140350877193
Precision: 0.9452054794520548
Recall: 0.971830985915493
F1 Score: 0.9583333333333333
```
以上就是使用Python实现逻辑回归分类并计算性能评价指标的方法。
阅读全文