python根据4*4的混淆矩阵cm计算查准率、查全率和F1-score
时间: 2024-03-23 13:37:32 浏览: 179
可以使用以下代码来计算查准率、查全率和F1-score:
```python
import numpy as np
cm = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
# 计算真正例(True Positive)
tp = np.diag(cm)
# 计算假正例(False Positive)
fp = np.sum(cm, axis=0) - tp
# 计算假反例(False Negative)
fn = np.sum(cm, axis=1) - tp
# 计算查准率(Precision)
precision = tp / (tp + fp)
# 计算查全率(Recall)
recall = tp / (tp + fn)
# 计算F1-score
f1_score = 2 * precision * recall / (precision + recall)
print("查准率:", precision)
print("查全率:", recall)
print("F1-score:", f1_score)
```
请将上述代码中的 `cm` 数组替换为你的混淆矩阵即可。
相关问题
使用python编程实现逻辑回归模型,在给定数据集上,绘制损失函数曲线图。使用混淆矩阵、错误率、精度、查全率、查准率、F1指标评估逻辑回归模型性能表现,请写出代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
# 生成样本数据
np.random.seed(0)
X_train = np.random.randn(100, 2) * 2 + 2
y_train = np.zeros(100)
X_test = np.random.randn(50, 2) * 2 + 2
y_test = np.zeros(50)
X_train = np.concatenate([X_train, np.random.randn(100, 2) * 2 - 2])
y_train = np.concatenate([y_train, np.ones(100)])
X_test = np.concatenate([X_test, np.random.randn(50, 2) * 2 - 2])
y_test = np.concatenate([y_test, np.ones(50)])
# 训练逻辑回归模型
clf = LogisticRegression()
clf.fit(X_train, y_train)
# 绘制损失函数曲线图
plt.plot(clf.loss_curve_)
plt.xlabel('Iterations')
plt.ylabel('Loss')
plt.show()
# 预测测试数据集
y_pred = clf.predict(X_test)
# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
# 计算错误率和精度
error_rate = 1 - accuracy_score(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
# 计算查全率、查准率和F1指标
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
# 打印结果
print('混淆矩阵:\n', cm)
print('错误率:', error_rate)
print('精度:', accuracy)
print('查全率:', recall)
print('查准率:', precision)
print('F1指标:', f1)
Python代码实现机器学习计算模型预测结果的准确率,错误率,查准率,查全率,F1,ROC和AUC曲线
在Python中,可以使用各种机器学习库和框架来实现模型预测结果的评估指标。以下是一些常用的指标和相应的代码实现示例:
1. 准确率(Accuracy):预测正确的样本数占总样本数的比例。
```python
from sklearn.metrics import accuracy_score
y_true = [0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1]
accuracy = accuracy_score(y_true, y_pred)
print("准确率:", accuracy)
```
2. 错误率(Error Rate):预测错误的样本数占总样本数的比例。
```python
error_rate = 1 - accuracy
print("错误率:", error_rate)
```
3. 查准率(Precision):预测为正例的样本中,真正为正例的比例。
```python
from sklearn.metrics import precision_score
precision = precision_score(y_true, y_pred)
print("查准率:", precision)
```
4. 查全率(Recall):真正为正例的样本中,被正确预测为正例的比例。
```python
from sklearn.metrics import recall_score
recall = recall_score(y_true, y_pred)
print("查全率:", recall)
```
5. F1值(F1 Score):综合考虑了查准率和查全率的指标,是二者的调和平均值。
```python
from sklearn.metrics import f1_score
f1 = f1_score(y_true, y_pred)
print("F1值:", f1)
```
6. ROC曲线和AUC(Receiver Operating Characteristic curve and Area Under the Curve):用于评估二分类模型的性能。ROC曲线是以不同的分类阈值为基础,绘制出真正例率(TPR)和假正例率(FPR)之间的关系曲线。AUC表示ROC曲线下的面积,用于衡量模型的分类能力。
```python
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 计算预测概率
y_score = [0.2, 0.6, 0.8, 0.3, 0.9]
fpr, tpr, thresholds = roc_curve(y_true, y_score)
# 绘制ROC曲线
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % auc(fpr, tpr))
plt.plot([0, 1], [0, 1], 'k--') # 绘制对角线
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc='lower right')
plt.show()
# 计算AUC
auc_score = auc(fpr, tpr)
print("AUC:", auc_score)
```
阅读全文