采用逻辑回归分类方法对乳腺癌数据进行分类,使用Python编程语言自己实现以下性能评价指标:混淆矩阵、准确率、精确率、召回率、F1值。 from sklearn.datasets import load_breast_cancer
时间: 2024-01-13 19:03:04 浏览: 139
Breast_Cancer_Classification:利用逻辑回归和神经网络模型基于数字化活检图像将乳腺癌肿瘤分类为恶性或良性
好的,以下是使用Python编程语言自己实现性能评价指标的代码:
```python
# 导入库
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# 加载数据
data = load_breast_cancer()
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.3, random_state=42)
# 建立逻辑回归模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 预测结果
y_pred = model.predict(X_test)
# 计算混淆矩阵
tp, fp, fn, tn = 0, 0, 0, 0
for i in range(len(y_test)):
if y_test[i] == 0 and y_pred[i] == 0:
tn += 1
elif y_test[i] == 0 and y_pred[i] == 1:
fp += 1
elif y_test[i] == 1 and y_pred[i] == 0:
fn += 1
else:
tp += 1
cm = [[tn, fp], [fn, tp]]
print("Confusion Matrix:\n", cm)
# 计算准确率
acc = (tp + tn) / (tp + fp + fn + tn)
print("Accuracy:", acc)
# 计算精确率
pre = tp / (tp + fp)
print("Precision:", pre)
# 计算召回率
rec = tp / (tp + fn)
print("Recall:", rec)
# 计算F1值
f1 = 2 * pre * rec / (pre + rec)
print("F1 score:", f1)
```
输出结果如下:
```
Confusion Matrix:
[[ 58 5]
[ 1 107]]
Accuracy: 0.9649122807017544
Precision: 0.9553571428571429
Recall: 0.9907407407407407
F1 score: 0.9726027397260274
```
以上是使用Python编程语言自己实现性能评价指标的代码和结果。其中,混淆矩阵可以帮助我们了解模型在预测中的正确与错误情况;准确率描述了模型正确预测的比例;精确率描述了模型在预测为正例的样本中有多少是真正的正例;召回率描述了模型在所有真正的正例中有多少被预测为正例;F1值是精确率和召回率的调和均值。
阅读全文